Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/fix-react-runtime-execid-churn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@lynx-js/react": patch
---

fix: reduce redundant updates for main-thread handlers and gestures

- Updates are faster when the main-thread event handler or gesture object is stable across rerenders (fewer unnecessary native updates).
- Spread props rerenders that don't semantically change the handler/gesture no longer trigger redundant updates.
- Removing a gesture from spread props reliably clears the gesture state on the target element.
2 changes: 1 addition & 1 deletion packages/lynx/gesture-runtime/src/baseGesture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ abstract class BaseGesture<
return removeUndefined(result);
};

toJSON = (): Record<string, unknown> => {
toJSON = function(this: BaseGesture): Record<string, unknown> {
return this.serialize();
};

Expand Down
2 changes: 1 addition & 1 deletion packages/lynx/gesture-runtime/src/composition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class ComposedGesture implements GestureKind {
};
};

toJSON = (): Record<string, unknown> => {
toJSON = function(this: ComposedGesture): Record<string, unknown> {
return this.serialize();
};
}
Expand Down
160 changes: 121 additions & 39 deletions packages/react/runtime/__test__/snapshot/gesture.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@ describe('Gesture', () => {
},
},
__isGesture: true,
toJSON: () => ({
...gesture,
__isSerialized: true,
}),
toJSON: function() {
return {
...this,
__isSerialized: true,
};
},
};

