@lynx-js/react 0.105.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 (337) hide show
  1. package/CHANGELOG.md +250 -0
  2. package/LICENSE +202 -0
  3. package/README.md +20 -0
  4. package/components/lib/Page.d.ts +3 -0
  5. package/components/lib/Page.js +56 -0
  6. package/components/lib/Page.js.map +1 -0
  7. package/components/lib/index.d.ts +1 -0
  8. package/components/lib/index.js +5 -0
  9. package/components/lib/index.js.map +1 -0
  10. package/components/src/Page.ts +75 -0
  11. package/components/src/index.ts +5 -0
  12. package/package.json +162 -0
  13. package/refresh/.gitignore +1 -0
  14. package/refresh/.npmignore +3 -0
  15. package/refresh/.turbo/turbo-build.log +8 -0
  16. package/refresh/dist/index.js +311 -0
  17. package/refresh/package.json +21 -0
  18. package/refresh/src/index.ts +6 -0
  19. package/refresh/src/prefresh.d.ts +11 -0
  20. package/refresh/tsconfig.json +4 -0
  21. package/refresh/turbo.json +15 -0
  22. package/runtime/jsx-dev-runtime/index.d.ts +5 -0
  23. package/runtime/jsx-dev-runtime/index.js +4 -0
  24. package/runtime/jsx-runtime/index.d.ts +4 -0
  25. package/runtime/jsx-runtime/index.js +4 -0
  26. package/runtime/lazy/import.d.ts +1 -0
  27. package/runtime/lazy/import.js +55 -0
  28. package/runtime/lazy/internal.js +39 -0
  29. package/runtime/lazy/jsx-dev-runtime.js +12 -0
  30. package/runtime/lazy/jsx-runtime.js +12 -0
  31. package/runtime/lazy/legacy-react-runtime.js +20 -0
  32. package/runtime/lazy/react-lepus.js +9 -0
  33. package/runtime/lazy/react.js +46 -0
  34. package/runtime/lazy/target.js +11 -0
  35. package/runtime/lepus/index.d.ts +4 -0
  36. package/runtime/lepus/index.js +43 -0
  37. package/runtime/lepus/jsx-dev-runtime/index.js +4 -0
  38. package/runtime/lepus/jsx-runtime/index.js +66 -0
  39. package/runtime/lib/backgroundSnapshot.d.ts +23 -0
  40. package/runtime/lib/backgroundSnapshot.js +348 -0
  41. package/runtime/lib/backgroundSnapshot.js.map +1 -0
  42. package/runtime/lib/compat/componentIs.d.ts +4 -0
  43. package/runtime/lib/compat/componentIs.js +20 -0
  44. package/runtime/lib/compat/componentIs.js.map +1 -0
  45. package/runtime/lib/compat/initData.d.ts +43 -0
  46. package/runtime/lib/compat/initData.js +93 -0
  47. package/runtime/lib/compat/initData.js.map +1 -0
  48. package/runtime/lib/compat/lynxComponent.d.ts +5 -0
  49. package/runtime/lib/compat/lynxComponent.js +71 -0
  50. package/runtime/lib/compat/lynxComponent.js.map +1 -0
  51. package/runtime/lib/debug/debug.d.ts +2 -0
  52. package/runtime/lib/debug/debug.js +9 -0
  53. package/runtime/lib/debug/debug.js.map +1 -0
  54. package/runtime/lib/debug/printSnapshot.d.ts +3 -0
  55. package/runtime/lib/debug/printSnapshot.js +21 -0
  56. package/runtime/lib/debug/printSnapshot.js.map +1 -0
  57. package/runtime/lib/debug/profile.d.ts +1 -0
  58. package/runtime/lib/debug/profile.js +51 -0
  59. package/runtime/lib/debug/profile.js.map +1 -0
  60. package/runtime/lib/document.d.ts +4 -0
  61. package/runtime/lib/document.js +49 -0
  62. package/runtime/lib/document.js.map +1 -0
  63. package/runtime/lib/gesture/processGesture.d.ts +4 -0
  64. package/runtime/lib/gesture/processGesture.js +48 -0
  65. package/runtime/lib/gesture/processGesture.js.map +1 -0
  66. package/runtime/lib/gesture/processGestureBagkround.d.ts +2 -0
  67. package/runtime/lib/gesture/processGestureBagkround.js +16 -0
  68. package/runtime/lib/gesture/processGestureBagkround.js.map +1 -0
  69. package/runtime/lib/gesture/types.d.ts +34 -0
  70. package/runtime/lib/gesture/types.js +13 -0
  71. package/runtime/lib/gesture/types.js.map +1 -0
  72. package/runtime/lib/hooks/react.d.ts +26 -0
  73. package/runtime/lib/hooks/react.js +35 -0
  74. package/runtime/lib/hooks/react.js.map +1 -0
  75. package/runtime/lib/hooks/useLynxGlobalEventListener.d.ts +29 -0
  76. package/runtime/lib/hooks/useLynxGlobalEventListener.js +53 -0
  77. package/runtime/lib/hooks/useLynxGlobalEventListener.js.map +1 -0
  78. package/runtime/lib/hydrate.d.ts +18 -0
  79. package/runtime/lib/hydrate.js +271 -0
  80. package/runtime/lib/hydrate.js.map +1 -0
  81. package/runtime/lib/index.d.ts +9 -0
  82. package/runtime/lib/index.js +46 -0
  83. package/runtime/lib/index.js.map +1 -0
  84. package/runtime/lib/internal.d.ts +26 -0
  85. package/runtime/lib/internal.js +41 -0
  86. package/runtime/lib/internal.js.map +1 -0
  87. package/runtime/lib/legacy-react-runtime/index.d.ts +7 -0
  88. package/runtime/lib/legacy-react-runtime/index.js +34 -0
  89. package/runtime/lib/legacy-react-runtime/index.js.map +1 -0
  90. package/runtime/lib/lifecycle/delayUnmount.d.ts +8 -0
  91. package/runtime/lib/lifecycle/delayUnmount.js +68 -0
  92. package/runtime/lib/lifecycle/delayUnmount.js.map +1 -0
  93. package/runtime/lib/lifecycle/destroy.d.ts +2 -0
  94. package/runtime/lib/lifecycle/destroy.js +21 -0
  95. package/runtime/lib/lifecycle/destroy.js.map +1 -0
  96. package/runtime/lib/lifecycle/pass.d.ts +3 -0
  97. package/runtime/lib/lifecycle/pass.js +12 -0
  98. package/runtime/lib/lifecycle/pass.js.map +1 -0
  99. package/runtime/lib/lifecycle/patchUpdate.d.ts +23 -0
  100. package/runtime/lib/lifecycle/patchUpdate.js +179 -0
  101. package/runtime/lib/lifecycle/patchUpdate.js.map +1 -0
  102. package/runtime/lib/lifecycle/reload.d.ts +3 -0
  103. package/runtime/lib/lifecycle/reload.js +63 -0
  104. package/runtime/lib/lifecycle/reload.js.map +1 -0
  105. package/runtime/lib/lifecycle/render.d.ts +2 -0
  106. package/runtime/lib/lifecycle/render.js +44 -0
  107. package/runtime/lib/lifecycle/render.js.map +1 -0
  108. package/runtime/lib/lifecycleConstant.d.ts +12 -0
  109. package/runtime/lib/lifecycleConstant.js +17 -0
  110. package/runtime/lib/lifecycleConstant.js.map +1 -0
  111. package/runtime/lib/list.d.ts +40 -0
  112. package/runtime/lib/list.js +305 -0
  113. package/runtime/lib/list.js.map +1 -0
  114. package/runtime/lib/lynx/calledByNative.d.ts +2 -0
  115. package/runtime/lib/lynx/calledByNative.js +113 -0
  116. package/runtime/lib/lynx/calledByNative.js.map +1 -0
  117. package/runtime/lib/lynx/component.d.ts +1 -0
  118. package/runtime/lib/lynx/component.js +69 -0
  119. package/runtime/lib/lynx/component.js.map +1 -0
  120. package/runtime/lib/lynx/dynamic-js.d.ts +2 -0
  121. package/runtime/lib/lynx/dynamic-js.js +31 -0
  122. package/runtime/lib/lynx/dynamic-js.js.map +1 -0
  123. package/runtime/lib/lynx/env.d.ts +1 -0
  124. package/runtime/lib/lynx/env.js +92 -0
  125. package/runtime/lib/lynx/env.js.map +1 -0
  126. package/runtime/lib/lynx/injectLepusMethods.d.ts +2 -0
  127. package/runtime/lib/lynx/injectLepusMethods.js +50 -0
  128. package/runtime/lib/lynx/injectLepusMethods.js.map +1 -0
  129. package/runtime/lib/lynx/lazy-bundle.d.ts +9 -0
  130. package/runtime/lib/lynx/lazy-bundle.js +119 -0
  131. package/runtime/lib/lynx/lazy-bundle.js.map +1 -0
  132. package/runtime/lib/lynx/performance.d.ts +29 -0
  133. package/runtime/lib/lynx/performance.js +100 -0
  134. package/runtime/lib/lynx/performance.js.map +1 -0
  135. package/runtime/lib/lynx/tt.d.ts +4 -0
  136. package/runtime/lib/lynx/tt.js +193 -0
  137. package/runtime/lib/lynx/tt.js.map +1 -0
  138. package/runtime/lib/lynx-api.d.ts +311 -0
  139. package/runtime/lib/lynx-api.js +116 -0
  140. package/runtime/lib/lynx-api.js.map +1 -0
  141. package/runtime/lib/lynx.d.ts +2 -0
  142. package/runtime/lib/lynx.js +91 -0
  143. package/runtime/lib/lynx.js.map +1 -0
  144. package/runtime/lib/opcodes.d.ts +2 -0
  145. package/runtime/lib/opcodes.js +59 -0
  146. package/runtime/lib/opcodes.js.map +1 -0
  147. package/runtime/lib/renderToOpcodes/constants.d.ts +17 -0
  148. package/runtime/lib/renderToOpcodes/constants.js +23 -0
  149. package/runtime/lib/renderToOpcodes/constants.js.map +1 -0
  150. package/runtime/lib/renderToOpcodes/index.d.ts +13 -0
  151. package/runtime/lib/renderToOpcodes/index.js +227 -0
  152. package/runtime/lib/renderToOpcodes/index.js.map +1 -0
  153. package/runtime/lib/root.d.ts +7 -0
  154. package/runtime/lib/root.js +17 -0
  155. package/runtime/lib/root.js.map +1 -0
  156. package/runtime/lib/snapshot/event.d.ts +3 -0
  157. package/runtime/lib/snapshot/event.js +24 -0
  158. package/runtime/lib/snapshot/event.js.map +1 -0
  159. package/runtime/lib/snapshot/gesture.d.ts +2 -0
  160. package/runtime/lib/snapshot/gesture.js +18 -0
  161. package/runtime/lib/snapshot/gesture.js.map +1 -0
  162. package/runtime/lib/snapshot/platformInfo.d.ts +4 -0
  163. package/runtime/lib/snapshot/platformInfo.js +35 -0
  164. package/runtime/lib/snapshot/platformInfo.js.map +1 -0
  165. package/runtime/lib/snapshot/ref.d.ts +10 -0
  166. package/runtime/lib/snapshot/ref.js +119 -0
  167. package/runtime/lib/snapshot/ref.js.map +1 -0
  168. package/runtime/lib/snapshot/spread.d.ts +5 -0
  169. package/runtime/lib/snapshot/spread.js +289 -0
  170. package/runtime/lib/snapshot/spread.js.map +1 -0
  171. package/runtime/lib/snapshot/workletEvent.d.ts +3 -0
  172. package/runtime/lib/snapshot/workletEvent.js +22 -0
  173. package/runtime/lib/snapshot/workletEvent.js.map +1 -0
  174. package/runtime/lib/snapshot/workletRef.d.ts +5 -0
  175. package/runtime/lib/snapshot/workletRef.js +52 -0
  176. package/runtime/lib/snapshot/workletRef.js.map +1 -0
  177. package/runtime/lib/snapshot.d.ts +81 -0
  178. package/runtime/lib/snapshot.js +477 -0
  179. package/runtime/lib/snapshot.js.map +1 -0
  180. package/runtime/lib/snapshotPatch.d.ts +14 -0
  181. package/runtime/lib/snapshotPatch.js +31 -0
  182. package/runtime/lib/snapshotPatch.js.map +1 -0
  183. package/runtime/lib/snapshotPatchApply.d.ts +2 -0
  184. package/runtime/lib/snapshotPatchApply.js +110 -0
  185. package/runtime/lib/snapshotPatchApply.js.map +1 -0
  186. package/runtime/lib/utils.d.ts +4 -0
  187. package/runtime/lib/utils.js +32 -0
  188. package/runtime/lib/utils.js.map +1 -0
  189. package/runtime/lib/worklet/ctx.d.ts +1 -0
  190. package/runtime/lib/worklet/ctx.js +17 -0
  191. package/runtime/lib/worklet/ctx.js.map +1 -0
  192. package/runtime/lib/worklet/execMap.d.ts +1 -0
  193. package/runtime/lib/worklet/execMap.js +42 -0
  194. package/runtime/lib/worklet/execMap.js.map +1 -0
  195. package/runtime/lib/worklet/functionality.d.ts +1 -0
  196. package/runtime/lib/worklet/functionality.js +11 -0
  197. package/runtime/lib/worklet/functionality.js.map +1 -0
  198. package/runtime/lib/worklet/hmr.d.ts +1 -0
  199. package/runtime/lib/worklet/hmr.js +21 -0
  200. package/runtime/lib/worklet/hmr.js.map +1 -0
  201. package/runtime/lib/worklet/indexMap.d.ts +8 -0
  202. package/runtime/lib/worklet/indexMap.js +17 -0
  203. package/runtime/lib/worklet/indexMap.js.map +1 -0
  204. package/runtime/lib/worklet/jsImpl.d.ts +11 -0
  205. package/runtime/lib/worklet/jsImpl.js +68 -0
  206. package/runtime/lib/worklet/jsImpl.js.map +1 -0
  207. package/runtime/lib/worklet/runWorklet.d.ts +14 -0
  208. package/runtime/lib/worklet/runWorklet.js +82 -0
  209. package/runtime/lib/worklet/runWorklet.js.map +1 -0
  210. package/runtime/lib/worklet/workletRef.d.ts +131 -0
  211. package/runtime/lib/worklet/workletRef.js +96 -0
  212. package/runtime/lib/worklet/workletRef.js.map +1 -0
  213. package/runtime/lib/worklet/workletRefPool.d.ts +2 -0
  214. package/runtime/lib/worklet/workletRefPool.js +22 -0
  215. package/runtime/lib/worklet/workletRefPool.js.map +1 -0
  216. package/runtime/src/backgroundSnapshot.ts +452 -0
  217. package/runtime/src/compat/componentIs.ts +30 -0
  218. package/runtime/src/compat/initData.ts +140 -0
  219. package/runtime/src/compat/lynxComponent.ts +80 -0
  220. package/runtime/src/debug/debug.ts +9 -0
  221. package/runtime/src/debug/printSnapshot.ts +28 -0
  222. package/runtime/src/debug/profile.ts +54 -0
  223. package/runtime/src/document.ts +53 -0
  224. package/runtime/src/gesture/processGesture.ts +77 -0
  225. package/runtime/src/gesture/processGestureBagkround.ts +15 -0
  226. package/runtime/src/gesture/types.ts +39 -0
  227. package/runtime/src/hooks/react.ts +62 -0
  228. package/runtime/src/hooks/useLynxGlobalEventListener.ts +59 -0
  229. package/runtime/src/hydrate.ts +364 -0
  230. package/runtime/src/index.ts +89 -0
  231. package/runtime/src/internal.ts +60 -0
  232. package/runtime/src/legacy-react-runtime/index.ts +40 -0
  233. package/runtime/src/lifecycle/delayUnmount.ts +77 -0
  234. package/runtime/src/lifecycle/destroy.ts +26 -0
  235. package/runtime/src/lifecycle/pass.ts +14 -0
  236. package/runtime/src/lifecycle/patchUpdate.ts +240 -0
  237. package/runtime/src/lifecycle/reload.ts +79 -0
  238. package/runtime/src/lifecycle/render.ts +46 -0
  239. package/runtime/src/lifecycleConstant.ts +16 -0
  240. package/runtime/src/list.ts +375 -0
  241. package/runtime/src/lynx/calledByNative.ts +132 -0
  242. package/runtime/src/lynx/component.ts +116 -0
  243. package/runtime/src/lynx/dynamic-js.ts +33 -0
  244. package/runtime/src/lynx/env.ts +110 -0
  245. package/runtime/src/lynx/injectLepusMethods.ts +53 -0
  246. package/runtime/src/lynx/lazy-bundle.ts +126 -0
  247. package/runtime/src/lynx/performance.ts +117 -0
  248. package/runtime/src/lynx/tt.ts +212 -0
  249. package/runtime/src/lynx-api.ts +374 -0
  250. package/runtime/src/lynx.ts +105 -0
  251. package/runtime/src/opcodes.ts +64 -0
  252. package/runtime/src/renderToOpcodes/constants.ts +24 -0
  253. package/runtime/src/renderToOpcodes/index.ts +301 -0
  254. package/runtime/src/root.ts +19 -0
  255. package/runtime/src/snapshot/event.ts +33 -0
  256. package/runtime/src/snapshot/gesture.ts +25 -0
  257. package/runtime/src/snapshot/platformInfo.ts +48 -0
  258. package/runtime/src/snapshot/ref.ts +151 -0
  259. package/runtime/src/snapshot/spread.ts +287 -0
  260. package/runtime/src/snapshot/workletEvent.ts +33 -0
  261. package/runtime/src/snapshot/workletRef.ts +63 -0
  262. package/runtime/src/snapshot.ts +576 -0
  263. package/runtime/src/snapshotPatch.ts +56 -0
  264. package/runtime/src/snapshotPatchApply.ts +117 -0
  265. package/runtime/src/utils.ts +35 -0
  266. package/runtime/src/worklet/ctx.ts +22 -0
  267. package/runtime/src/worklet/execMap.ts +50 -0
  268. package/runtime/src/worklet/functionality.ts +12 -0
  269. package/runtime/src/worklet/hmr.ts +26 -0
  270. package/runtime/src/worklet/indexMap.ts +20 -0
  271. package/runtime/src/worklet/jsImpl.ts +86 -0
  272. package/runtime/src/worklet/runWorklet.ts +88 -0
  273. package/runtime/src/worklet/workletRef.ts +229 -0
  274. package/runtime/src/worklet/workletRefPool.ts +25 -0
  275. package/transform/cjs/main.cjs +6 -0
  276. package/transform/cjs/main.d.cts +3 -0
  277. package/transform/cjs/package.json +3 -0
  278. package/transform/dist/wasm.cjs +8478 -0
  279. package/transform/index.d.ts +187 -0
  280. package/transform/main.js +15 -0
  281. package/types/react.d.ts +147 -0
  282. package/types/react.docs.d.ts +74 -0
  283. package/worklet-runtime/dist/dev.js +414 -0
  284. package/worklet-runtime/dist/dev.js.map +8 -0
  285. package/worklet-runtime/dist/main.js +414 -0
  286. package/worklet-runtime/dist/main.js.map +8 -0
  287. package/worklet-runtime/lib/api/element.d.ts +14 -0
  288. package/worklet-runtime/lib/api/element.js +71 -0
  289. package/worklet-runtime/lib/api/element.js.map +1 -0
  290. package/worklet-runtime/lib/api/lepusQuerySelector.d.ts +3 -0
  291. package/worklet-runtime/lib/api/lepusQuerySelector.js +23 -0
  292. package/worklet-runtime/lib/api/lepusQuerySelector.js.map +1 -0
  293. package/worklet-runtime/lib/api/lynxApi.d.ts +2 -0
  294. package/worklet-runtime/lib/api/lynxApi.js +35 -0
  295. package/worklet-runtime/lib/api/lynxApi.js.map +1 -0
  296. package/worklet-runtime/lib/bindings/bindings.d.ts +14 -0
  297. package/worklet-runtime/lib/bindings/bindings.js +61 -0
  298. package/worklet-runtime/lib/bindings/bindings.js.map +1 -0
  299. package/worklet-runtime/lib/bindings/events.d.ts +7 -0
  300. package/worklet-runtime/lib/bindings/events.js +9 -0
  301. package/worklet-runtime/lib/bindings/events.js.map +1 -0
  302. package/worklet-runtime/lib/bindings/index.d.ts +4 -0
  303. package/worklet-runtime/lib/bindings/index.js +7 -0
  304. package/worklet-runtime/lib/bindings/index.js.map +1 -0
  305. package/worklet-runtime/lib/bindings/loadRuntime.d.ts +8 -0
  306. package/worklet-runtime/lib/bindings/loadRuntime.js +25 -0
  307. package/worklet-runtime/lib/bindings/loadRuntime.js.map +1 -0
  308. package/worklet-runtime/lib/bindings/types.d.ts +35 -0
  309. package/worklet-runtime/lib/bindings/types.js +2 -0
  310. package/worklet-runtime/lib/bindings/types.js.map +1 -0
  311. package/worklet-runtime/lib/delayWorkletEvent.d.ts +11 -0
  312. package/worklet-runtime/lib/delayWorkletEvent.js +49 -0
  313. package/worklet-runtime/lib/delayWorkletEvent.js.map +1 -0
  314. package/worklet-runtime/lib/global.d.ts +15 -0
  315. package/worklet-runtime/lib/global.js +2 -0
  316. package/worklet-runtime/lib/global.js.map +1 -0
  317. package/worklet-runtime/lib/index.d.ts +1 -0
  318. package/worklet-runtime/lib/index.js +12 -0
  319. package/worklet-runtime/lib/index.js.map +1 -0
  320. package/worklet-runtime/lib/jsFunctionLifecycle.d.ts +18 -0
  321. package/worklet-runtime/lib/jsFunctionLifecycle.js +55 -0
  322. package/worklet-runtime/lib/jsFunctionLifecycle.js.map +1 -0
  323. package/worklet-runtime/lib/listeners.d.ts +2 -0
  324. package/worklet-runtime/lib/listeners.js +17 -0
  325. package/worklet-runtime/lib/listeners.js.map +1 -0
  326. package/worklet-runtime/lib/utils/profile.d.ts +1 -0
  327. package/worklet-runtime/lib/utils/profile.js +20 -0
  328. package/worklet-runtime/lib/utils/profile.js.map +1 -0
  329. package/worklet-runtime/lib/utils/version.d.ts +1 -0
  330. package/worklet-runtime/lib/utils/version.js +10 -0
  331. package/worklet-runtime/lib/utils/version.js.map +1 -0
  332. package/worklet-runtime/lib/workletRef.d.ts +12 -0
  333. package/worklet-runtime/lib/workletRef.js +50 -0
  334. package/worklet-runtime/lib/workletRef.js.map +1 -0
  335. package/worklet-runtime/lib/workletRuntime.d.ts +2 -0
  336. package/worklet-runtime/lib/workletRuntime.js +128 -0
  337. package/worklet-runtime/lib/workletRuntime.js.map +1 -0
