@lynx-js/react 0.108.0 → 0.109.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/package.json +4 -4
  3. package/refresh/.turbo/turbo-build.log +1 -1
  4. package/runtime/jsx-dev-runtime/index.d.ts +13 -7
  5. package/runtime/jsx-runtime/index.d.ts +17 -7
  6. package/runtime/lib/gesture/processGesture.d.ts +2 -2
  7. package/runtime/lib/gesture/processGesture.js +14 -7
  8. package/runtime/lib/gesture/processGesture.js.map +1 -1
  9. package/runtime/lib/lifecycle/patch/isMainThreadHydrationFinished.d.ts +2 -0
  10. package/runtime/lib/lifecycle/patch/isMainThreadHydrationFinished.js +9 -0
  11. package/runtime/lib/lifecycle/patch/isMainThreadHydrationFinished.js.map +1 -0
  12. package/runtime/lib/lifecycle/patch/snapshotPatchApply.js +2 -2
  13. package/runtime/lib/lifecycle/patch/snapshotPatchApply.js.map +1 -1
  14. package/runtime/lib/lifecycle/patch/updateMainThread.js +4 -3
  15. package/runtime/lib/lifecycle/patch/updateMainThread.js.map +1 -1
  16. package/runtime/lib/lifecycle/reload.js +2 -0
  17. package/runtime/lib/lifecycle/reload.js.map +1 -1
  18. package/runtime/lib/snapshot/gesture.d.ts +1 -1
  19. package/runtime/lib/snapshot/gesture.js +4 -3
  20. package/runtime/lib/snapshot/gesture.js.map +1 -1
  21. package/runtime/lib/snapshot/ref.js.map +1 -1
  22. package/runtime/lib/snapshot/spread.d.ts +0 -6
  23. package/runtime/lib/snapshot/spread.js +4 -10
  24. package/runtime/lib/snapshot/spread.js.map +1 -1
  25. package/runtime/lib/snapshot/workletEvent.d.ts +2 -1
  26. package/runtime/lib/snapshot/workletEvent.js +4 -3
  27. package/runtime/lib/snapshot/workletEvent.js.map +1 -1
  28. package/runtime/lib/snapshot/workletRef.d.ts +4 -4
  29. package/runtime/lib/snapshot/workletRef.js +6 -4
  30. package/runtime/lib/snapshot/workletRef.js.map +1 -1
  31. package/runtime/lib/snapshot.d.ts +1 -0
  32. package/runtime/lib/snapshot.js +5 -4
  33. package/runtime/lib/snapshot.js.map +1 -1
  34. package/runtime/lib/worklet/runOnBackground.js +21 -12
  35. package/runtime/lib/worklet/runOnBackground.js.map +1 -1
  36. package/runtime/lib/worklet/workletRef.d.ts +1 -0
  37. package/runtime/lib/worklet/workletRef.js +14 -12
  38. package/runtime/lib/worklet/workletRef.js.map +1 -1
  39. package/runtime/src/gesture/processGesture.ts +23 -15
  40. package/runtime/src/lifecycle/patch/isMainThreadHydrationFinished.ts +10 -0
  41. package/runtime/src/lifecycle/patch/snapshotPatchApply.ts +8 -2
  42. package/runtime/src/lifecycle/patch/updateMainThread.ts +4 -3
  43. package/runtime/src/lifecycle/reload.ts +2 -0
  44. package/runtime/src/snapshot/gesture.ts +6 -4
  45. package/runtime/src/snapshot/ref.ts +2 -2
  46. package/runtime/src/snapshot/spread.ts +15 -5
  47. package/runtime/src/snapshot/workletEvent.ts +5 -3
  48. package/runtime/src/snapshot/workletRef.ts +17 -18
  49. package/runtime/src/snapshot.ts +6 -4
  50. package/runtime/src/worklet/runOnBackground.ts +37 -15
  51. package/runtime/src/worklet/workletRef.ts +15 -12
  52. package/testing-library/dist/vitest-global-setup.js +7 -0
  53. package/transform/dist/wasm.cjs +1 -1
  54. package/transform/index.d.ts +443 -14
  55. package/types/react.d.ts +1 -1
  56. package/worklet-runtime/dist/dev.js +133 -33
  57. package/worklet-runtime/dist/dev.js.map +4 -4
  58. package/worklet-runtime/dist/main.js +133 -33
  59. package/worklet-runtime/dist/main.js.map +4 -4
  60. package/worklet-runtime/lib/api/element.d.ts +3 -3
  61. package/worklet-runtime/lib/api/element.js +3 -3
  62. package/worklet-runtime/lib/api/element.js.map +1 -1
  63. package/worklet-runtime/lib/api/lepusQuerySelector.js +1 -3
  64. package/worklet-runtime/lib/api/lepusQuerySelector.js.map +1 -1
  65. package/worklet-runtime/lib/api/lynxApi.js +10 -10
  66. package/worklet-runtime/lib/api/lynxApi.js.map +1 -1
  67. package/worklet-runtime/lib/bindings/bindings.d.ts +1 -1
  68. package/worklet-runtime/lib/bindings/bindings.js +21 -8
  69. package/worklet-runtime/lib/bindings/bindings.js.map +1 -1
  70. package/worklet-runtime/lib/bindings/events.d.ts +4 -1
  71. package/worklet-runtime/lib/bindings/events.js.map +1 -1
  72. package/worklet-runtime/lib/bindings/index.d.ts +1 -1
  73. package/worklet-runtime/lib/bindings/index.js +1 -1
  74. package/worklet-runtime/lib/bindings/index.js.map +1 -1
  75. package/worklet-runtime/lib/bindings/loadRuntime.js +0 -2
  76. package/worklet-runtime/lib/bindings/loadRuntime.js.map +1 -1
  77. package/worklet-runtime/lib/bindings/types.d.ts +15 -5
  78. package/worklet-runtime/lib/bindings/types.js +3 -0
  79. package/worklet-runtime/lib/bindings/types.js.map +1 -1
  80. package/worklet-runtime/lib/delayRunOnBackground.d.ts +12 -0
  81. package/worklet-runtime/lib/delayRunOnBackground.js +23 -0
  82. package/worklet-runtime/lib/delayRunOnBackground.js.map +1 -0
  83. package/worklet-runtime/lib/delayWorkletEvent.js +2 -2
  84. package/worklet-runtime/lib/delayWorkletEvent.js.map +1 -1
  85. package/worklet-runtime/lib/global.d.ts +6 -3
  86. package/worklet-runtime/lib/hydrate.d.ts +11 -0
  87. package/worklet-runtime/lib/hydrate.js +86 -0
  88. package/worklet-runtime/lib/hydrate.js.map +1 -0
  89. package/worklet-runtime/lib/jsFunctionLifecycle.js +2 -2
  90. package/worklet-runtime/lib/jsFunctionLifecycle.js.map +1 -1
  91. package/worklet-runtime/lib/listeners.js.map +1 -1
  92. package/worklet-runtime/lib/utils/profile.d.ts +1 -1
  93. package/worklet-runtime/lib/utils/version.js +1 -1
  94. package/worklet-runtime/lib/workletRef.d.ts +6 -3
  95. package/worklet-runtime/lib/workletRef.js +33 -6
  96. package/worklet-runtime/lib/workletRef.js.map +1 -1
  97. package/worklet-runtime/lib/workletRuntime.js +12 -14
  98. package/worklet-runtime/lib/workletRuntime.js.map +1 -1