return (
Expand Down Expand Up @@ -171,10 +173,12 @@ describe('Gesture', () => {
simultaneousWith: [{ id: 2 }],
continueWith: [{ id: 2 }],
__isGesture: true,
toJSON: () => ({
...panGesture,
__isSerialized: true,
}),
toJSON: function() {
return {
...this,
__isSerialized: true,
};
},
};
const tapGesture = {
id: 2,
Expand All @@ -186,20 +190,24 @@ describe('Gesture', () => {
},
__isGesture: true,
waitFor: [{ id: 1 }],
toJSON: () => ({
...tapGesture,
__isSerialized: true,
}),
toJSON: function() {
return {
...this,
__isSerialized: true,
};
},
};

const gesture = {
type: -1,
__isGesture: true,
gestures: [panGesture, tapGesture],
toJSON: () => ({
...gesture,
__isSerialized: true,
}),
toJSON: function() {
return {
...this,
__isSerialized: true,
};
},
};

return (
Expand Down Expand Up @@ -302,10 +310,9 @@ describe('Gesture', () => {
},
},
__isGesture: true,
toJSON() {
const { toJSON, ...rest } = this;
toJSON: function() {
return {
...rest,
...this,
__isSerialized: true,
};
},
Expand Down Expand Up @@ -443,10 +450,9 @@ describe('Gesture', () => {
},
},
__isGesture: true,
toJSON() {
const { toJSON, ...rest } = this;
toJSON: function() {
return {
...rest,
...this,
__isSerialized: true,
};
},
Expand Down Expand Up @@ -568,10 +574,12 @@ describe('Gesture', () => {
minDistance: 100,
},
__isGesture: true,
toJSON: () => ({
...gesture,
__isSerialized: true,
}),
toJSON: function() {
return {
...this,
__isSerialized: true,
};
},
};

return (
Expand Down Expand Up @@ -676,10 +684,12 @@ describe('Gesture in spread', () => {
},
},
__isGesture: true,
toJSON: () => ({
...gesture,
__isSerialized: true,
}),
toJSON: function() {
return {
...this,
__isSerialized: true,
};
},
};

const props = {
Expand Down Expand Up @@ -806,10 +816,9 @@ describe('Gesture in spread', () => {
},
},
__isGesture: true,
toJSON() {
const { toJSON, ...rest } = this;
toJSON: function() {
return {
...rest,
...this,
__isSerialized: true,
};
},
Expand Down Expand Up @@ -915,10 +924,9 @@ describe('Gesture in spread', () => {
},
},
__isGesture: true,
toJSON() {
const { toJSON, ...rest } = this;
toJSON: function() {
return {
...rest,
...this,
__isSerialized: true,
};
},
Expand Down Expand Up @@ -1041,10 +1049,9 @@ describe('Gesture in spread', () => {
},
},
__isGesture: true,
toJSON() {
const { toJSON, ...rest } = this;
toJSON: function() {
return {
...rest,
...this,
__isSerialized: true,
};
},
Expand Down Expand Up @@ -1117,6 +1124,81 @@ describe('Gesture in spread', () => {
expect(elementTree.__GetGestureDetectorIds(textElement).includes(1)).toBe(false);
}
});
it('keeps flatten when spread removes gesture but retains no-flatten attrs', async function() {
const spyRemoveGesture = vi.spyOn(globalThis, '__RemoveGestureDetector');
let _gesture = {
id: 1,
type: 0,
callbacks: {
onUpdate: {
_wkltId: 'bdd4:dd564:2',
},
},
__isGesture: true,
toJSON: function() {
return {
...this,
__isSerialized: true,
};
},
};
let keepGesture = true;

function Comp() {
const props = keepGesture
? {
'clip-radius': 8,
'main-thread:gesture': _gesture,
}
: {
'clip-radius': 8,
};
return (
<view>
<text {...props}>1</text>
</view>
);
}

{
__root.__jsx = <Comp />;
renderPage();
}

{
globalEnvManager.switchToBackground();
render(<Comp />, __root);
}

{
lynxCoreInject.tt.OnLifecycleEvent(...globalThis.__OnLifecycleEvent.mock.calls[0]);

globalEnvManager.switchToMainThread();
const rLynxChange = lynx.getNativeApp().callLepusMethod.mock.calls[0];
globalThis[rLynxChange[0]](rLynxChange[1]);
}

{
globalEnvManager.switchToBackground();
lynx.getNativeApp().callLepusMethod.mockClear();
spyRemoveGesture.mockClear();
keepGesture = false;

render(<Comp />, __root);

globalEnvManager.switchToMainThread();
const rLynxChange = lynx.getNativeApp().callLepusMethod.mock.calls[0];
globalThis[rLynxChange[0]](rLynxChange[1]);
const textElement = __root.__element_root.children[0].children[0];

expect(spyRemoveGesture).toHaveBeenCalledTimes(1);
expect(spyRemoveGesture).toHaveBeenCalledWith(textElement, 1);
expect(textElement.props['clip-radius']).toBe(8);
expect(textElement.props.flatten).toBe(false);
expect(textElement.props['has-react-gesture']).toBeUndefined();
expect(textElement.props.gesture).toBeUndefined();
}
});
it('remove stale detector ids when gesture count shrinks on diff', async function() {
const spySetGesture = vi.spyOn(globalThis, '__SetGestureDetector');
const spyRemoveGesture = vi.spyOn(globalThis, '__RemoveGestureDetector');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { afterEach, beforeEach, describe, expect, it } from 'vitest';

import { prepareGestureForCommit } from '../../../src/snapshot/gesture/processGestureBagkround';
import { clearConfigCacheForTesting } from '../../../src/snapshot/worklet/functionality';

describe('prepareGestureForCommit', () => {
let previousSdkVersion;

beforeEach(() => {
previousSdkVersion = SystemInfo.lynxSdkVersion;
SystemInfo.lynxSdkVersion = '2.14';
clearConfigCacheForTesting();
});

afterEach(() => {
SystemInfo.lynxSdkVersion = previousSdkVersion;
clearConfigCacheForTesting();
});

it('does not mutate input gesture and supports non-object callbacks', () => {
const gesture = {
id: 1,
type: 0,
callbacks: {
onUpdate: null,
},
__isGesture: true,
toJSON() {
const { toJSON, ...rest } = this;
return {
...rest,
__isSerialized: true,
};
},
};

const committed = prepareGestureForCommit(gesture);
expect(committed).not.toBe(gesture);
expect(committed.callbacks).not.toBe(gesture.callbacks);
expect(committed.callbacks.onUpdate).toBe(null);

expect(committed.toJSON).not.toBe(gesture.toJSON);

// Gesture runtime provides toJSON; ensure the committed object still serializes.
const json = committed.toJSON();
expect(json.__isSerialized).toBe(true);
});

it('serializes committed callbacks even when the source toJSON closes over the original gesture', () => {
const gesture = {
id: 1,
type: 0,
callbacks: {
onUpdate: {
_wkltId: 'bdd4:dd564:2',
},
},
waitFor: [],
simultaneousWith: [],
continueWith: [],
__isGesture: true,
};
gesture.toJSON = () => ({
id: gesture.id,
type: gesture.type,
callbacks: gesture.callbacks,
waitFor: [],
simultaneousWith: [],
continueWith: [],
__isSerialized: true,
});

const committed = prepareGestureForCommit(gesture);
const json = committed.toJSON();

expect(committed.callbacks.onUpdate).toEqual({
_wkltId: 'bdd4:dd564:2',
});
expect(committed.callbacks.onUpdate).not.toBe(gesture.callbacks.onUpdate);
expect(json.callbacks.onUpdate).toEqual({
_wkltId: 'bdd4:dd564:2',
});
expect(json.callbacks.onUpdate).not.toBe(gesture.callbacks.onUpdate);
expect(json.callbacks.onUpdate).toBe(committed.callbacks.onUpdate);
});
});
Loading
Loading