-
Notifications
You must be signed in to change notification settings - Fork 124
fix(react): retain offscreen worklet ctx refs #2592
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
Yradex
merged 1 commit into
lynx-family:main
from
Yradex:wt/run-on-background-retain-offscreen
May 12, 2026
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/react": patch | ||
| --- | ||
|
|
||
| Retain main-thread worklet context references before offscreen snapshot elements are materialized, so event, ref, gesture, and spread callbacks stay alive until the DOM update path can attach them. |
133 changes: 133 additions & 0 deletions
133
packages/react/runtime/__test__/snapshot/workletLifecycle.test.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,133 @@ | ||
| // Copyright 2026 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 { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { updateGesture } from '../../src/snapshot/snapshot/gesture'; | ||
| import { updateSpread } from '../../src/snapshot/snapshot/spread'; | ||
| import { updateWorkletEvent } from '../../src/snapshot/snapshot/workletEvent'; | ||
| import { updateWorkletRef } from '../../src/snapshot/snapshot/workletRef'; | ||
|
|
||
| function createSnapshot(value: unknown) { | ||
| return { | ||
| __id: 1, | ||
| __values: [value], | ||
| type: 'TestSnapshot', | ||
| } as any; | ||
| } | ||
|
|
||
| describe('worklet lifecycle without elements', () => { | ||
| let addRef: ReturnType<typeof vi.fn>; | ||
|
|
||
| beforeEach(() => { | ||
| addRef = vi.fn(); | ||
| globalThis.lynxWorkletImpl = { | ||
| _jsFunctionLifecycleManager: { | ||
| addRef, | ||
| }, | ||
| } as any; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| delete globalThis.lynxWorkletImpl; | ||
| }); | ||
|
|
||
| it('retains main-thread event worklet ctx before elements are materialized', () => { | ||
| const worklet = { | ||
| _execId: 1, | ||
| _wkltId: 'event', | ||
| }; | ||
|
|
||
| updateWorkletEvent(createSnapshot(worklet), 0, undefined as any, 0, 'main-thread', 'bindEvent', 'tap'); | ||
|
|
||
| expect(addRef).toHaveBeenCalledTimes(1); | ||
| expect(addRef).toHaveBeenCalledWith(1, worklet); | ||
| }); | ||
|
|
||
| it('retains main-thread ref worklet ctx before elements are materialized', () => { | ||
| const worklet = { | ||
| _execId: 2, | ||
| _wkltId: 'ref', | ||
| }; | ||
|
|
||
| updateWorkletRef(createSnapshot(worklet), 0, undefined, 0, 'main-thread'); | ||
|
|
||
| expect(addRef).toHaveBeenCalledTimes(1); | ||
| expect(addRef).toHaveBeenCalledWith(2, worklet); | ||
| }); | ||
|
|
||
| it('retains main-thread gesture callbacks before elements are materialized', () => { | ||
| const callback = { | ||
| _execId: 3, | ||
| _wkltId: 'gesture', | ||
| }; | ||
| const gesture = { | ||
| __isSerialized: true, | ||
| callbacks: { | ||
| onUpdate: callback, | ||
| }, | ||
| id: 1, | ||
| type: 0, | ||
| }; | ||
|
|
||
| updateGesture(createSnapshot(gesture), 0, undefined, 0, 'main-thread'); | ||
|
|
||
| expect(addRef).toHaveBeenCalledTimes(1); | ||
| expect(addRef).toHaveBeenCalledWith(3, callback); | ||
| }); | ||
|
|
||
| it('retains main-thread spread worklet ctx before elements are materialized', () => { | ||
| const eventWorklet = { | ||
| _execId: 4, | ||
| _wkltId: 'spread-event', | ||
| }; | ||
| const refWorklet = { | ||
| _execId: 5, | ||
| _wkltId: 'spread-ref', | ||
| }; | ||
| const gestureCallback = { | ||
| _execId: 6, | ||
| _wkltId: 'spread-gesture', | ||
| }; | ||
| const gesture = { | ||
| __isSerialized: true, | ||
| callbacks: { | ||
| onUpdate: gestureCallback, | ||
| }, | ||
| id: 1, | ||
| type: 0, | ||
| }; | ||
|
|
||
| updateSpread( | ||
| createSnapshot({ | ||
| 'main-thread:bindtap': eventWorklet, | ||
| 'main-thread:gesture': gesture, | ||
| 'main-thread:ref': refWorklet, | ||
| }), | ||
| 0, | ||
| {}, | ||
| 0, | ||
| ); | ||
|
|
||
| expect(addRef.mock.calls).toEqual([ | ||
| [4, eventWorklet], | ||
| [6, gestureCallback], | ||
| [5, refWorklet], | ||
| ]); | ||
| }); | ||
|
|
||
| it('does not retain unchanged spread worklet ctx again before elements are materialized', () => { | ||
| const eventWorklet = { | ||
| _execId: 7, | ||
| _wkltId: 'spread-event', | ||
| }; | ||
| const spread = { | ||
| 'main-thread:bindtap': eventWorklet, | ||
| }; | ||
|
|
||
| updateSpread(createSnapshot(spread), 0, spread, 0); | ||
|
|
||
| expect(addRef).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
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
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
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
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.