@@ -2,8 +2,13 @@
2
2
  // Licensed under the Apache License Version 2.0 that can be found in the
3
3
  // LICENSE file in the root directory of this source tree.
4
4
 
5
- import type { JsFnHandle, RunWorkletCtxRetData, Worklet } from '@lynx-js/react/worklet-runtime/bindings';
6
- import { WorkletEvents } from '@lynx-js/react/worklet-runtime/bindings';
5
+ import type {
6
+ ClosureValueType,
7
+ JsFnHandle,
8
+ RunWorkletCtxRetData,
9
+ Worklet,
10
+ } from '@lynx-js/react/worklet-runtime/bindings';
11
+ import { WorkletEvents, delayRunOnBackground } from '@lynx-js/react/worklet-runtime/bindings';
7
12
 
8
13
  import { destroyTasks } from './destroy.js';
9
14
  import { WorkletExecIdMap } from './execMap.js';
@@ -44,7 +49,7 @@ function init() {
44
49
  function runJSFunction(event: RuntimeProxy.Event): void {
45
50
  'background only';
46
51
  const data = JSON.parse(event.data as string) as RunOnBackgroundData;
47
- const obj = execIdMap!.findJsFnHandle(data.obj._execId!, data.obj._jsFnId);
52
+ const obj = execIdMap!.findJsFnHandle(data.obj._execId!, data.obj._jsFnId!);
48
53
  const f = obj?._fn;
49
54
  if (!f) {
50
55
  throw new Error('runOnBackground: JS function not found: ' + JSON.stringify(data.obj));
@@ -104,22 +109,39 @@ function runOnBackground<R, Fn extends (...args: any[]) => R>(f: Fn): (...args:
104
109
  if (obj._error) {
105
110
  throw new Error(obj._error);
106
111
  }
107
- return async (...params: any[]): Promise<R> => {
112
+ return async (...params: ClosureValueType[]): Promise<R> => {
108
113
  return new Promise((resolve) => {
109
114
  const resolveId = onFunctionCall(resolve);
110
- lynx.getJSContext!().dispatchEvent({
111
- type: WorkletEvents.runOnBackground,
112
- data: JSON.stringify({
113
- obj: {
114
- _jsFnId: obj._jsFnId,
115
- _execId: obj._execId!,
116
- },
117
- params,
118
- resolveId,
119
- } as RunOnBackgroundData),
120
- });
115
+
116
+ if (obj._isFirstScreen) {
117
+ delayRunOnBackground(obj, (fnId: number, execId: number) => {
118
+ dispatchRunBackgroundFunctionEvent(fnId, params, execId, resolveId);
119
+ });
120
+ return;
121
+ }
122
+
123
+ dispatchRunBackgroundFunctionEvent(obj._jsFnId!, params, obj._execId!, resolveId);
121
124
  });
122
125
  };
123
126
  }
124
127
 
128
+ function dispatchRunBackgroundFunctionEvent(
129
+ fnId: number,
130
+ params: ClosureValueType[],
131
+ execId: number,
132
+ resolveId: number,
133
+ ): void {
134
+ lynx.getJSContext!().dispatchEvent({
135
+ type: WorkletEvents.runOnBackground,
136
+ data: JSON.stringify({
137
+ obj: {
138
+ _jsFnId: fnId,
139
+ _execId: execId,
140
+ },
141
+ params,
142
+ resolveId,
143
+ } as RunOnBackgroundData),
144
+ });
145
+ }
146
+
125
147
  export { registerWorkletCtx, runJSFunction, runOnBackground };
@@ -9,13 +9,19 @@ import { WorkletEvents } from '@lynx-js/react/worklet-runtime/bindings';
9
9
  import { addWorkletRefInitValue } from './workletRefPool.js';
10
10
  import { useMemo } from '../hooks/react.js';
11
11
 
12
- let lastId = 0;
12
+ // Split into two variables for testing purposes
13
+ let lastIdBG = 0;
14
+ let lastIdMT = 0;
15
+
16
+ export function clearWorkletRefLastIdForTesting(): void {
17
+ lastIdBG = lastIdMT = 0;
18
+ }
13
19
 
14
20
  abstract class WorkletRef<T> {
15
21
  /**
16
22
  * @internal
17
23
  */
18
- protected _id: number;
24
+ protected _wvid: number;
19
25
  /**
20
26
  * @internal
21
27
  */
@@ -33,16 +39,13 @@ abstract class WorkletRef<T> {
33
39
  * @internal
34
40
  */
35
41
  protected constructor(initValue: T, type: string) {
42
+ this._initValue = initValue;
43
+ this._type = type;
36
44
  if (__JS__) {
37
- this._id = ++lastId;
38
- this._initValue = initValue;
39
- this._type = type;
40
- addWorkletRefInitValue(this._id, initValue);
45
+ this._wvid = ++lastIdBG;
46
+ addWorkletRefInitValue(this._wvid, initValue);
41
47
  } else {
42
- // Out of the js thread, the `WorkletRef` class here is just a placeholder and should not be accessed directly.
43
- // The real WorkletRef will be generated by the worklet runtime.
44
- this._id = -1;
45
- this._type = '__LEPUS__';
48
+ this._wvid = --lastIdMT;
46
49
  }
47
50
  }
48
51
 
@@ -72,7 +75,7 @@ abstract class WorkletRef<T> {
72
75
  */
73
76
  toJSON(): { _wvid: WorkletRefImpl<T>['_wvid'] } {
74
77
  return {
75
- _wvid: this._id,
78
+ _wvid: this._wvid,
76
79
  };
77
80
  }
78
81
  }
@@ -88,7 +91,7 @@ export class MainThreadRef<T> extends WorkletRef<T> {
88
91
  constructor(initValue: T) {
89
92
  super(initValue, 'main-thread');
90
93
  if (__JS__) {
91
- const id = this._id;
94
+ const id = this._wvid;
92
95
  this._lifecycleObserver = lynx.getNativeApp().createJSObjectDestructionObserver?.(() => {
93
96
  lynx.getCoreContext?.().dispatchEvent({
94
97
  type: WorkletEvents.releaseWorkletRef,
@@ -86,6 +86,13 @@ globalThis.onInjectBackgroundThreadGlobals = (target)=>{
86
86
  globalThis.lynxCoreInject = target.lynxCoreInject;
87
87
  (0, __WEBPACK_EXTERNAL_MODULE__runtime_lib_lynx_tt_js_37d9bcb1__.injectTt)();
88
88
  globalThis.lynxCoreInject = oldLynxCoreInject;
89
+ target.lynx.requireModuleAsync = async (url, callback)=>{
90
+ try {
91
+ callback(null, await __vite_ssr_dynamic_import__(url));
92
+ } catch (err) {
93
+ callback(err, null);
94
+ }
95
+ };
89
96
  (0, __WEBPACK_EXTERNAL_MODULE__runtime_lib_lifecycle_patch_snapshotPatch_js_c1ebb33c__.deinitGlobalSnapshotPatch)();
90
97
  (0, __WEBPACK_EXTERNAL_MODULE__runtime_lib_lifecycle_patch_commit_js_9adc7328__.clearCommitTaskId)();
91
98
  };