@lynx-js/react 0.105.1 → 0.106.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 (109) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/package.json +2 -2
  3. package/refresh/.turbo/turbo-build.log +1 -1
  4. package/refresh/package.json +1 -1
  5. package/runtime/lib/backgroundSnapshot.d.ts +1 -1
  6. package/runtime/lib/backgroundSnapshot.js +2 -2
  7. package/runtime/lib/backgroundSnapshot.js.map +1 -1
  8. package/runtime/lib/compat/initData.js +0 -2
  9. package/runtime/lib/compat/initData.js.map +1 -1
  10. package/runtime/lib/gesture/processGesture.js +3 -0
  11. package/runtime/lib/gesture/processGesture.js.map +1 -1
  12. package/runtime/lib/gesture/types.d.ts +2 -0
  13. package/runtime/lib/internal.d.ts +2 -1
  14. package/runtime/lib/internal.js +3 -2
  15. package/runtime/lib/internal.js.map +1 -1
  16. package/runtime/lib/lifecycle/destroy.js +3 -3
  17. package/runtime/lib/lifecycle/destroy.js.map +1 -1
  18. package/runtime/lib/lifecycle/{patchUpdate.d.ts → patch/commit.d.ts} +8 -6
  19. package/runtime/lib/lifecycle/patch/commit.js +167 -0
  20. package/runtime/lib/lifecycle/patch/commit.js.map +1 -0
  21. package/runtime/lib/lifecycle/patch/snapshotPatch.js.map +1 -0
  22. package/runtime/lib/{snapshotPatchApply.js → lifecycle/patch/snapshotPatchApply.js} +1 -4
  23. package/runtime/lib/lifecycle/patch/snapshotPatchApply.js.map +1 -0
  24. package/runtime/lib/lifecycle/patch/updateMainThread.d.ts +3 -0
  25. package/runtime/lib/lifecycle/patch/updateMainThread.js +56 -0
  26. package/runtime/lib/lifecycle/patch/updateMainThread.js.map +1 -0
  27. package/runtime/lib/lifecycle/reload.js +3 -4
  28. package/runtime/lib/lifecycle/reload.js.map +1 -1
  29. package/runtime/lib/lifecycle/render.d.ts +3 -1
  30. package/runtime/lib/lifecycle/render.js +9 -2
  31. package/runtime/lib/lifecycle/render.js.map +1 -1
  32. package/runtime/lib/list.d.ts +3 -0
  33. package/runtime/lib/list.js +11 -3
  34. package/runtime/lib/list.js.map +1 -1
  35. package/runtime/lib/lynx/calledByNative.js +40 -5
  36. package/runtime/lib/lynx/calledByNative.js.map +1 -1
  37. package/runtime/lib/lynx/component.js +3 -3
  38. package/runtime/lib/lynx/component.js.map +1 -1
  39. package/runtime/lib/lynx/dynamic-js.js +0 -1
  40. package/runtime/lib/lynx/dynamic-js.js.map +1 -1
  41. package/runtime/lib/lynx/env.js +1 -2
  42. package/runtime/lib/lynx/env.js.map +1 -1
  43. package/runtime/lib/lynx/lazy-bundle.js.map +1 -1
  44. package/runtime/lib/lynx/performance.js +1 -1
  45. package/runtime/lib/lynx/performance.js.map +1 -1
  46. package/runtime/lib/lynx/tt.d.ts +1 -0
  47. package/runtime/lib/lynx/tt.js +48 -7
  48. package/runtime/lib/lynx/tt.js.map +1 -1
  49. package/runtime/lib/lynx-api.d.ts +1 -1
  50. package/runtime/lib/lynx-api.js +2 -2
  51. package/runtime/lib/lynx-api.js.map +1 -1
  52. package/runtime/lib/lynx.d.ts +0 -1
  53. package/runtime/lib/lynx.js +4 -46
  54. package/runtime/lib/lynx.js.map +1 -1
  55. package/runtime/lib/opcodes.d.ts +6 -0
  56. package/runtime/lib/opcodes.js +70 -0
  57. package/runtime/lib/opcodes.js.map +1 -1
  58. package/runtime/lib/root.d.ts +1 -0
  59. package/runtime/lib/root.js.map +1 -1
  60. package/runtime/lib/snapshot/ref.d.ts +2 -2
  61. package/runtime/lib/snapshot/ref.js +6 -7
  62. package/runtime/lib/snapshot/ref.js.map +1 -1
  63. package/runtime/lib/snapshot.js +1 -1
  64. package/runtime/lib/snapshot.js.map +1 -1
  65. package/runtime/lib/worklet/functionCall.js.map +1 -1
  66. package/runtime/lib/worklet/hmr.js +1 -1
  67. package/runtime/lib/worklet/hmr.js.map +1 -1
  68. package/runtime/lib/worklet/runOnBackground.js.map +1 -1
  69. package/runtime/lib/worklet/workletRef.js +1 -1
  70. package/runtime/lib/worklet/workletRef.js.map +1 -1
  71. package/runtime/src/backgroundSnapshot.ts +3 -3
  72. package/runtime/src/compat/initData.ts +5 -6
  73. package/runtime/src/gesture/processGesture.ts +4 -0
  74. package/runtime/src/gesture/types.ts +2 -0
  75. package/runtime/src/internal.ts +4 -2
  76. package/runtime/src/lifecycle/destroy.ts +3 -4
  77. package/runtime/src/lifecycle/{patchUpdate.ts → patch/commit.ts} +94 -100
  78. package/runtime/src/{snapshotPatchApply.ts → lifecycle/patch/snapshotPatchApply.ts} +2 -2
  79. package/runtime/src/lifecycle/patch/updateMainThread.ts +71 -0
  80. package/runtime/src/lifecycle/reload.ts +3 -5
  81. package/runtime/src/lifecycle/render.ts +12 -3
  82. package/runtime/src/list.ts +12 -3
  83. package/runtime/src/lynx/calledByNative.ts +47 -4
  84. package/runtime/src/lynx/component.ts +3 -3
  85. package/runtime/src/lynx/dynamic-js.ts +2 -3
  86. package/runtime/src/lynx/env.ts +2 -2
  87. package/runtime/src/lynx/lazy-bundle.ts +1 -1
  88. package/runtime/src/lynx/performance.ts +3 -2
  89. package/runtime/src/lynx/tt.ts +58 -9
  90. package/runtime/src/lynx-api.ts +3 -3
  91. package/runtime/src/lynx.ts +5 -55
  92. package/runtime/src/opcodes.ts +90 -0
  93. package/runtime/src/root.ts +1 -1
  94. package/runtime/src/snapshot/ref.ts +6 -9
  95. package/runtime/src/snapshot.ts +1 -1
  96. package/runtime/src/worklet/functionCall.ts +1 -1
  97. package/runtime/src/worklet/hmr.ts +1 -1
  98. package/runtime/src/worklet/runOnBackground.ts +4 -4
  99. package/runtime/src/worklet/workletRef.ts +1 -1
  100. package/transform/dist/wasm.cjs +1 -1
  101. package/types/react.docs.d.ts +1 -1
  102. package/runtime/lib/lifecycle/patchUpdate.js +0 -179
  103. package/runtime/lib/lifecycle/patchUpdate.js.map +0 -1
  104. package/runtime/lib/snapshotPatch.js.map +0 -1
  105. package/runtime/lib/snapshotPatchApply.js.map +0 -1
  106. /package/runtime/lib/{snapshotPatch.d.ts → lifecycle/patch/snapshotPatch.d.ts} +0 -0
  107. /package/runtime/lib/{snapshotPatch.js → lifecycle/patch/snapshotPatch.js} +0 -0
  108. /package/runtime/lib/{snapshotPatchApply.d.ts → lifecycle/patch/snapshotPatchApply.d.ts} +0 -0
  109. /package/runtime/src/{snapshotPatch.ts → lifecycle/patch/snapshotPatch.ts} +0 -0
