@lynx-js/react 0.108.0 → 0.108.1

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 (51) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/package.json +3 -3
  3. package/refresh/.turbo/turbo-build.log +1 -1
  4. package/runtime/lib/lifecycle/patch/isMainThreadHydrationFinished.d.ts +2 -0
  5. package/runtime/lib/lifecycle/patch/isMainThreadHydrationFinished.js +9 -0
  6. package/runtime/lib/lifecycle/patch/isMainThreadHydrationFinished.js.map +1 -0
  7. package/runtime/lib/lifecycle/patch/updateMainThread.js +4 -3
  8. package/runtime/lib/lifecycle/patch/updateMainThread.js.map +1 -1
  9. package/runtime/lib/lifecycle/reload.js +2 -0
  10. package/runtime/lib/lifecycle/reload.js.map +1 -1
  11. package/runtime/lib/snapshot/ref.js.map +1 -1
  12. package/runtime/lib/snapshot/spread.d.ts +0 -6
  13. package/runtime/lib/snapshot/spread.js +4 -10
  14. package/runtime/lib/snapshot/spread.js.map +1 -1
  15. package/runtime/lib/snapshot/workletEvent.d.ts +2 -1
  16. package/runtime/lib/snapshot/workletEvent.js +1 -1
  17. package/runtime/lib/snapshot/workletEvent.js.map +1 -1
  18. package/runtime/lib/snapshot/workletRef.d.ts +4 -4
  19. package/runtime/lib/snapshot/workletRef.js +8 -6
  20. package/runtime/lib/snapshot/workletRef.js.map +1 -1
  21. package/runtime/lib/worklet/workletRef.d.ts +1 -0
  22. package/runtime/lib/worklet/workletRef.js +14 -12
  23. package/runtime/lib/worklet/workletRef.js.map +1 -1
  24. package/runtime/src/lifecycle/patch/isMainThreadHydrationFinished.ts +10 -0
  25. package/runtime/src/lifecycle/patch/updateMainThread.ts +4 -3
  26. package/runtime/src/lifecycle/reload.ts +2 -0
  27. package/runtime/src/snapshot/ref.ts +2 -2
  28. package/runtime/src/snapshot/spread.ts +15 -5
  29. package/runtime/src/snapshot/workletEvent.ts +3 -2
  30. package/runtime/src/snapshot/workletRef.ts +18 -19
  31. package/runtime/src/worklet/workletRef.ts +15 -12
  32. package/transform/dist/wasm.cjs +1 -1
  33. package/worklet-runtime/dist/dev.js +25 -13
  34. package/worklet-runtime/dist/dev.js.map +3 -3
  35. package/worklet-runtime/dist/main.js +25 -13
  36. package/worklet-runtime/dist/main.js.map +3 -3
  37. package/worklet-runtime/lib/bindings/bindings.d.ts +1 -1
  38. package/worklet-runtime/lib/bindings/bindings.js +2 -2
  39. package/worklet-runtime/lib/bindings/bindings.js.map +1 -1
  40. package/worklet-runtime/lib/bindings/index.d.ts +1 -1
  41. package/worklet-runtime/lib/bindings/index.js +1 -1
  42. package/worklet-runtime/lib/bindings/index.js.map +1 -1
  43. package/worklet-runtime/lib/bindings/types.d.ts +7 -3
  44. package/worklet-runtime/lib/bindings/types.js +3 -0
  45. package/worklet-runtime/lib/bindings/types.js.map +1 -1
  46. package/worklet-runtime/lib/ctxTrace.d.ts +8 -0
  47. package/worklet-runtime/lib/ctxTrace.js +13 -0
  48. package/worklet-runtime/lib/ctxTrace.js.map +1 -0
  49. package/worklet-runtime/lib/global.d.ts +3 -3
  50. package/worklet-runtime/lib/workletRuntime.js +11 -8
  51. package/worklet-runtime/lib/workletRuntime.js.map +1 -1
@@ -1,23 +1,19 @@
1
1
  // Copyright 2024 The Lynx Authors. All rights reserved.
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
- import {
5
- type Worklet,
6
- type WorkletRef,
7
- runWorkletCtx,
8
- updateWorkletRef as update,
9
- } from '@lynx-js/react/worklet-runtime/bindings';
4
+ import { runWorkletCtx, updateWorkletRef as update } from '@lynx-js/react/worklet-runtime/bindings';
5
+ import type { Element, Worklet, WorkletRefId, WorkletRefImpl } from '@lynx-js/react/worklet-runtime/bindings';
10
6
 
11
- import { SnapshotInstance } from '../snapshot.js';
7
+ import type { SnapshotInstance } from '../snapshot.js';
12
8
 
13
- function workletUnRef(value: Worklet | WorkletRef<unknown>): void {
14
- if ('_wvid' in value) {
15
- update(value as any, null);
9
+ function workletUnRef(value: Worklet | WorkletRefImpl<Element>): void {
10
+ if ('_wvid' in value && (value._wvid as WorkletRefId) > 0) {
11
+ update(value as WorkletRefImpl<Element>, null);
16
12
  } else if ('_wkltId' in value) {
17
13
  if (typeof value._unmount == 'function') {
18
- value._unmount();
14
+ (value._unmount as () => void)();
19
15
  } else {
20
- runWorkletCtx(value as any, [null]);
16
+ runWorkletCtx(value, [null]);
21
17
  }
22
18
  }
23
19
  }
@@ -25,7 +21,7 @@ function workletUnRef(value: Worklet | WorkletRef<unknown>): void {
25
21
  function updateWorkletRef(
26
22
  snapshot: SnapshotInstance,
27
23
  expIndex: number,
28
- oldValue: any,
24
+ oldValue: WorkletRefImpl<Element> | Worklet | undefined,
29
25
  elementIndex: number,
30
26
  _workletType: string,
31
27
  ): void {
@@ -38,15 +34,18 @@ function updateWorkletRef(
38
34
  snapshot.__worklet_ref_set?.delete(oldValue);
39
35
  }
40
36
 
41
- const value = snapshot.__values![expIndex];
37
+ const value = snapshot.__values![expIndex] as (WorkletRefImpl<Element> | Worklet | undefined);
42
38
  if (value === null || value === undefined) {
43
39
  // do nothing
44
40
  } else if (value._wvid) {
45
- update(value as any, snapshot.__elements[elementIndex]!);
46
- } else if (value._wkltId) {
47
- // @ts-ignore
48
- value._unmount = runWorkletCtx(value as any, [{ elementRefptr: snapshot.__elements[elementIndex]! }]);
49
- } else if (value._type === '__LEPUS__' || value._lepusWorkletHash) {
41
+ if ((value._wvid as WorkletRefId) > 0) {
42
+ update(value as WorkletRefImpl<Element>, snapshot.__elements[elementIndex]!);
43
+ }
44
+ } else if ((value as Worklet)._wkltId) {
45
+ (value as Worklet)._unmount = runWorkletCtx(value as Worklet, [{
46
+ elementRefptr: (snapshot.__elements[elementIndex]!) as any,
47
+ }]) as () => void;
48
+ } else if (value._type === '__LEPUS__' || (value as Worklet)._lepusWorkletHash) {
50
49
  // During the initial render, we will not update the WorkletRef because the background thread is not ready yet.
51
50
  } else {
52
51
  throw new Error('MainThreadRef: main-thread:ref must be of type MainThreadRef or main-thread function.');
@@ -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,