@@ -0,0 +1,364 @@
1
+ // Copyright 2024 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+
5
+ import { __pendingListUpdates, componentAtIndexFactory, enqueueComponentFactory } from './list.js';
6
+ import { unref } from './snapshot/ref.js';
7
+ import { DynamicPartType, SnapshotInstance } from './snapshot.js';
8
+ import { isEmptyObject } from './utils.js';
9
+
10
+ export interface DiffResult<K> {
11
+ $$diff: true;
12
+ // insert No.j to new
13
+ i: Record<number, K>;
14
+ // remove No.i from old
15
+ r: number[];
16
+ // move No.i from old to No.j of new
17
+ m: Record<number, number>;
18
+ }
19
+
20
+ export interface Typed {
21
+ type: string;
22
+ }
23
+
24
+ export function isEmptyDiffResult<K>(diffResult: DiffResult<K>): boolean {
25
+ const hasChanged = !isEmptyObject(diffResult.i)
26
+ || !isEmptyObject(diffResult.m) || diffResult.r.length > 0;
27
+ return !hasChanged;
28
+ }
29
+
30
+ export function diffArrayLepus<A extends Typed, B extends Typed>(
31
+ before: A[],
32
+ after: B[],
33
+ isSameType: (a: A, b: B) => boolean,
34
+ onDiffChildren: (a: A, b: B, oldIndex: number, newIndex: number) => void,
35
+ ): DiffResult<B> {
36
+ let lastPlacedIndex = 0;
37
+ const result: DiffResult<B> = {
38
+ $$diff: true,
39
+ i: {},
40
+ r: [],
41
+ m: {},
42
+ };
43
+ const beforeMap: Record<string, Set<[A, number]>> = {};
44
+
45
+ for (let i = 0; i < before.length; i++) {
46
+ const node = before[i]!;
47
+ (beforeMap[node.type] ??= new Set()).add([node, i]);
48
+ }
49
+
50
+ for (let i = 0; i < after.length; i++) {
51
+ const afterNode = after[i]!;
52
+ const beforeNodes = beforeMap[afterNode.type];
53
+ let beforeNode: [A, number];
54
+
55
+ if (
56
+ beforeNodes
57
+ && beforeNodes.size > 0
58
+ // @ts-expect-error TS does not know about iterator :)
59
+ && (([beforeNode] = beforeNodes), beforeNode)
60
+ && isSameType(beforeNode[0], afterNode)
61
+ ) {
62
+ // Reuse old node
63
+ beforeNodes.delete(beforeNode);
64
+
65
+ const oldIndex = beforeNode[1];
66
+ onDiffChildren(beforeNode[0], afterNode, oldIndex, i);
67
+ if (oldIndex < lastPlacedIndex) {
68
+ result.m[oldIndex] = i;
69
+ continue;
70
+ } else {
71
+ lastPlacedIndex = oldIndex;
72
+ }
73
+ } else {
74
+ // Create new node
75
+ result.i[i] = afterNode;
76
+ }
77
+ }
78
+ // Delete
79
+ for (const k in beforeMap) {
80
+ for (const [, i] of beforeMap[k]!) {
81
+ result.r.push(i);
82
+ }
83
+ }
84
+
85
+ return result;
86
+ }
87
+
88
+ // export function diffIterableLepus<A extends Typed, B extends Typed>(
89
+ // before: A[],
90
+ // after: Iterable<B>,
91
+ // isSameType: (a: A, b: B) => boolean,
92
+ // onDiffChildren: (a: A, b: B) => void
93
+ // ): DiffResult<B> {
94
+ // let returnResult = EMPTY_DIFF_RESULT as DiffResult<B>;
95
+ // let lastPlacedIndex = 0;
96
+ // const result: DiffResult<B> = {
97
+ // $$diff: true,
98
+ // i: {},
99
+ // r: [],
100
+ // m: {},
101
+ // };
102
+ // const beforeMap: Record<string, Set<[A, number]>> = {};
103
+
104
+ // for (let i = 0; i < before.length; i++) {
105
+ // let node = before[i];
106
+ // (beforeMap[node.type] ??= new Set()).add([node, i]);
107
+ // }
108
+
109
+ // let i = 0;
110
+ // for (const afterNode of after) {
111
+ // const beforeNodes = beforeMap[afterNode.type];
112
+ // let beforeNode: [A, number];
113
+
114
+ // if (beforeNodes && (([beforeNode] = beforeNodes), beforeNode) && isSameType(beforeNode[0], afterNode)) {
115
+ // // Reuse old node
116
+ // beforeNodes.delete(beforeNode);
117
+
118
+ // const oldIndex = beforeNode[1];
119
+ // onDiffChildren(beforeNode[0], afterNode);
120
+ // if (oldIndex < lastPlacedIndex) {
121
+ // result.m[oldIndex] = i;
122
+ // returnResult = result;
123
+ // i++;
124
+ // continue;
125
+ // } else {
126
+ // lastPlacedIndex = oldIndex;
127
+ // }
128
+ // } else {
129
+ // // Create new node
130
+ // result.i[i] = afterNode;
131
+ // returnResult = result;
132
+ // }
133
+ // i++;
134
+ // }
135
+ // // delete
136
+ // for (const k in beforeMap) {
137
+ // for (const [, i] of beforeMap[k]) {
138
+ // result.r.push(i);
139
+ // returnResult = result;
140
+ // }
141
+ // }
142
+
143
+ // return result;
144
+ // }
145
+
146
+ export function diffArrayAction<T, K>(
147
+ before: T[],
148
+ diffResult: DiffResult<K>,
149
+ onInsert: (node: K, target: T | undefined) => T,
150
+ onRemove: (node: T) => void,
151
+ onMove: (node: T, target: T | undefined) => void,
152
+ ): T[] {
153
+ if (isEmptyDiffResult(diffResult)) {
154
+ return before;
155
+ }
156
+ const deleteSet = new Set(diffResult.r);
157
+ const { i: insertMap, m: placementMap } = diffResult;
158
+ const moveTempMap = new Map<number, T>();
159
+ let old: T | undefined;
160
+ let k = 0;
161
+ old = before[k];
162
+ // let current: T | null | undefined = null;
163
+ const result: T[] = [];
164
+ let i = 0; // index of the old list
165
+ let j = 0; // index of the new list
166
+ let remain = Object.keys(insertMap).length;
167
+ while (old || remain > 0) {
168
+ let keep = false;
169
+ if (old && deleteSet.has(j)) {
170
+ // delete
171
+ onRemove(old);
172
+ } else if (old && placementMap[j] !== undefined) {
173
+ // save node to re-use
174
+ moveTempMap.set(placementMap[j]!, old);
175
+ remain++;
176
+ } else {
177
+ // insert node
178
+ let newNode = old;
179
+ if (moveTempMap.has(i)) {
180
+ // insert re-used node
181
+ newNode = moveTempMap.get(i)!;
182
+ keep = true;
183
+ onMove(newNode, old);
184
+ remain--;
185
+ } else if (insertMap[i] !== undefined) {
186
+ // insert new node
187
+ newNode = onInsert(insertMap[i]!, old);
188
+ keep = true;
189
+ remain--;
190
+ }
191
+
192
+ result.push(newNode!);
193
+ i++;
194
+ }
195
+ if (old && !keep) {
196
+ old = before[++k];
197
+ j++;
198
+ }
199
+ }
200
+
201
+ return result;
202
+ }
203
+
204
+ export interface HydrationOptions {
205
+ skipUnRef?: boolean;
206
+ swap?: Record<number, number>;
207
+ }
208
+
209
+ export function hydrate(before: SnapshotInstance, after: SnapshotInstance, options?: HydrationOptions): void {
210
+ after.__elements = before.__elements;
211
+ after.__element_root = before.__element_root;
212
+
213
+ if (!(options?.skipUnRef)) {
214
+ unref(before, false);
215
+ }
216
+
217
+ let swap;
218
+ if (swap = options?.swap) {
219
+ swap[before.__id] = after.__id;
220
+ }
221
+
222
+ after.__values?.forEach((value, index) => {
223
+ const old = before.__values![index];
224
+ if (value !== old) {
225
+ after.__values![index] = old;
226
+ after.setAttribute(index, value);
227
+ }
228
+ });
229
+
230
+ const { slot } = after.__snapshot_def;
231
+
232
+ if (!slot) {
233
+ return;
234
+ }
235
+
236
+ const beforeChildNodes = before.childNodes;
237
+ const afterChildNodes = after.childNodes;
238
+
239
+ slot.forEach(([type, elementIndex], index) => {
240
+ switch (type) {
241
+ case DynamicPartType.Slot:
242
+ case DynamicPartType.MultiChildren: {
243
+ // TODO: the following null assertions are not 100% safe
244
+ const v1 = beforeChildNodes[index]!;
245
+ const v2 = afterChildNodes[index]!;
246
+ hydrate(v1, v2, options);
247
+ break;
248
+ }
249
+ case DynamicPartType.Children: {
250
+ const diffResult = diffArrayLepus(
251
+ beforeChildNodes,
252
+ afterChildNodes,
253
+ (a, b) => a.type === b.type,
254
+ (a, b) => {
255
+ hydrate(a, b, options);
256
+ },
257
+ );
258
+ diffArrayAction(
259
+ beforeChildNodes,
260
+ diffResult,
261
+ (node, target) => {
262
+ node.ensureElements();
263
+ __InsertElementBefore(
264
+ before.__elements![elementIndex]!,
265
+ node.__element_root!,
266
+ target?.__element_root,
267
+ );
268
+ return node;
269
+ },
270
+ node => {
271
+ __RemoveElement(
272
+ before.__elements![elementIndex]!,
273
+ node.__element_root!,
274
+ );
275
+ },
276
+ (node, target) => {
277
+ __RemoveElement(
278
+ before.__elements![elementIndex]!,
279
+ node.__element_root!,
280
+ );
281
+ __InsertElementBefore(
282
+ before.__elements![elementIndex]!,
283
+ node.__element_root!,
284
+ target?.__element_root,
285
+ );
286
+ },
287
+ );
288
+ break;
289
+ }
290
+ case DynamicPartType.ListChildren: {
291
+ const removals: number[] = [];
292
+ const insertions: number[] = [];
293
+ const updateAction: any[] = [];
294
+
295
+ const diffResult = diffArrayLepus(
296
+ beforeChildNodes,
297
+ afterChildNodes,
298
+ (a, b) => a.type === b.type,
299
+ (a, b, oldIndex, newIndex) => {
300
+ if (
301
+ JSON.stringify(a.__listItemPlatformInfo)
302
+ !== JSON.stringify(b.__listItemPlatformInfo)
303
+ ) {
304
+ updateAction.push({
305
+ ...b.__listItemPlatformInfo,
306
+ from: newIndex,
307
+ to: newIndex,
308
+ // no flush
309
+ flush: false,
310
+ });
311
+ }
312
+
313
+ // Mark list-item which is rendered (has `__elements`) as DELETE
314
+ // so list platform will call `enqueueComponent` on it
315
+ // and will call `componentAtIndex` on the inserted one
316
+ // In this way:
317
+ // 1. we make sure `<list/>` for hydrate is like a leaf node
318
+ // 2. we avoid hydrate so modifying recycleMap can be avoid
319
+ // 3. the delete list-item is recycled for later use, so no waste
320
+ if (a.__elements) {
321
+ removals.push(oldIndex);
322
+ insertions.push(newIndex);
323
+ }
324
+ },
325
+ );
326
+
327
+ for (const i of diffResult.r) {
328
+ removals.push(i);
329
+ }
330
+ for (const i in diffResult.i) {
331
+ insertions.push(Number(i));
332
+ }
333
+ for (const i in diffResult.m) {
334
+ removals.push(Number(i));
335
+ insertions.push(diffResult.m[i]!);
336
+ }
337
+ insertions.sort((a, b) => a - b);
338
+ removals.sort((a, b) => a - b);
339
+
340
+ const info = {
341
+ insertAction: insertions.map((it) => ({
342
+ position: it,
343
+ type: afterChildNodes[it]!.type,
344
+ ...afterChildNodes[it]!.__listItemPlatformInfo,
345
+ })),
346
+ removeAction: removals,
347
+ updateAction,
348
+ };
349
+
350
+ const listElement = before.__elements![elementIndex]!;
351
+ __SetAttribute(listElement, 'update-list-info', info);
352
+ __UpdateListCallbacks(
353
+ listElement,
354
+ componentAtIndexFactory(afterChildNodes),
355
+ enqueueComponentFactory(),
356
+ );
357
+
358
+ // The `before` & `after` target to the same list element, so we need to
359
+ // avoid the newly created list's (behind snapshot instance `after`) "update-list-info" being recorded.
360
+ delete __pendingListUpdates.values[after.__id];
361
+ }
362
+ }
363
+ });
364
+ }
@@ -0,0 +1,89 @@
1
+ // Copyright 2024 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+
5
+ import './lynx.js';
6
+ import './lynx/component.js';
7
+ import {
8
+ Children,
9
+ Component,
10
+ Fragment,
11
+ PureComponent,
12
+ Suspense,
13
+ lazy as backgroundLazy,
14
+ createContext,
15
+ createElement,
16
+ createRef,
17
+ forwardRef,
18
+ isValidElement,
19
+ memo,
20
+ useSyncExternalStore,
21
+ } from 'preact/compat';
22
+
23
+ import {
24
+ useCallback,
25
+ useContext,
26
+ useDebugValue,
27
+ useEffect,
28
+ useImperativeHandle,
29
+ useLayoutEffect,
30
+ useMemo,
31
+ useReducer,
32
+ useRef,
33
+ useState,
34
+ } from './hooks/react.js';
35
+ import { mainThreadLazy } from './lynx/lazy-bundle.js';
36
+
37
+ export { Component, createContext } from 'preact';
38
+ export { PureComponent } from 'preact/compat';
39
+ export * from './hooks/react.js';
40
+
41
+ const lazy: typeof import('preact/compat').lazy = __LEPUS__
42
+ ? mainThreadLazy
43
+ : backgroundLazy;
44
+
45
+ /**
46
+ * @internal
47
+ */
48
+ export default {
49
+ // hooks
50
+ useState,
51
+ useReducer,
52
+ useEffect,
53
+ useLayoutEffect,
54
+ useRef,
55
+ useImperativeHandle,
56
+ useMemo,
57
+ useCallback,
58
+ useContext,
59
+ useDebugValue,
60
+ useSyncExternalStore,
61
+
62
+ createContext,
63
+ createRef,
64
+ Fragment,
65
+ isValidElement,
66
+ Children,
67
+ Component,
68
+ PureComponent,
69
+ memo,
70
+ forwardRef,
71
+ Suspense,
72
+ lazy,
73
+ createElement,
74
+ };
75
+
76
+ export {
77
+ Children,
78
+ createRef,
79
+ Fragment,
80
+ isValidElement,
81
+ memo,
82
+ forwardRef,
83
+ Suspense,
84
+ lazy,
85
+ createElement,
86
+ useSyncExternalStore,
87
+ };
88
+
89
+ export * from './lynx-api.js';
@@ -0,0 +1,60 @@
1
+ // Copyright 2024 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+
5
+ import { Suspense, createElement, lazy } from 'preact/compat';
6
+ import type { FC } from 'react';
7
+
8
+ import { factory as factory2 } from './compat/componentIs.js';
9
+ import { useMemo } from './hooks/react.js';
10
+ import { snapshotCreateList } from './list.js';
11
+ import { loadLazyBundle } from './lynx/lazy-bundle.js';
12
+ import { __root } from './root.js';
13
+ import { DynamicPartType, SnapshotInstance, snapshotManager, __page, __pageId, createSnapshot } from './snapshot.js';
14
+
15
+ export { __page, __pageId, __root };
16
+
17
+ export { SnapshotInstance, snapshotCreateList, createSnapshot, snapshotManager };
18
+
19
+ export const __DynamicPartSlot: DynamicPartType = DynamicPartType.Slot;
20
+ export const __DynamicPartMultiChildren: DynamicPartType = DynamicPartType.MultiChildren;
21
+ export const __DynamicPartChildren: DynamicPartType = DynamicPartType.Children;
22
+ export const __DynamicPartListChildren: DynamicPartType = DynamicPartType.ListChildren;
23
+ export const __DynamicPartChildren_0: [DynamicPartType, number][] = [[DynamicPartType.Children, 0]];
24
+
25
+ export { updateSpread } from './snapshot/spread.js';
26
+ export { updateEvent } from './snapshot/event.js';
27
+ export { updateRef, transformRef } from './snapshot/ref.js';
28
+ export { updateWorkletEvent } from './snapshot/workletEvent.js';
29
+ export { updateWorkletRef } from './snapshot/workletRef.js';
30
+ export { updateGesture } from './snapshot/gesture.js';
31
+ export { updateListItemPlatformInfo } from './snapshot/platformInfo.js';
32
+
33
+ export {
34
+ options,
35
+ // Component is not an internal API, but refresh needs it from 'react/internal'
36
+ Component,
37
+ } from 'preact';
38
+ export type { Options } from 'preact';
39
+
40
+ export { loadDynamicJS, __dynamicImport } from './lynx/dynamic-js.js';
41
+
42
+ export { withInitDataInState } from './compat/initData.js';
43
+
44
+ export { wrapWithLynxComponent } from './compat/lynxComponent.js';
45
+
46
+ /**
47
+ * @internal a polyfill for <component is=? />
48
+ */
49
+ export const __ComponentIsPolyfill: FC<{ is: string }> = /* @__PURE__ */ factory2(
50
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
51
+ { Suspense, lazy, createElement, useMemo } as any,
52
+ loadLazyBundle,
53
+ );
54
+
55
+ export { loadLazyBundle } from './lynx/lazy-bundle.js';
56
+
57
+ export { transformToWorklet } from './worklet/runWorklet.js';
58
+ export { registerWorkletOnBackground } from './worklet/hmr.js';
59
+
60
+ export { loadWorkletRuntime } from '@lynx-js/react/worklet-runtime/bindings';
@@ -0,0 +1,40 @@
1
+ // Copyright 2024 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+ import { createContext } from 'preact';
5
+ import { lazy } from 'preact/compat';
6
+
7
+ import {
8
+ ComponentFromReactRuntime as Component,
9
+ ComponentFromReactRuntime as PureComponent,
10
+ } from '../compat/lynxComponent.js';
11
+ import { useCallback, useEffect, useMemo, useReducer, useState } from '../hooks/react.js';
12
+
13
+ /* v8 ignore next 3 */
14
+ function __runInJS<T>(value: T): T | undefined | null {
15
+ return value;
16
+ }
17
+
18
+ // should mirror @lynx-js/react-runtime's exports
19
+ export { ComponentFromReactRuntime as Component } from '../compat/lynxComponent.js';
20
+ export { ComponentFromReactRuntime as PureComponent } from '../compat/lynxComponent.js';
21
+ export { createContext } from 'preact';
22
+ export { lazy } from 'preact/compat';
23
+ export { useState, useReducer, useEffect, useMemo, useCallback /*, useInstance */ } from '../hooks/react.js';
24
+ export { __runInJS };
25
+
26
+ /**
27
+ * @internal
28
+ */
29
+ export default {
30
+ Component,
31
+ PureComponent,
32
+ createContext,
33
+ lazy,
34
+ useState,
35
+ useReducer,
36
+ useEffect,
37
+ useMemo,
38
+ useCallback,
39
+ __runInJS,
40
+ };
@@ -0,0 +1,77 @@
1
+ // Copyright 2024 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+ import { options } from 'preact';
5
+ import type { VNode } from 'preact';
6
+
7
+ import { CATCH_ERROR, COMPONENT, DIFF, VNODE } from '../renderToOpcodes/constants.js';
8
+
9
+ type FakeVNode = VNode;
10
+ type DelayedUnmounts = (() => void)[];
11
+ let delayedUnmounts: DelayedUnmounts = [];
12
+ let parentVNode: FakeVNode | undefined;
13
+
14
+ function takeDelayedUnmounts(): DelayedUnmounts {
15
+ const ret = delayedUnmounts;
16
+ delayedUnmounts = [];
17
+ // not clearing `parentVNode` here, which would be cleared before next diff in `options[DIFF]`
18
+ return ret;
19
+ }
20
+
21
+ function runDelayedUnmounts(delayedUnmounts: DelayedUnmounts): void {
22
+ for (const fn of delayedUnmounts) {
23
+ fn();
24
+ }
25
+ }
26
+
27
+ /**
28
+ * Delay `componentWillUnmount` until main thread patching finishes.
29
+ */
30
+ function initDelayUnmount(): void {
31
+ const oldUnmount = options.unmount;
32
+ options.unmount = (vnode: VNode) => {
33
+ if (!parentVNode) {
34
+ // `parentVNode` is the first vnode to unmount,
35
+ // which is needed to find proper error boundary when running `componentWillUnmount`.
36
+ // Shallow copy vnode to prevent modification to vnode in preact unmounting process.
37
+ parentVNode = { ...vnode };
38
+
39
+ const oldDiff = options[DIFF] as (vnode: VNode) => void;
40
+ options[DIFF] = (vnode: VNode) => {
41
+ // A new diff indicates that the unmounting process of parentVNode is finished.
42
+ parentVNode = undefined;
43
+ options[DIFF] = oldDiff;
44
+ oldDiff?.(vnode);
45
+ };
46
+ }
47
+
48
+ const component = vnode[COMPONENT];
49
+ if (component) {
50
+ if (oldUnmount) {
51
+ const vnode_clone = { ...vnode };
52
+ delayedUnmounts.push(() => {
53
+ const v = vnode_clone[COMPONENT]![VNODE] as VNode<{}> | null;
54
+ vnode_clone[COMPONENT]![VNODE] = vnode_clone;
55
+ oldUnmount?.(vnode_clone);
56
+ vnode_clone[COMPONENT]![VNODE] = v;
57
+ });
58
+ }
59
+ if (component.componentWillUnmount) {
60
+ const unmount = component.componentWillUnmount;
61
+ // @ts-ignore
62
+ component.componentWillUnmount = undefined;
63
+ const parentVNode_ = parentVNode;
64
+ delayedUnmounts.push(() => {
65
+ try {
66
+ component.componentWillUnmount = unmount;
67
+ component.componentWillUnmount();
68
+ } catch (e) {
69
+ options[CATCH_ERROR](e, parentVNode_);
70
+ }
71
+ });
72
+ }
73
+ }
74
+ };
75
+ }
76
+
77
+ export { initDelayUnmount, takeDelayedUnmounts, runDelayedUnmounts };
@@ -0,0 +1,26 @@
1
+ // Copyright 2024 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+ import { render } from 'preact';
5
+
6
+ import { __root } from '../root.js';
7
+ import { globalCommitTaskMap } from './patchUpdate.js';
8
+
9
+ function destroyBackground(): void {
10
+ if (__PROFILE__) {
11
+ console.profile('destroyBackground');
12
+ }
13
+
14
+ render(null, __root as any);
15
+
16
+ globalCommitTaskMap.forEach(task => {
17
+ task();
18
+ });
19
+ globalCommitTaskMap.clear();
20
+
21
+ if (__PROFILE__) {
22
+ console.profileEnd();
23
+ }
24
+ }
25
+
26
+ export { destroyBackground };
@@ -0,0 +1,14 @@
1
+ // Copyright 2024 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+ let reloadVersion = 0;
5
+
6
+ function getReloadVersion(): number {
7
+ return reloadVersion;
8
+ }
9
+
10
+ function increaseReloadVersion(): number {
11
+ return ++reloadVersion;
12
+ }
13
+
14
+ export { getReloadVersion, increaseReloadVersion };