@@ -1,21 +1,71 @@
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 { render } from 'preact';
4
+ import { options } from 'preact';
5
+ import type { VNode } from 'preact';
5
6
 
6
7
  import { LifecycleConstant, NativeUpdateDataType } from '../lifecycleConstant.js';
7
8
  import { PerformanceTimingKeys, beginPipeline, markTiming } from './performance.js';
8
9
  import { BackgroundSnapshotInstance, hydrate } from '../backgroundSnapshot.js';
9
10
  import { destroyBackground } from '../lifecycle/destroy.js';
10
- import { commitPatchUpdate, genCommitTaskId, globalCommitTaskMap } from '../lifecycle/patchUpdate.js';
11
+ import { commitPatchUpdate, genCommitTaskId, globalCommitTaskMap } from '../lifecycle/patch/commit.js';
11
12
  import { reloadBackground } from '../lifecycle/reload.js';
12
- import { runWithForce } from '../lynx.js';
13
- import { CHILDREN } from '../renderToOpcodes/constants.js';
13
+ import { renderBackground } from '../lifecycle/render.js';
14
+ import { CHILDREN, COMPONENT, DIFF, DIFFED, FORCE } from '../renderToOpcodes/constants.js';
14
15
  import { __root } from '../root.js';
15
16
  import { globalRefsToSet, updateBackgroundRefs } from '../snapshot/ref.js';
