-
Notifications
You must be signed in to change notification settings - Fork 122
feat: gesture callback memo #2277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@lynx-js/gesture-runtime': patch | ||
| --- | ||
|
|
||
| Optimize gesture callbacks and relationships to prevent unnecessary gesture registration and rerenders. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
packages/lynx/gesture-runtime/__test__/useMainThreadMemoizedFn.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| // Copyright 2025 The Lynx Authors. All rights reserved. | ||
| // Licensed under the Apache License Version 2.0 that can be found in the | ||
| // LICENSE file in the root directory of this source tree. | ||
|
|
||
| import { useState } from '@lynx-js/react'; | ||
| import { act, render } from '@lynx-js/react/testing-library'; | ||
| import { beforeEach, describe, expect, test, vi } from 'vitest'; | ||
|
|
||
| import { useMainThreadMemoizedFn } from '../src/utils/useMainThreadMemoizedFn.js'; | ||
|
|
||
| describe('useMainThreadMemoizedFn', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| test('should return a stable reference but execute the latest logic on background thread', async () => { | ||
| let memoizedFnRef: any; | ||
| let _setCount: any; | ||
|
|
||
| const App = () => { | ||
| const [count, setCount] = useState(0); | ||
| _setCount = setCount; | ||
|
|
||
| const memoizedFn = useMainThreadMemoizedFn(() => { | ||
| 'main thread'; | ||
| return count; | ||
| }); | ||
|
|
||
| memoizedFnRef = memoizedFn; | ||
|
|
||
| return <view></view>; | ||
| }; | ||
|
|
||
| render(<App />); | ||
|
|
||
| const fn1 = memoizedFnRef; | ||
| let res = globalThis.runWorklet(fn1, []); | ||
| expect(res).toBe(0); | ||
|
|
||
| await act(() => { | ||
| _setCount(1); | ||
| }); | ||
|
|
||
| const fn2 = memoizedFnRef; | ||
| expect(fn1).toBe(fn2); // Stable reference! | ||
|
|
||
| res = globalThis.runWorklet(fn2, []); | ||
| expect(res).toBe(1); // Latest logic! | ||
| }); | ||
|
|
||
| test('should return a stable reference but execute the latest logic on main thread', async () => { | ||
| let memoizedFnRef: any; | ||
| let _setCount: any; | ||
|
|
||
| const App = () => { | ||
| const [count, setCount] = useState(0); | ||
| _setCount = setCount; | ||
|
|
||
| const memoizedFn = useMainThreadMemoizedFn(() => { | ||
| 'main thread'; | ||
| return count; | ||
| }); | ||
|
|
||
| memoizedFnRef = memoizedFn; | ||
|
|
||
| return <view></view>; | ||
| }; | ||
|
|
||
| await act(() => { | ||
| render(<App />, { | ||
| enableMainThread: true, | ||
| enableBackgroundThread: true, | ||
| }); | ||
| }); | ||
|
|
||
| const fn1 = memoizedFnRef; | ||
| let res = globalThis.runWorklet(fn1, []); | ||
| expect(res).toBe(0); | ||
|
|
||
| await act(() => { | ||
| _setCount(1); | ||
| }); | ||
|
|
||
| const fn2 = memoizedFnRef; | ||
| expect(fn1).toBe(fn2); // Stable reference! | ||
|
|
||
| res = globalThis.runWorklet(fn2, []); | ||
| expect(res).toBe(1); // Latest logic! | ||
| }); | ||
|
|
||
| test('should pass arguments properly and return value', () => { | ||
| let memoizedFnRef: any; | ||
|
|
||
| const App = () => { | ||
| const memoizedFn = useMainThreadMemoizedFn((a: number, b: number) => { | ||
| 'main thread'; | ||
| return a + b; | ||
| }); | ||
|
|
||
| memoizedFnRef = memoizedFn; | ||
|
|
||
| return <view></view>; | ||
| }; | ||
|
|
||
| render(<App />); | ||
|
|
||
| expect(globalThis.runWorklet(memoizedFnRef, [2, 3])).toBe(5); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
packages/lynx/gesture-runtime/src/utils/useMainThreadMemoizedFn.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| // Copyright 2025 The Lynx Authors. All rights reserved. | ||
| // Licensed under the Apache License Version 2.0 that can be found in the | ||
| // LICENSE file in the root directory of this source tree. | ||
|
|
||
| import { runOnMainThread, useMainThreadRef, useMemo } from '@lynx-js/react'; | ||
| import { runWorkletCtx } from '@lynx-js/react/worklet-runtime/bindings'; | ||
|
|
||
| type noop = (this: unknown, ...args: unknown[]) => unknown; | ||
|
|
||
| type PickFunction<T extends noop> = ( | ||
| this: ThisParameterType<T>, | ||
| ...args: Parameters<T> | ||
| ) => ReturnType<T>; | ||
|
|
||
| /** | ||
| * @internal | ||
| * Hooks for persistent main thread functions. | ||
| * It ensures the returned function has a stable reference while always executing the latest logic on the main thread. | ||
| * @example | ||
| * ```tsx | ||
| * const handleScroll = useMainThreadMemoizedFn((e: MainThread.TouchEvent) => { | ||
| * 'main thread'; | ||
| * console.log(count); // Access captured variable | ||
| * }); | ||
| * ``` | ||
| */ | ||
| export function useMainThreadMemoizedFn<T extends noop>(fn: T): T { | ||
|
Yradex marked this conversation as resolved.
|
||
| // Create a ref on the main thread to hold the function | ||
| const fnMTRef = useMainThreadRef<T>(fn); | ||
|
|
||
| // Synchronize the latest function to the main thread ref during render | ||
| useMemo(() => { | ||
| if (__MAIN_THREAD__) { | ||
| /* v8 ignore next 5 */ | ||
| // @ts-expect-error - This is a worklet context, we can directly assign to the ref | ||
| runWorkletCtx(() => { | ||
| 'main thread'; | ||
| fnMTRef.current = fn; | ||
| }, []); | ||
| } else { | ||
| /* v8 ignore next 4 */ | ||
| void runOnMainThread((latestFn: T) => { | ||
| 'main thread'; | ||
| fnMTRef.current = latestFn; | ||
| })(fn); | ||
| } | ||
| }, [fn]); | ||
|
|
||
| // Return a stable wrapper function | ||
| const memoizedFn = useMemo<PickFunction<T>>(() => { | ||
| /* v8 ignore next 10 */ | ||
| return function(this: ThisParameterType<T>, ...args: Parameters<T>) { | ||
| 'main thread'; | ||
| // Call the latest function stored in the ref | ||
| const currentFn = fnMTRef.current; | ||
| if (currentFn) { | ||
| return currentFn.apply(this, args) as ReturnType<T>; | ||
| } | ||
| return undefined as ReturnType<T>; | ||
| }; | ||
| }, []); | ||
|
|
||
| return memoizedFn as unknown as T; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.