16
17
  import { backgroundSnapshotInstanceManager } from '../snapshot.js';
17
18
  import { destroyWorklet } from '../worklet/destroy.js';
18
19
 
20
+ export function runWithForce(cb: () => void): void {
21
+ // save vnode and its `_component` in WeakMap
22
+ const m = new WeakMap<VNode, any>();
23
+
24
+ const oldDiff = options[DIFF];
25
+
26
+ options[DIFF] = (vnode: VNode) => {
27
+ if (oldDiff) {
28
+ oldDiff(vnode);
29
+ }
30
+
31
+ // when `options[DIFF]` is called, a newVnode is passed in
32
+ // so its `vnode[COMPONENT]` should be null,
33
+ // but it will be set later
34
+ Object.defineProperty(vnode, COMPONENT, {
35
+ configurable: true,
36
+ set(c) {
37
+ m.set(vnode, c);
38
+ if (c) {
39
+ c[FORCE] = true;
40
+ }
41
+ },
42
+ get() {
43
+ return m.get(vnode);
44
+ },
45
+ });
46
+ };
47
+
48
+ const oldDiffed = options[DIFFED];
49
+
50
+ options[DIFFED] = (vnode: VNode) => {
51
+ if (oldDiffed) {
52
+ oldDiffed(vnode);
53
+ }
54
+
55
+ // delete is a reverse operation of previous `Object.defineProperty`
56
+ delete vnode[COMPONENT];
57
+ // restore
58
+ vnode[COMPONENT] = m.get(vnode);
59
+ };
60
+
61
+ try {
62
+ cb();
63
+ } finally {
64
+ options[DIFF] = oldDiff as (vnode: VNode) => void;
65
+ options[DIFFED] = oldDiffed as (vnode: VNode) => void;
66
+ }
67
+ }
68
+
19
69
  function injectTt(): void {
20
70
  // @ts-ignore
21
71
  const tt = lynxCoreInject.tt;
@@ -76,7 +126,7 @@ async function OnLifecycleEvent([type, data]: [string, any]) {
76
126
  try {
77
127
  publishEvent([idStr, ...rest].join(':'), data);
78
128
  } catch (e) {
79
- lynx.reportError(e);
129
+ lynx.reportError(e as Error);
80
130
  }
81
131
  });
82
132
  delayedEvents.length = 0;
@@ -94,14 +144,13 @@ async function OnLifecycleEvent([type, data]: [string, any]) {
94
144
  if (__PROFILE__) {
95
145
  console.profileEnd();
96
146
  }
97
- markTiming(PerformanceTimingKeys.pack_changes_start);
98
147
  // console.debug("********** After hydration:");
99
148
  // printSnapshotInstance(__root as BackgroundSnapshotInstance);
100
149
  if (__PROFILE__) {
101
150
  console.profile('commitChanges');
102
151
  }
103
152
  const commitTaskId = genCommitTaskId();
104
- await commitPatchUpdate({ snapshotPatch }, { commitTaskId, isHydration: true });
153
+ await commitPatchUpdate({ patchList: [{ snapshotPatch, id: commitTaskId }] }, { isHydration: true });
105
154
  updateBackgroundRefs(commitTaskId);
106
155
  globalCommitTaskMap.forEach((commitTask, id) => {
107
156
  if (id > commitTaskId) {
@@ -139,7 +188,7 @@ function flushDelayedLifecycleEvents(): void {
139
188
  try {
140
189
  OnLifecycleEvent(e);
141
190
  } catch (e) {
142
- lynx.reportError(e);
191
+ lynx.reportError(e as Error);
143
192
  }
144
193
  });
145
194
  delayedLifecycleEvents.length = 0;
@@ -183,7 +232,7 @@ function updateGlobalProps(newData: Record<string, any>): void {
183
232
  // This is already done because updateFromRoot will consume all dirty flags marked by
184
233
  // the setState, and setState's flush will be a noop. No extra diffs will be needed.
185
234
  Promise.resolve().then(() => {
186
- runWithForce(() => render(__root.__jsx, __root as any));
235
+ runWithForce(() => renderBackground(__root.__jsx, __root as any));
187
236
  });
188
237
  lynxCoreInject.tt.GlobalEventEmitter.emit('onGlobalPropsChanged');
189
238
  }
@@ -1,13 +1,13 @@
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 { render } from 'preact';
5
4
  import { createContext, createElement } from 'preact/compat';
6
5
  import { useState } from 'preact/hooks';
7
6
  import type { Consumer, FC, ReactNode } from 'react';
8
7
 
9
8
  import { factory, withInitDataInState } from './compat/initData.js';
10
9
  import { useLynxGlobalEventListener } from './hooks/useLynxGlobalEventListener.js';
10
+ import { renderBackground } from './lifecycle/render.js';
11
11
  import { LifecycleConstant } from './lifecycleConstant.js';
12
12
  import { flushDelayedLifecycleEvents } from './lynx/tt.js';
13
13
  import { __root } from './root.js';
@@ -86,7 +86,7 @@ export const root: Root = {
86
86
  __root.__jsx = jsx;
87
87
  } else {
88
88
  __root.__jsx = jsx;
89
- render(jsx, __root as any);
89
+ renderBackground(jsx, __root as any);
90
90
  if (__FIRST_SCREEN_SYNC_TIMING__ === 'immediately') {}
91
91
  else {
92
92
  lynx.getNativeApp().callLepusMethod(LifecycleConstant.jsReady, {});
@@ -366,7 +366,7 @@ export interface Lynx {
366
366
  * ```
367
367
  * @public
368
368
  */
369
- registerDataProcessors: (dataProcessorDefinition: DataProcessorDefinition) => void;
369
+ registerDataProcessors: (dataProcessorDefinition?: DataProcessorDefinition) => void;
370
370
  }
371
371
 
372
372
  export { runOnMainThread } from './worklet/runOnMainThread.js';
@@ -2,81 +2,31 @@
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
  import { options } from 'preact';
5
- import type { VNode } from 'preact';
6
5
  // to make sure preact's hooks to register earlier than ours
7
6
  import './hooks/react.js';
8
7
 
9
8
  import { initProfileHook } from './debug/profile.js';
10
9
  import { document, setupBackgroundDocument } from './document.js';
11
10
  import { initDelayUnmount } from './lifecycle/delayUnmount.js';
12
- import { injectUpdatePatch, replaceCommitHook, replaceRequestAnimationFrame } from './lifecycle/patchUpdate.js';
11
+ import { replaceCommitHook, replaceRequestAnimationFrame } from './lifecycle/patch/commit.js';
12
+ import { injectUpdateMainThread } from './lifecycle/patch/updateMainThread.js';
13
13
  import { injectCalledByNative } from './lynx/calledByNative.js';
14
14
  import { setupLynxEnv } from './lynx/env.js';
15
15
  import { injectLepusMethods } from './lynx/injectLepusMethods.js';
16
16
  import { initTimingAPI } from './lynx/performance.js';
17
17
  import { injectTt } from './lynx/tt.js';
18
- import { COMPONENT, DIFF, DIFFED, FORCE } from './renderToOpcodes/constants.js';
19
18
 
20
19
  // @ts-expect-error Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature
21
20
  if (__LEPUS__ && typeof globalThis.processEvalResult === 'undefined') {
22
21
  // @ts-expect-error Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature
23
- globalThis.processEvalResult = <T>(result: (schema: string) => T, schema: string) => {
24
- return result(schema);
22
+ globalThis.processEvalResult = <T>(result: ((schema: string) => T) | undefined, schema: string) => {
23
+ return result?.(schema);
25
24
  };
26
25
  }
27
26
 
28
- export function runWithForce(cb: () => void): void {
29
- // save vnode and its `_component` in WeakMap
30
- const m = new WeakMap<VNode, any>();
31
-
32
- const oldDiff = options[DIFF];
33
-
34
- options[DIFF] = (vnode: VNode) => {
35
- if (oldDiff) {
36
- oldDiff(vnode);
37
- }
38
-
39
- // when `options[DIFF]` is called, a newVnode is passed in
40
- // so its `vnode[COMPONENT]` should be null,
41
- // but it will be set later
42
- Object.defineProperty(vnode, COMPONENT, {
43
- configurable: true,
44
- set(c) {
45
- m.set(vnode, c);
46
- if (c) {
47
- c[FORCE] = true;
48
- }
49
- },
50
- get() {
51
- return m.get(vnode);
52
- },
53
- });
54
- };
55
-
56
- const oldDiffed = options[DIFFED];
57
-
58
- options[DIFFED] = (vnode: VNode) => {
59
- if (oldDiffed) {
60
- oldDiffed(vnode);
61
- }
62
-
63
- // delete is a reverse operation of previous `Object.defineProperty`
64
- delete vnode[COMPONENT];
65
- // restore
66
- vnode[COMPONENT] = m.get(vnode);
67
- };
68
-
69
- try {
70
- cb();
71
- } finally {
72
- options[DIFF] = oldDiff as (vnode: VNode) => void;
73
- options[DIFFED] = oldDiffed as (vnode: VNode) => void;
74
- }
75
- }
76
-
77
27
  if (__LEPUS__) {
78
28
  injectCalledByNative();
79
- injectUpdatePatch();
29
+ injectUpdateMainThread();
80
30
  if (__DEV__) {
81
31
  injectLepusMethods();
82
32
  }
@@ -1,6 +1,7 @@
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 { componentAtIndexFactory, enqueueComponentFactory, gRecycleMap, gSignMap } from './list.js';
4
5
  import { CHILDREN } from './renderToOpcodes/constants.js';
5
6
  import { SnapshotInstance } from './snapshot.js';
6
7
 
@@ -11,6 +12,90 @@ const enum Opcode {
11
12
  Text,
12
13
  }
13
14
 
15
+ interface SSRFiberElement {
16
+ ssrID: string;
17
+ }
18
+ export type SSRSnapshotInstance = [string, number, SSRFiberElement[]];
19
+
20
+ export function ssrHydrateByOpcodes(
21
+ opcodes: any[],
22
+ into: SnapshotInstance,
23
+ refMap?: Record<string, FiberElement>,
24
+ ): void {
25
+ let top: SnapshotInstance & { __pendingElements?: SSRFiberElement[] } = into;
26
+ const stack: SnapshotInstance[] = [into];
27
+ for (let i = 0; i < opcodes.length;) {
28
+ const opcode = opcodes[i];
29
+ switch (opcode) {
30
+ case Opcode.Begin: {
31
+ const p = top;
32
+ const [type, __id, elements] = opcodes[i + 1] as SSRSnapshotInstance;
33
+ top = new SnapshotInstance(type, __id);
34
+ top.__pendingElements = elements;
35
+ p.insertBefore(top);
36
+ stack.push(top);
37
+
38
+ i += 2;
39
+ break;
40
+ }
41
+ case Opcode.End: {
42
+ // @ts-ignore
43
+ top[CHILDREN] = undefined;
44
+
45
+ top.__elements = top.__pendingElements!.map(({ ssrID }) => refMap![ssrID]!);
46
+ top.__element_root = top.__elements[0];
47
+ delete top.__pendingElements;
48
+
49
+ if (top.__snapshot_def.isListHolder) {
50
+ const listElement = top.__element_root!;
51
+ const listElementUniqueID = __GetElementUniqueID(listElement);
52
+ const signMap = gSignMap[listElementUniqueID] = new Map();
53
+ gRecycleMap[listElementUniqueID] = new Map();
54
+ const enqueueFunc = enqueueComponentFactory();
55
+ const componentAtIndex = componentAtIndexFactory(top.childNodes);
56
+ for (const child of top.childNodes) {
57
+ if (child.__element_root) {
58
+ const childElementUniqueID = __GetElementUniqueID(child.__element_root);
59
+ signMap.set(childElementUniqueID, child);
60
+ enqueueFunc(
61
+ listElement,
62
+ listElementUniqueID,
63
+ childElementUniqueID,
64
+ );
65
+ }
66
+ }
67
+ __UpdateListCallbacks(listElement, componentAtIndex, enqueueFunc);
68
+ }
69
+
70
+ stack.pop();
71
+ const p = stack[stack.length - 1];
72
+ top = p!;
73
+
74
+ i += 1;
75
+ break;
76
+ }
77
+ case Opcode.Attr: {
78
+ const key = opcodes[i + 1];
79
+ const value = opcodes[i + 2];
80
+ top.setAttribute(key, value);
81
+
82
+ i += 3;
83
+ break;
84
+ }
85
+ case Opcode.Text: {
86
+ const [[type, __id, elements], text] = opcodes[i + 1] as [SSRSnapshotInstance, string];
87
+ const s = new SnapshotInstance(type, __id);
88
+ s.setAttribute(0, text);
89
+ top.insertBefore(s);
90
+ s.__elements = elements.map(({ ssrID }) => refMap![ssrID]!);
91
+ s.__element_root = s.__elements[0];
92
+ i += 2;
93
+ break;
94
+ }
95
+ }
96
+ }
97
+ }
98
+
14
99
  export function renderOpcodesInto(opcodes: any[], into: SnapshotInstance): void {
15
100
  let top: SnapshotInstance = into;
16
101
  const stack: SnapshotInstance[] = [into];
@@ -24,6 +109,7 @@ export function renderOpcodesInto(opcodes: any[], into: SnapshotInstance): void
24
109
  if (top.__parent) {
25
110
  // already inserted
26
111
  top = new SnapshotInstance(top.type);
112
+ opcodes[i + 1] = top;
27
113
  }
28
114
  p.insertBefore(top);
29
115
  stack.push(top);
@@ -53,6 +139,10 @@ export function renderOpcodesInto(opcodes: any[], into: SnapshotInstance): void
53
139
  case Opcode.Text: {
54
140
  const text = opcodes[i + 1];
55
141
  const s = new SnapshotInstance(null as unknown as string);
142
+ if (__ENABLE_SSR__) {
143
+ // We need store the just created SnapshotInstance, or it will be lost when we leave the function
144
+ opcodes[i + 1] = [s, text];
145
+ }
56
146
  s.setAttribute(0, text);
57
147
  top.insertBefore(s);
58
148
 
@@ -4,7 +4,7 @@
4
4
  import { BackgroundSnapshotInstance } from './backgroundSnapshot.js';
5
5
  import { SnapshotInstance } from './snapshot.js';
6
6
 
7
- let __root: (SnapshotInstance | BackgroundSnapshotInstance) & { __jsx?: React.ReactNode };
7
+ let __root: (SnapshotInstance | BackgroundSnapshotInstance) & { __jsx?: React.ReactNode; __opcodes?: any[] };
8
8
 
9
9
  function setRoot(root: typeof __root): void {
10
10
  __root = root;
@@ -3,13 +3,13 @@
3
3
  // LICENSE file in the root directory of this source tree.
4
4
  import type { Worklet, WorkletRef } from '@lynx-js/react/worklet-runtime/bindings';
5
5
 
6
- import { nextCommitTaskId } from '../lifecycle/patchUpdate.js';
6
+ import { nextCommitTaskId } from '../lifecycle/patch/commit.js';
7
7
  import { SnapshotInstance, backgroundSnapshotInstanceManager } from '../snapshot.js';
8
8
  import { workletUnRef } from './workletRef.js';
9
9
 
10
10
  let globalRefPatch: Record<string, number | null> = {};
11
- let globalRefsToRemove: Map</* commitId */ number, Map</* sign */ string, /* ref */ any>> = /* @__PURE__ */ new Map();
12
- let globalRefsToSet: Map</* commitId */ number, Record<string, number>> = /* @__PURE__ */ new Map();
11
+ const globalRefsToRemove: Map</* commitId */ number, Map</* sign */ string, /* ref */ any>> = /* @__PURE__ */ new Map();
12
+ const globalRefsToSet: Map</* commitId */ number, Record<string, number>> = /* @__PURE__ */ new Map();
13
13
  let nextRefId = 1;
14
14
 
15
15
  function unref(snapshot: SnapshotInstance, recursive: boolean): void {
@@ -35,7 +35,7 @@ function unref(snapshot: SnapshotInstance, recursive: boolean): void {
35
35
  function applyRef(ref: any, value: any) {
36
36
  // TODO: ref: exceptions thrown in user functions should be able to be caught by an Error Boundary
37
37
  if (typeof ref == 'function') {
38
- let hasRefUnmount = typeof ref._unmount == 'function';
38
+ const hasRefUnmount = typeof ref._unmount == 'function';
39
39
  if (hasRefUnmount) {
40
40
  // @ts-ignore TS doesn't like moving narrowing checks into variables
41
41
  ref._unmount();
@@ -54,7 +54,7 @@ function updateBackgroundRefs(commitId: number): void {
54
54
  const oldRefMap = globalRefsToRemove.get(commitId);
55
55
  if (oldRefMap) {
56
56
  globalRefsToRemove.delete(commitId);
57
- for (let ref of oldRefMap.values()) {
57
+ for (const ref of oldRefMap.values()) {
58
58
  applyRef(ref, null);
59
59
  }
60
60
  }
@@ -65,10 +65,7 @@ function updateBackgroundRefs(commitId: number): void {
65
65
  const ref = backgroundSnapshotInstanceManager.getValueBySign(sign);
66
66
  if (ref) {
67
67
  // TODO: ref: support __REF_FIRE_IMMEDIATELY__
68
- const v = newRefMap[sign]
69
- && lynx.createSelectorQuery().selectUniqueID(
70
- newRefMap[sign],
71
- );
68
+ const v = newRefMap[sign] && lynx.createSelectorQuery().selectUniqueID(newRefMap[sign]);
72
69
  applyRef(ref, v);
73
70
  }
74
71
  }
@@ -6,7 +6,7 @@ import type { Worklet, WorkletRefImpl } from '@lynx-js/react/worklet-runtime/bin
6
6
  import type { BackgroundSnapshotInstance } from './backgroundSnapshot.js';
7
7
  import { ListUpdateInfoRecording, __pendingListUpdates, snapshotDestroyList } from './list.js';
8
8
  import { unref } from './snapshot/ref.js';
9
- import { SnapshotOperation, __globalSnapshotPatch } from './snapshotPatch.js';
9
+ import { SnapshotOperation, __globalSnapshotPatch } from './lifecycle/patch/snapshotPatch.js';
10
10
  import { isDirectOrDeepEqual } from './utils.js';
11
11
 
12
12
  export const enum DynamicPartType {
@@ -11,7 +11,7 @@ import { IndexMap } from './indexMap.js';
11
11
  let resolveMap: IndexMap<(value: any) => void> | undefined;
12
12
 
13
13
  function initReturnValueListener(): void {
14
- const context: RuntimeProxy = __JS__ ? lynx.getCoreContext() : lynx.getJSContext();
14
+ const context: RuntimeProxy = __JS__ ? lynx.getCoreContext!() : lynx.getJSContext!();
15
15
 
16
16
  resolveMap = new IndexMap();
17
17
  context.addEventListener(WorkletEvents.FunctionCallRet, onFunctionCallRet);
@@ -1,7 +1,7 @@
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 { SnapshotOperation, __globalSnapshotPatch } from '../snapshotPatch.js';
4
+ import { SnapshotOperation, __globalSnapshotPatch } from '../lifecycle/patch/snapshotPatch.js';
5
5
 
6
6
  const workletHashSet: Set<string> = /* @__PURE__ */ new Set();
7
7
 
@@ -28,12 +28,12 @@ function init() {
28
28
  }
29
29
 
30
30
  execIdMap = new WorkletExecIdMap();
31
- lynx.getCoreContext().addEventListener(WorkletEvents.runOnBackground, runJSFunction);
32
- lynx.getCoreContext().addEventListener(WorkletEvents.releaseBackgroundWorkletCtx, releaseBackgroundWorkletCtx);
31
+ lynx.getCoreContext!().addEventListener(WorkletEvents.runOnBackground, runJSFunction);
32
+ lynx.getCoreContext!().addEventListener(WorkletEvents.releaseBackgroundWorkletCtx, releaseBackgroundWorkletCtx);
33
33
 
34
34
  destroyTasks.push(() => {
35
- lynx.getCoreContext().removeEventListener(WorkletEvents.runOnBackground, runJSFunction);
36
- lynx.getCoreContext().removeEventListener(WorkletEvents.releaseBackgroundWorkletCtx, releaseBackgroundWorkletCtx);
35
+ lynx.getCoreContext!().removeEventListener(WorkletEvents.runOnBackground, runJSFunction);
36
+ lynx.getCoreContext!().removeEventListener(WorkletEvents.releaseBackgroundWorkletCtx, releaseBackgroundWorkletCtx);
37
37
  execIdMap = undefined;
38
38
  });
39
39
  }
@@ -89,7 +89,7 @@ export class MainThreadRef<T> extends WorkletRef<T> {
89
89
  super(initValue, 'main-thread');
90
90
  if (__JS__) {
91
91
  const id = this._id;
92
- this._lifecycleObserver = lynx.getNativeApp().createJSObjectDestructionObserver(() => {
92
+ this._lifecycleObserver = lynx.getNativeApp().createJSObjectDestructionObserver?.(() => {
93
93
  lynx.getCoreContext?.().dispatchEvent({
94
94
  type: WorkletEvents.releaseWorkletRef,
95
95
  data: {