@lynx-js/react 0.129.2 → 0.129.4

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 (420) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/components/tsconfig.tsbuildinfo +1 -1
  3. package/package.json +2 -2
  4. package/refresh/.turbo/turbo-build.log +3 -3
  5. package/refresh/dist/index.js +113 -20
  6. package/refresh/rslib.config.ts +13 -3
  7. package/refresh/src/index.ts +100 -0
  8. package/refresh/src/self.ts +38 -0
  9. package/runtime/__test__/{snapshot → core}/attribute-name.test.ts +1 -1
  10. package/runtime/__test__/core/background-function.test.ts +37 -0
  11. package/runtime/__test__/{shared → core}/component-stack.test.ts +4 -4
  12. package/runtime/__test__/core/ref.test.ts +218 -1
  13. package/runtime/__test__/core/sync-effect-flush.test.ts +58 -0
  14. package/runtime/__test__/element-template/debug/alog.test.ts +20 -12
  15. package/runtime/__test__/element-template/fixtures/background/hydrate/_shared.tsx +15 -34
  16. package/runtime/__test__/element-template/fixtures/background/hydrate/children.creates-missing-nodes-recursively/output.txt +2 -0
  17. package/runtime/__test__/element-template/fixtures/background/hydrate/children.iterates-existing-slots/output.txt +1 -0
  18. package/runtime/__test__/element-template/fixtures/background/hydrate/children.missing-attrs-element/output.txt +6 -1
  19. package/runtime/__test__/element-template/fixtures/background/hydrate/children.missing-slot-record-on-main/output.txt +1 -0
  20. package/runtime/__test__/element-template/fixtures/background/hydrate/{coverage.move-before-child → children.reconciles-raw-text-slots}/case.tsx +1 -1
  21. package/runtime/__test__/element-template/fixtures/background/hydrate/coverage.emit-create-raw-text/output.txt +1 -0
  22. package/runtime/__test__/element-template/fixtures/background/hydrate/coverage.emit-create-raw-text-non-text/output.txt +1 -0
  23. package/runtime/__test__/element-template/fixtures/background/hydrate/root-key-mismatch-reconciles/output.txt +1 -0
  24. package/runtime/__test__/element-template/fixtures/background/hydrate-compiled/children.creates-and-inserts-new/output.txt +1 -0
  25. package/runtime/__test__/element-template/fixtures/background/hydrate-compiled/children.inserts-before-existing-sibling/output.txt +1 -0
  26. package/runtime/__test__/element-template/fixtures/background/hydrate-compiled/children.mixed-operations/output.txt +1 -0
  27. package/runtime/__test__/element-template/fixtures/background/hydrate-compiled/children.non-string-raw-text-key-on-main/output.txt +1 -0
  28. package/runtime/__test__/element-template/fixtures/background/hydrate-compiled/complex-trees.deeply-nested-dynamic-content/output.txt +1 -0
  29. package/runtime/__test__/element-template/fixtures/background/instance/ops/emits-bundle-url-in-create-template/output.txt +1 -0
  30. package/runtime/__test__/element-template/fixtures/list/mts/index.tsx +185 -0
  31. package/runtime/__test__/element-template/fixtures/page/render-page/native-log.txt +1 -0
  32. package/runtime/__test__/element-template/fixtures/patch/detaches-node-when-moving-between-slots/case.tsx +2 -0
  33. package/runtime/__test__/element-template/fixtures/patch-compiled/applies-insert-before-with-reference/ops.txt +1 -0
  34. package/runtime/__test__/element-template/fixtures/patch-compiled/apply-hydration-ops/ops.txt +1 -0
  35. package/runtime/__test__/element-template/fixtures/render/child-siblings/papi.txt +10 -5
  36. package/runtime/__test__/element-template/fixtures/render/component/papi.txt +6 -3
  37. package/runtime/__test__/element-template/fixtures/render/component-slot-content/papi.txt +6 -3
  38. package/runtime/__test__/element-template/fixtures/render/css-scope-dynamic-bundle-url/papi.txt +2 -1
  39. package/runtime/__test__/element-template/fixtures/render/direct-native/appends-root-text-via-append-element/native-log.txt +1 -0
  40. package/runtime/__test__/element-template/fixtures/render/direct-native/creates-template-from-attrs-and-slot-text/native-log.txt +2 -0
  41. package/runtime/__test__/element-template/fixtures/render/direct-native/handles-multiple-template-children-in-the-same-slot/native-log.txt +3 -0
  42. package/runtime/__test__/element-template/fixtures/render/direct-native/handles-multiple-text-nodes-in-the-same-slot/native-log.txt +3 -0
  43. package/runtime/__test__/element-template/fixtures/render/direct-native/inserts-nested-templates-into-parent-slots/native-log.txt +3 -0
  44. package/runtime/__test__/element-template/fixtures/render/direct-native/keeps-slot-children-separated-and-ordered/native-log.txt +3 -0
  45. package/runtime/__test__/element-template/fixtures/render/direct-native/passes-bundle-url-to-native-create/native-log.txt +1 -0
  46. package/runtime/__test__/element-template/fixtures/render/mapped-view-children/papi.txt +14 -7
  47. package/runtime/__test__/element-template/fixtures/render/mixed-children/papi.txt +8 -4
  48. package/runtime/__test__/element-template/fixtures/render/multiple-text/papi.txt +38 -19
  49. package/runtime/__test__/element-template/fixtures/render/nested-templates/papi.txt +6 -3
  50. package/runtime/__test__/element-template/fixtures/render/react-example/papi.txt +6 -3
  51. package/runtime/__test__/element-template/fixtures/render/sparse-element-slot/papi.txt +6 -3
  52. package/runtime/__test__/element-template/fixtures/render/template-dedupe/papi.txt +10 -5
  53. package/runtime/__test__/element-template/lynx/performance.test.ts +1 -0
  54. package/runtime/__test__/element-template/native/callDestroyLifetimeFun.test.ts +1 -0
  55. package/runtime/__test__/element-template/native/mts-destroy.test.ts +10 -15
  56. package/runtime/__test__/element-template/native/reload.test.ts +4 -7
  57. package/runtime/__test__/element-template/runtime/background/adapter/background-adapter.et.test.tsx +0 -32
  58. package/runtime/__test__/element-template/runtime/background/commit-context.test.ts +0 -14
  59. package/runtime/__test__/element-template/runtime/background/commit-hook.test.ts +10 -0
  60. package/runtime/__test__/element-template/runtime/background/event/compiled-fixtures.test.tsx +22 -0
  61. package/runtime/__test__/element-template/runtime/background/hydrate.test.ts +197 -252
  62. package/runtime/__test__/element-template/runtime/background/instance.test.ts +277 -126
  63. package/runtime/__test__/element-template/runtime/background/passive-update-depth.test.tsx +496 -0
  64. package/runtime/__test__/element-template/runtime/background/render/preact.test.tsx +16 -22
  65. package/runtime/__test__/element-template/runtime/background/root-render.test.tsx +5 -3
  66. package/runtime/__test__/element-template/runtime/background/suspense.test.tsx +2 -0
  67. package/runtime/__test__/element-template/runtime/background/update/compiled-fixtures.test.tsx +1 -0
  68. package/runtime/__test__/element-template/runtime/background/update-timing.test.ts +1 -0
  69. package/runtime/__test__/element-template/runtime/hydration/hydration-listener.test.ts +3 -4
  70. package/runtime/__test__/element-template/runtime/list/list-mts-compiled.test.ts +525 -0
  71. package/runtime/__test__/element-template/runtime/page/compiled-fixtures.test.tsx +2 -0
  72. package/runtime/__test__/element-template/runtime/page/root-instance.test.ts +2 -14
  73. package/runtime/__test__/element-template/runtime/patch/element-template-patch.test.tsx +425 -1032
  74. package/runtime/__test__/element-template/runtime/patch/patch-listener-alog.test.ts +1 -0
  75. package/runtime/__test__/element-template/runtime/patch/update-timing.test.ts +1 -0
  76. package/runtime/__test__/element-template/runtime/prop-adapters/event.test.ts +0 -20
  77. package/runtime/__test__/element-template/runtime/prop-adapters/gesture.test.ts +2 -1
  78. package/runtime/__test__/element-template/runtime/prop-adapters/hydrate.test.ts +1 -0
  79. package/runtime/__test__/element-template/runtime/prop-adapters/ref.test.ts +65 -0
  80. package/runtime/__test__/element-template/runtime/prop-adapters/spread-mts-compiled.test.ts +12 -4
  81. package/runtime/__test__/element-template/runtime/render/render-components.test.jsx +2 -5
  82. package/runtime/__test__/element-template/runtime/render/render-direct-native.test.tsx +56 -121
  83. package/runtime/__test__/element-template/runtime/render/render-direct-suspense.test.ts +18 -17
  84. package/runtime/__test__/element-template/runtime/render/render-direct.test.ts +5 -4
  85. package/runtime/__test__/element-template/runtime/render/render-main-thread.contract.test.ts +2 -0
  86. package/runtime/__test__/element-template/runtime/render/render-main-thread.test.ts +1 -60
  87. package/runtime/__test__/element-template/runtime/render/render-transform.contract.test.ts +32 -89
  88. package/runtime/__test__/element-template/runtime/template/element-template-handle.test.ts +8 -6
  89. package/runtime/__test__/element-template/runtime/template/main-thread-dynamic-attr-state.test.ts +34 -6
  90. package/runtime/__test__/element-template/test-utils/debug/updateRunner.test.tsx +20 -12
  91. package/runtime/__test__/element-template/test-utils/debug/updateRunner.ts +2 -28
  92. package/runtime/__test__/element-template/test-utils/mock/mockNativePapi/templateTree.test.ts +14 -5
  93. package/runtime/__test__/element-template/test-utils/mock/mockNativePapi/templateTree.ts +1 -57
  94. package/runtime/__test__/element-template/test-utils/mock/mockNativePapi.ts +29 -13
  95. package/runtime/__test__/element-template/vitest.config.ts +1 -0
  96. package/runtime/__test__/guardrails/snapshot-containment.test.ts +98 -77
  97. package/runtime/__test__/snapshot/compat/export.test.jsx +8 -2
  98. package/runtime/__test__/snapshot/debug/passive-update-depth.test.jsx +549 -0
  99. package/runtime/__test__/snapshot/debug/react-hooks-profile.test.jsx +3 -3
  100. package/runtime/__test__/snapshot/lifecycle/reload-background-function.test.jsx +138 -1
  101. package/runtime/__test__/snapshot/lifecycle/reload-delayed-background.test.jsx +1 -1
  102. package/runtime/__test__/snapshot/lifecycle/transfer.mts.test.jsx +194 -0
  103. package/runtime/__test__/snapshot/lifecycle/transfer.test.jsx +210 -0
  104. package/runtime/__test__/snapshot/list.test.jsx +109 -0
  105. package/runtime/__test__/snapshot/lynxQueueMicrotask.test.jsx +2 -2
  106. package/runtime/__test__/snapshot/ref.test.jsx +124 -1
  107. package/runtime/__test__/snapshot/utils/envManager.ts +5 -0
  108. package/runtime/__test__/snapshot/utils/globals.js +1 -0
  109. package/runtime/__test__/worklet-runtime/delayWorkletEvent.test.js +22 -11
  110. package/runtime/__test__/worklet-runtime/hydrate.test.js +1 -0
  111. package/runtime/__test__/worklet-runtime/initEntry.test.js +12 -0
  112. package/runtime/__test__/worklet-runtime/jsFunctionLifecycle.test.js +24 -16
  113. package/runtime/__test__/worklet-runtime/runOnBackground.test.js +188 -0
  114. package/runtime/__test__/worklet-runtime/workletRef.test.js +16 -0
  115. package/runtime/__test__/worklet-runtime/workletRuntime.test.js +53 -1
  116. package/runtime/compat/index.d.ts +17 -1
  117. package/runtime/compat/index.js +3 -2
  118. package/runtime/lazy/compat.js +1 -0
  119. package/runtime/lazy/internal.js +1 -0
  120. package/runtime/lib/core/attribute-name.js.map +1 -0
  121. package/runtime/lib/core/background-function/run-on-background.js +7 -7
  122. package/runtime/lib/core/background-function/run-on-background.js.map +1 -1
  123. package/runtime/lib/{shared → core}/component-stack.js +1 -1
  124. package/runtime/lib/core/component-stack.js.map +1 -0
  125. package/runtime/lib/core/deep-equal.d.ts +1 -0
  126. package/runtime/lib/core/deep-equal.js +31 -0
  127. package/runtime/lib/core/deep-equal.js.map +1 -0
  128. package/runtime/lib/core/hooks/mainThreadImpl.js +1 -1
  129. package/runtime/lib/core/hooks/mainThreadImpl.js.map +1 -1
  130. package/runtime/lib/core/lynx-page-data.js +1 -1
  131. package/runtime/lib/core/lynx-page-data.js.map +1 -1
  132. package/runtime/lib/core/mts-capability.js +1 -1
  133. package/runtime/lib/core/mts-capability.js.map +1 -1
  134. package/runtime/lib/core/performance.js +2 -1
  135. package/runtime/lib/core/performance.js.map +1 -1
  136. package/runtime/lib/core/ref.d.ts +1 -2
  137. package/runtime/lib/core/ref.js +60 -9
  138. package/runtime/lib/core/ref.js.map +1 -1
  139. package/runtime/lib/core/sync-effect-flush.d.ts +9 -0
  140. package/runtime/lib/core/sync-effect-flush.js +31 -0
  141. package/runtime/lib/core/sync-effect-flush.js.map +1 -0
  142. package/runtime/lib/core/thread-function-call/main-thread.js +1 -1
  143. package/runtime/lib/core/thread-function-call/main-thread.js.map +1 -1
  144. package/runtime/lib/element-template/background/attr-slots.d.ts +1 -0
  145. package/runtime/lib/element-template/background/attr-slots.js.map +1 -1
  146. package/runtime/lib/element-template/background/commit-hook.js +1 -1
  147. package/runtime/lib/element-template/background/commit-hook.js.map +1 -1
  148. package/runtime/lib/element-template/background/destroy.js +1 -1
  149. package/runtime/lib/element-template/background/destroy.js.map +1 -1
  150. package/runtime/lib/element-template/background/hydrate.js +173 -85
  151. package/runtime/lib/element-template/background/hydrate.js.map +1 -1
  152. package/runtime/lib/element-template/background/instance.d.ts +7 -10
  153. package/runtime/lib/element-template/background/instance.js +65 -58
  154. package/runtime/lib/element-template/background/instance.js.map +1 -1
  155. package/runtime/lib/element-template/background/render-scope.js +2 -1
  156. package/runtime/lib/element-template/background/render-scope.js.map +1 -1
  157. package/runtime/lib/element-template/debug/alog.d.ts +1 -10
  158. package/runtime/lib/element-template/debug/alog.js +1 -16
  159. package/runtime/lib/element-template/debug/alog.js.map +1 -1
  160. package/runtime/lib/element-template/debug/profile.js +1 -1
  161. package/runtime/lib/element-template/debug/profile.js.map +1 -1
  162. package/runtime/lib/element-template/native/index.js +2 -2
  163. package/runtime/lib/element-template/native/index.js.map +1 -1
  164. package/runtime/lib/element-template/native/patch-listener.js +4 -8
  165. package/runtime/lib/element-template/native/patch-listener.js.map +1 -1
  166. package/runtime/lib/element-template/native/reload-main-thread.js +0 -4
  167. package/runtime/lib/element-template/native/reload-main-thread.js.map +1 -1
  168. package/runtime/lib/element-template/prop-adapters/ref.d.ts +2 -1
  169. package/runtime/lib/element-template/prop-adapters/ref.js +4 -4
  170. package/runtime/lib/element-template/prop-adapters/ref.js.map +1 -1
  171. package/runtime/lib/element-template/prop-adapters/spread.js +1 -1
  172. package/runtime/lib/element-template/prop-adapters/spread.js.map +1 -1
  173. package/runtime/lib/element-template/protocol/opcodes.d.ts +0 -2
  174. package/runtime/lib/element-template/protocol/opcodes.js +0 -2
  175. package/runtime/lib/element-template/protocol/opcodes.js.map +1 -1
  176. package/runtime/lib/element-template/protocol/types.d.ts +8 -30
  177. package/runtime/lib/element-template/protocol/types.js.map +1 -1
  178. package/runtime/lib/element-template/runtime/list/list.d.ts +14 -54
  179. package/runtime/lib/element-template/runtime/list/list.js +82 -546
  180. package/runtime/lib/element-template/runtime/list/list.js.map +1 -1
  181. package/runtime/lib/element-template/runtime/list/platform-info.d.ts +2 -0
  182. package/runtime/lib/element-template/runtime/list/platform-info.js +26 -0
  183. package/runtime/lib/element-template/runtime/list/platform-info.js.map +1 -0
  184. package/runtime/lib/element-template/runtime/patch.js +42 -119
  185. package/runtime/lib/element-template/runtime/patch.js.map +1 -1
  186. package/runtime/lib/element-template/runtime/render/create-rendered-host.d.ts +1 -1
  187. package/runtime/lib/element-template/runtime/render/create-rendered-host.js +24 -21
  188. package/runtime/lib/element-template/runtime/render/create-rendered-host.js.map +1 -1
  189. package/runtime/lib/element-template/runtime/render/render-components.d.ts +1 -1
  190. package/runtime/lib/element-template/runtime/render/render-components.js +3 -6
  191. package/runtime/lib/element-template/runtime/render/render-components.js.map +1 -1
  192. package/runtime/lib/element-template/runtime/render/render-direct.js +21 -20
  193. package/runtime/lib/element-template/runtime/render/render-direct.js.map +1 -1
  194. package/runtime/lib/element-template/runtime/render/render-main-thread.js +0 -13
  195. package/runtime/lib/element-template/runtime/render/render-main-thread.js.map +1 -1
  196. package/runtime/lib/element-template/runtime/template/attr-slot-plan.js +8 -0
  197. package/runtime/lib/element-template/runtime/template/attr-slot-plan.js.map +1 -1
  198. package/runtime/lib/element-template/runtime/template/handle.d.ts +3 -3
  199. package/runtime/lib/element-template/runtime/template/handle.js +2 -2
  200. package/runtime/lib/element-template/runtime/template/handle.js.map +1 -1
  201. package/runtime/lib/element-template/runtime/template/main-thread-background-function.js +1 -6
  202. package/runtime/lib/element-template/runtime/template/main-thread-background-function.js.map +1 -1
  203. package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.d.ts +2 -1
  204. package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.js +44 -28
  205. package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.js.map +1 -1
  206. package/runtime/lib/element-template/runtime/template/typed-attributes.d.ts +2 -0
  207. package/runtime/lib/element-template/runtime/template/typed-attributes.js +8 -1
  208. package/runtime/lib/element-template/runtime/template/typed-attributes.js.map +1 -1
  209. package/runtime/lib/internal.d.ts +1 -0
  210. package/runtime/lib/internal.js +1 -0
  211. package/runtime/lib/internal.js.map +1 -1
  212. package/runtime/lib/lynx-api.js +6 -1
  213. package/runtime/lib/lynx-api.js.map +1 -1
  214. package/runtime/lib/lynx.js +4 -4
  215. package/runtime/lib/lynx.js.map +1 -1
  216. package/runtime/lib/root.d.ts +5 -1
  217. package/runtime/lib/root.js +12 -1
  218. package/runtime/lib/root.js.map +1 -1
  219. package/runtime/lib/shared/microtask.d.ts +1 -0
  220. package/runtime/lib/shared/microtask.js +28 -0
  221. package/runtime/lib/shared/microtask.js.map +1 -0
  222. package/runtime/lib/shared/utils.d.ts +8 -0
  223. package/runtime/lib/shared/utils.js +27 -0
  224. package/runtime/lib/shared/utils.js.map +1 -0
  225. package/runtime/lib/{worklet-runtime/utils → shared}/version.d.ts +1 -0
  226. package/runtime/lib/shared/version.js +15 -0
  227. package/runtime/lib/shared/version.js.map +1 -0
  228. package/runtime/lib/snapshot/alog/render.js +1 -1
  229. package/runtime/lib/snapshot/alog/render.js.map +1 -1
  230. package/runtime/lib/snapshot/debug/profileHooks.js +1 -1
  231. package/runtime/lib/snapshot/debug/profileHooks.js.map +1 -1
  232. package/runtime/lib/snapshot/lifecycle/destroy.js +2 -2
  233. package/runtime/lib/snapshot/lifecycle/destroy.js.map +1 -1
  234. package/runtime/lib/snapshot/lifecycle/isRendering.js +2 -1
  235. package/runtime/lib/snapshot/lifecycle/isRendering.js.map +1 -1
  236. package/runtime/lib/snapshot/lifecycle/patch/commit.d.ts +7 -2
  237. package/runtime/lib/snapshot/lifecycle/patch/commit.js +11 -6
  238. package/runtime/lib/snapshot/lifecycle/patch/commit.js.map +1 -1
  239. package/runtime/lib/snapshot/lifecycle/patch/updateMainThread.d.ts +5 -0
  240. package/runtime/lib/snapshot/lifecycle/patch/updateMainThread.js +1 -1
  241. package/runtime/lib/snapshot/lifecycle/patch/updateMainThread.js.map +1 -1
  242. package/runtime/lib/snapshot/lifecycle/ref/delay.d.ts +1 -1
  243. package/runtime/lib/snapshot/lifecycle/ref/delay.js +2 -2
  244. package/runtime/lib/snapshot/lifecycle/ref/delay.js.map +1 -1
  245. package/runtime/lib/snapshot/lifecycle/reload.js +1 -1
  246. package/runtime/lib/snapshot/lifecycle/reload.js.map +1 -1
  247. package/runtime/lib/snapshot/list/list.js +12 -3
  248. package/runtime/lib/snapshot/list/list.js.map +1 -1
  249. package/runtime/lib/snapshot/lynx/tt.d.ts +3 -1
  250. package/runtime/lib/snapshot/lynx/tt.js +7 -3
  251. package/runtime/lib/snapshot/lynx/tt.js.map +1 -1
  252. package/runtime/lib/snapshot/renderToOpcodes/hydrate.js +1 -1
  253. package/runtime/lib/snapshot/renderToOpcodes/hydrate.js.map +1 -1
  254. package/runtime/lib/snapshot/snapshot/backgroundSnapshot.js +1 -1
  255. package/runtime/lib/snapshot/snapshot/backgroundSnapshot.js.map +1 -1
  256. package/runtime/lib/snapshot/snapshot/platformInfo.js +1 -1
  257. package/runtime/lib/snapshot/snapshot/platformInfo.js.map +1 -1
  258. package/runtime/lib/snapshot/snapshot/ref.d.ts +1 -0
  259. package/runtime/lib/snapshot/snapshot/ref.js +2 -2
  260. package/runtime/lib/snapshot/snapshot/ref.js.map +1 -1
  261. package/runtime/lib/snapshot/snapshot/snapshot.js +1 -1
  262. package/runtime/lib/snapshot/snapshot/snapshot.js.map +1 -1
  263. package/runtime/lib/snapshot/snapshot/spread.js +3 -2
  264. package/runtime/lib/snapshot/snapshot/spread.js.map +1 -1
  265. package/runtime/lib/snapshot/transfer.d.ts +15 -0
  266. package/runtime/lib/snapshot/transfer.js +343 -0
  267. package/runtime/lib/snapshot/transfer.js.map +1 -0
  268. package/runtime/lib/snapshot/worklet/functionality.js +1 -1
  269. package/runtime/lib/snapshot/worklet/functionality.js.map +1 -1
  270. package/runtime/lib/tsconfig.tsbuildinfo +1 -1
  271. package/runtime/lib/worklet-runtime/api/element.js +1 -1
  272. package/runtime/lib/worklet-runtime/api/element.js.map +1 -1
  273. package/runtime/lib/worklet-runtime/api/lynxApi.js +1 -1
  274. package/runtime/lib/worklet-runtime/api/lynxApi.js.map +1 -1
  275. package/runtime/lib/worklet-runtime/bindings/bindings.d.ts +3 -2
  276. package/runtime/lib/worklet-runtime/bindings/bindings.js +11 -1
  277. package/runtime/lib/worklet-runtime/bindings/bindings.js.map +1 -1
  278. package/runtime/lib/worklet-runtime/bindings/observers.js +9 -1
  279. package/runtime/lib/worklet-runtime/bindings/observers.js.map +1 -1
  280. package/runtime/lib/worklet-runtime/bindings/types.d.ts +1 -0
  281. package/runtime/lib/worklet-runtime/bindings/types.js.map +1 -1
  282. package/runtime/lib/worklet-runtime/delayWorkletEvent.d.ts +2 -4
  283. package/runtime/lib/worklet-runtime/delayWorkletEvent.js +42 -44
  284. package/runtime/lib/worklet-runtime/delayWorkletEvent.js.map +1 -1
  285. package/runtime/lib/worklet-runtime/global.d.ts +1 -2
  286. package/runtime/lib/worklet-runtime/global.js.map +1 -1
  287. package/runtime/lib/worklet-runtime/hydrate.js +2 -32
  288. package/runtime/lib/worklet-runtime/hydrate.js.map +1 -1
  289. package/runtime/lib/worklet-runtime/run-on-background/index.d.ts +28 -0
  290. package/runtime/lib/worklet-runtime/run-on-background/index.js +103 -0
  291. package/runtime/lib/worklet-runtime/run-on-background/index.js.map +1 -0
  292. package/runtime/lib/worklet-runtime/{jsFunctionLifecycle.d.ts → run-on-background/lifecycle.d.ts} +2 -2
  293. package/runtime/lib/worklet-runtime/{jsFunctionLifecycle.js → run-on-background/lifecycle.js} +12 -9
  294. package/runtime/lib/worklet-runtime/run-on-background/lifecycle.js.map +1 -0
  295. package/runtime/lib/worklet-runtime/workletRef.js +51 -54
  296. package/runtime/lib/worklet-runtime/workletRef.js.map +1 -1
  297. package/runtime/lib/worklet-runtime/workletRuntime.js +5 -6
  298. package/runtime/lib/worklet-runtime/workletRuntime.js.map +1 -1
  299. package/runtime/src/core/background-function/run-on-background.ts +7 -7
  300. package/runtime/src/{shared → core}/component-stack.ts +1 -1
  301. package/runtime/src/core/deep-equal.ts +33 -0
  302. package/runtime/src/core/hooks/mainThreadImpl.ts +1 -1
  303. package/runtime/src/core/lynx-page-data.ts +1 -1
  304. package/runtime/src/core/mts-capability.ts +1 -1
  305. package/runtime/src/core/performance.ts +2 -1
  306. package/runtime/src/core/ref.ts +92 -15
  307. package/runtime/src/core/sync-effect-flush.ts +30 -0
  308. package/runtime/src/core/thread-function-call/main-thread.ts +1 -1
  309. package/runtime/src/element-template/background/attr-slots.ts +2 -2
  310. package/runtime/src/element-template/background/commit-hook.ts +1 -1
  311. package/runtime/src/element-template/background/destroy.ts +1 -1
  312. package/runtime/src/element-template/background/hydrate.ts +229 -119
  313. package/runtime/src/element-template/background/instance.ts +82 -82
  314. package/runtime/src/element-template/background/render-scope.ts +2 -1
  315. package/runtime/src/element-template/debug/alog.ts +2 -30
  316. package/runtime/src/element-template/debug/profile.ts +1 -1
  317. package/runtime/src/element-template/native/index.ts +2 -2
  318. package/runtime/src/element-template/native/patch-listener.ts +6 -7
  319. package/runtime/src/element-template/native/reload-main-thread.ts +0 -4
  320. package/runtime/src/element-template/prop-adapters/ref.ts +12 -5
  321. package/runtime/src/element-template/prop-adapters/spread.ts +1 -1
  322. package/runtime/src/element-template/protocol/opcodes.ts +0 -2
  323. package/runtime/src/element-template/protocol/types.ts +10 -38
  324. package/runtime/src/element-template/runtime/list/list.ts +115 -744
  325. package/runtime/src/element-template/runtime/list/platform-info.ts +31 -0
  326. package/runtime/src/element-template/runtime/patch.ts +59 -163
  327. package/runtime/src/element-template/runtime/render/create-rendered-host.ts +27 -31
  328. package/runtime/src/element-template/runtime/render/render-components.ts +2 -5
  329. package/runtime/src/element-template/runtime/render/render-direct.ts +19 -18
  330. package/runtime/src/element-template/runtime/render/render-main-thread.ts +0 -14
  331. package/runtime/src/element-template/runtime/template/attr-slot-plan.ts +9 -0
  332. package/runtime/src/element-template/runtime/template/handle.ts +3 -2
  333. package/runtime/src/element-template/runtime/template/main-thread-background-function.ts +1 -6
  334. package/runtime/src/element-template/runtime/template/main-thread-dynamic-attr-state.ts +44 -30
  335. package/runtime/src/element-template/runtime/template/typed-attributes.ts +17 -1
  336. package/runtime/src/element-template/types.d.ts +6 -22
  337. package/runtime/src/internal.ts +2 -0
  338. package/runtime/src/lynx-api.ts +6 -1
  339. package/runtime/src/lynx.ts +4 -4
  340. package/runtime/src/root.ts +17 -1
  341. package/runtime/src/shared/microtask.ts +26 -0
  342. package/runtime/src/shared/utils.ts +37 -0
  343. package/runtime/src/shared/version.ts +16 -0
  344. package/runtime/src/snapshot/alog/render.ts +1 -1
  345. package/runtime/src/snapshot/debug/profileHooks.ts +1 -1
  346. package/runtime/src/snapshot/lifecycle/destroy.ts +2 -2
  347. package/runtime/src/snapshot/lifecycle/isRendering.ts +2 -1
  348. package/runtime/src/snapshot/lifecycle/patch/commit.ts +28 -7
  349. package/runtime/src/snapshot/lifecycle/patch/updateMainThread.ts +1 -1
  350. package/runtime/src/snapshot/lifecycle/ref/delay.ts +5 -2
  351. package/runtime/src/snapshot/lifecycle/reload.ts +1 -1
  352. package/runtime/src/snapshot/list/list.ts +12 -3
  353. package/runtime/src/snapshot/lynx/tt.ts +8 -3
  354. package/runtime/src/snapshot/renderToOpcodes/hydrate.ts +1 -1
  355. package/runtime/src/snapshot/snapshot/backgroundSnapshot.ts +1 -1
  356. package/runtime/src/snapshot/snapshot/platformInfo.ts +1 -1
  357. package/runtime/src/snapshot/snapshot/ref.ts +7 -4
  358. package/runtime/src/snapshot/snapshot/snapshot.ts +1 -1
  359. package/runtime/src/snapshot/snapshot/spread.ts +3 -2
  360. package/runtime/src/snapshot/transfer.ts +379 -0
  361. package/runtime/src/snapshot/worklet/functionality.ts +1 -1
  362. package/runtime/src/worklet-runtime/api/element.ts +1 -1
  363. package/runtime/src/worklet-runtime/api/lynxApi.ts +1 -1
  364. package/runtime/src/worklet-runtime/bindings/bindings.ts +12 -0
  365. package/runtime/src/worklet-runtime/bindings/observers.ts +9 -1
  366. package/runtime/src/worklet-runtime/bindings/types.ts +1 -0
  367. package/runtime/src/worklet-runtime/delayWorkletEvent.ts +49 -54
  368. package/runtime/src/worklet-runtime/global.ts +1 -2
  369. package/runtime/src/worklet-runtime/hydrate.ts +2 -35
  370. package/runtime/src/worklet-runtime/run-on-background/index.ts +145 -0
  371. package/runtime/src/worklet-runtime/{jsFunctionLifecycle.ts → run-on-background/lifecycle.ts} +11 -9
  372. package/runtime/src/worklet-runtime/workletRef.ts +63 -62
  373. package/runtime/src/worklet-runtime/workletRuntime.ts +10 -10
  374. package/testing-library/.turbo/turbo-build.log +10 -10
  375. package/testing-library/dist/pure.js +7 -4
  376. package/testing-library/dist/setupFiles/common/runtime-setup.js +3 -1
  377. package/testing-library/dist/setupFiles/inner/rstest.js +1 -1
  378. package/testing-library/dist/setupFiles/inner/vitest.js +1 -1
  379. package/testing-library/src/__tests__/list.test.jsx +52 -0
  380. package/testing-library/src/__tests__/worklet.test.jsx +81 -0
  381. package/testing-library/src/pure.jsx +5 -2
  382. package/testing-library/src/setupFiles/common/runtime-setup.js +2 -0
  383. package/transform/.turbo/turbo-build$colon$wasm.log +88 -201
  384. package/transform/crates/swc_plugin_element_template/tests/element_template.rs +19 -45
  385. package/transform/crates/swc_plugin_element_template/tests/element_template_contract.rs +0 -19
  386. package/transform/rspack-napi/tsconfig.tsbuildinfo +1 -1
  387. package/transform/swc-plugin-reactlynx/.turbo/turbo-build.log +129 -156
  388. package/transform/swc-plugin-reactlynx-compat/.turbo/turbo-build.log +282 -149
  389. package/runtime/__test__/element-template/fixtures/background/hydrate/coverage.move-before-child/output.txt +0 -10
  390. package/runtime/__test__/element-template/fixtures/background/hydrate/coverage.raw-text-key-branches/case.tsx +0 -5
  391. package/runtime/__test__/element-template/fixtures/background/instance/ops/supports-silent-insert-before/case.ts +0 -11
  392. package/runtime/__test__/element-template/fixtures/background/instance/ops/supports-silent-insert-before/output.txt +0 -1
  393. package/runtime/__test__/element-template/fixtures/background/instance/ops/treats-nullish-attrs-as-empty-object/case.ts +0 -23
  394. package/runtime/__test__/element-template/fixtures/background/instance/ops/treats-nullish-attrs-as-empty-object/output.txt +0 -6
  395. package/runtime/__test__/element-template/runtime/page/authored-page.test.ts +0 -35
  396. package/runtime/__test__/element-template/runtime/patch/update-fixture-helper.test.tsx +0 -40
  397. package/runtime/__test__/snapshot/lynx-data-processors.test.ts +0 -3
  398. package/runtime/lib/shared/attribute-name.js.map +0 -1
  399. package/runtime/lib/shared/component-stack.js.map +0 -1
  400. package/runtime/lib/snapshot/alog/index.d.ts +0 -1
  401. package/runtime/lib/snapshot/alog/index.js +0 -8
  402. package/runtime/lib/snapshot/alog/index.js.map +0 -1
  403. package/runtime/lib/utils.d.ts +0 -19
  404. package/runtime/lib/utils.js +0 -119
  405. package/runtime/lib/utils.js.map +0 -1
  406. package/runtime/lib/worklet-runtime/delayRunOnBackground.d.ts +0 -13
  407. package/runtime/lib/worklet-runtime/delayRunOnBackground.js +0 -29
  408. package/runtime/lib/worklet-runtime/delayRunOnBackground.js.map +0 -1
  409. package/runtime/lib/worklet-runtime/jsFunctionLifecycle.js.map +0 -1
  410. package/runtime/lib/worklet-runtime/utils/version.js +0 -10
  411. package/runtime/lib/worklet-runtime/utils/version.js.map +0 -1
  412. package/runtime/src/snapshot/alog/index.ts +0 -8
  413. package/runtime/src/utils.ts +0 -134
  414. package/runtime/src/worklet-runtime/delayRunOnBackground.ts +0 -53
  415. package/runtime/src/worklet-runtime/utils/version.ts +0 -11
  416. /package/runtime/__test__/element-template/fixtures/background/hydrate/{coverage.raw-text-key-branches → children.reconciles-raw-text-slots}/output.txt +0 -0
  417. /package/runtime/lib/{shared → core}/attribute-name.d.ts +0 -0
  418. /package/runtime/lib/{shared → core}/attribute-name.js +0 -0
  419. /package/runtime/lib/{shared → core}/component-stack.d.ts +0 -0
  420. /package/runtime/src/{shared → core}/attribute-name.ts +0 -0
@@ -1,9 +1,8 @@
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 { WorkletEvents } from './bindings/events.js';
5
- import { profile } from './utils/profile.js';
6
- import { isSdkVersionGt } from './utils/version.js';
4
+ import { WorkletEvents } from '../bindings/events.js';
5
+ import { profile } from '../utils/profile.js';
7
6
  /**
8
7
  * `JsFunctionLifecycleManager` monitors references to JS function handles to be called by `runOnBackground()`.
9
8
  * In JS context, functions to be called by `runOnBackground()` is referenced by `JsFnHandle`s and finally by `execId`.
@@ -27,6 +26,7 @@ class JsFunctionLifecycleManager {
27
26
  // A failed engine registration must leave the target eligible for a retry.
28
27
  this.registry.register(objToRef, execId);
29
28
  this.retainedObjects.add(objToRef);
29
+ this.execIdSetToFire.delete(execId);
30
30
  this.execIdRefCount.set(execId, (this.execIdRefCount.get(execId) ?? 0) + 1);
31
31
  }
32
32
  removeRef(execId) {
@@ -47,21 +47,24 @@ class JsFunctionLifecycleManager {
47
47
  dispose() {
48
48
  this.active = false;
49
49
  }
50
+ recreate() {
51
+ this.dispose();
52
+ return new JsFunctionLifecycleManager();
53
+ }
50
54
  fire() {
51
55
  if (!this.active)
52
56
  return;
57
+ this.willFire = false;
58
+ if (this.execIdSetToFire.size === 0)
59
+ return;
53
60
  profile('JsFunctionLifecycleManager.fire', () => {
54
61
  lynx.getJSContext().dispatchEvent({
55
62
  type: WorkletEvents.releaseBackgroundWorkletCtx,
56
63
  data: Array.from(this.execIdSetToFire),
57
64
  });
58
65
  this.execIdSetToFire.clear();
59
- this.willFire = false;
60
66
  });
61
67
  }
62
68
  }
63
- function isRunOnBackgroundEnabled() {
64
- return isSdkVersionGt(2, 15);
65
- }
66
- export { JsFunctionLifecycleManager, isRunOnBackgroundEnabled };
67
- //# sourceMappingURL=jsFunctionLifecycle.js.map
69
+ export { JsFunctionLifecycleManager };
70
+ //# sourceMappingURL=lifecycle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lifecycle.js","sourceRoot":"","sources":["../../../src/worklet-runtime/run-on-background/lifecycle.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,yEAAyE;AACzE,0DAA0D;AAC1D,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AACtD,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAE9C;;;;;GAKG;AACH,MAAM,0BAA0B;IACtB,cAAc,GAAG,IAAI,GAAG,EAAkB,CAAC;IAC3C,eAAe,GAAG,IAAI,GAAG,EAAU,CAA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AAK,OAAO,CAAC,OAAO,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE;gBAC/B,IAAI,CAAC,IAAI,EAAE,CAAC;YACd,CAAC,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IAED,OAAO;QACL,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACtB,CAAC;IAED,QAAQ;QACN,IAAI,CAAC,OAAO,EAAE,CAAC;QACf,OAAO,IAAI,0BAA0B,EAAE,CAAC;IAC1C,CAAC;IAED,IAAI;QACF,IAAI,CAAC,IAAI,CAAC,MAAM;YAAE,OAAO;QACzB,IAAI,CAAC,QAAQ,GAAG,KAAK,CAAC;QACtB,IAAI,IAAI,CAAC,eAAe,CAAC,IAAI,KAAK,CAAC;YAAE,OAAO;QAC5C,OAAO,CAAC,iCAAiC,EAAE,GAAG,EAAE;YAC9C,IAAI,CAAC,YAAY,EAAE,CAAC,aAAa,CAAC;gBAChC,IAAI,EAAE,aAAa,CAAC,2BAA2B;gBAC/C,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC;aACvC,CAAC,CAAC;YACH,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,CAAC;QAC/B,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAED,OAAO,EAAE,0BAA0B,EAAE,CAAC","sourcesContent":["// Copyright 2024 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\nimport { WorkletEvents } from '../bindings/events.js';\nimport { profile } from '../utils/profile.js';\n\n/**\n * `JsFunctionLifecycleManager` monitors references to JS function handles to be called by `runOnBackground()`.\n * In JS context, functions to be called by `runOnBackground()` is referenced by `JsFnHandle`s and finally by `execId`.\n * When all `JsFnHandle`s in lepus are released, an event will be sent to JS context to de-ref the `execId`,\n * resulting a de-ref to the js function in JS context.\n */\nclass JsFunctionLifecycleManager {\n private execIdRefCount = new Map<number, number>();\n private execIdSetToFire = new Set<number>();\n private retainedObjects = new WeakSet<object>();\n private willFire = false;\n private active = true;\n private registry: FinalizationRegistry<number>;\n\n constructor() {\n this.registry = new FinalizationRegistry<number>(this.removeRef.bind(this));\n }\n\n addRef(execId: number, objToRef: object): void {\n if (this.retainedObjects.has(objToRef)) {\n return;\n }\n // A failed engine registration must leave the target eligible for a retry.\n this.registry.register(objToRef, execId);\n this.retainedObjects.add(objToRef);\n this.execIdSetToFire.delete(execId);\n this.execIdRefCount.set(\n execId,\n (this.execIdRefCount.get(execId) ?? 0) + 1,\n );\n }\n\n removeRef(execId: number): void {\n const rc = this.execIdRefCount.get(execId)!;\n if (rc > 1) {\n this.execIdRefCount.set(execId, rc - 1);\n return;\n }\n this.execIdRefCount.delete(execId);\n this.execIdSetToFire.add(execId);\n if (!this.willFire) {\n this.willFire = true;\n void Promise.resolve().then(() => {\n this.fire();\n });\n }\n }\n\n dispose(): void {\n this.active = false;\n }\n\n recreate(): JsFunctionLifecycleManager {\n this.dispose();\n return new JsFunctionLifecycleManager();\n }\n\n fire(): void {\n if (!this.active) return;\n this.willFire = false;\n if (this.execIdSetToFire.size === 0) return;\n profile('JsFunctionLifecycleManager.fire', () => {\n lynx.getJSContext().dispatchEvent({\n type: WorkletEvents.releaseBackgroundWorkletCtx,\n data: Array.from(this.execIdSetToFire),\n });\n this.execIdSetToFire.clear();\n });\n }\n}\n\nexport { JsFunctionLifecycleManager };\n"]}
@@ -5,10 +5,9 @@ import { Element } from './api/element.js';
5
5
  import { assertCompatibleMainThreadObject, createMainThreadObject, initMainThreadObjects, isRealizedMainThreadObject, registerMainThreadObjectType, } from './mainThreadObject.js';
6
6
  import { mainThreadFlushLoopMark } from './utils/mainThreadFlushLoopGuard.js';
7
7
  import { profile } from './utils/profile.js';
8
- let impl;
9
8
  function initWorkletRef() {
10
9
  initMainThreadObjects();
11
- return (impl = {
10
+ const instance = {
12
11
  _workletRefMap: {},
13
12
  /**
14
13
  * Map of worklet refs that are created during first screen rendering.
@@ -17,11 +16,48 @@ function initWorkletRef() {
17
16
  * to free up memory.
18
17
  */
19
18
  _firstScreenWorkletRefMap: {},
20
- updateWorkletRef,
21
- updateWorkletRefInitValueChanges,
19
+ updateWorkletRef(handle, element) {
20
+ getFromWorkletRefMapOn(instance, handle).current = element
21
+ ? new Element(element)
22
+ : null;
23
+ },
24
+ updateWorkletRefInitValueChanges(patch) {
25
+ profile('updateWorkletRefInitValueChanges', () => {
26
+ let firstError;
27
+ let hasError = false;
28
+ patch.forEach(([id, value, type]) => {
29
+ try {
30
+ const handle = {
31
+ _wvid: id,
32
+ _initValue: value,
33
+ _type: type,
34
+ };
35
+ const existing = instance._workletRefMap[id];
36
+ if (existing) {
37
+ assertCompatibleWorkletValue(handle, existing, 'initialization patch');
38
+ }
39
+ else {
40
+ instance._workletRefMap[id] = createWorkletValue(handle);
41
+ }
42
+ }
43
+ catch (error) {
44
+ if (!hasError) {
45
+ firstError = error;
46
+ hasError = true;
47
+ }
48
+ }
49
+ });
50
+ if (hasError) {
51
+ throw firstError;
52
+ }
53
+ });
54
+ },
22
55
  registerMainThreadObjectType,
23
- clearFirstScreenWorkletRefMap,
24
- });
56
+ clearFirstScreenWorkletRefMap() {
57
+ instance._firstScreenWorkletRefMap = {};
58
+ },
59
+ };
60
+ return instance;
25
61
  }
26
62
  const createWorkletRef = (id, value) => {
27
63
  const ref = {
@@ -66,7 +102,7 @@ function assertCompatibleWorkletValue(handle, value, operation) {
66
102
  assertCompatibleMainThreadObject(handle, value, operation);
67
103
  }
68
104
  }
69
- const getFromWorkletRefMap = (refImpl) => {
105
+ function getFromWorkletRefMapOn(refImplState, refImpl) {
70
106
  const id = refImpl._wvid;
71
107
  /* v8 ignore next 3 */
72
108
  if (__DEV__) {
@@ -78,68 +114,29 @@ const getFromWorkletRefMap = (refImpl) => {
78
114
  // Might be called in two scenarios:
79
115
  // 1. In MTS events
80
116
  // 2. In `main-thread:ref`
81
- value = impl._firstScreenWorkletRefMap[id] ??= createWorkletValue(refImpl);
117
+ value = refImplState._firstScreenWorkletRefMap[id] ??= createWorkletValue(refImpl);
82
118
  }
83
119
  else {
84
- value = impl._workletRefMap[id];
120
+ value = refImplState._workletRefMap[id];
85
121
  }
86
122
  /* v8 ignore next 3 */
87
123
  if (__DEV__ && value === undefined) {
88
124
  throw new Error('MainThreadRef is not initialized: ' + id);
89
125
  }
90
126
  return value;
127
+ }
128
+ const getFromWorkletRefMap = (refImpl) => {
129
+ return getFromWorkletRefMapOn(lynxWorkletImpl._refImpl, refImpl);
91
130
  };
92
131
  function removeValueFromWorkletRefMap(id) {
93
- delete impl._workletRefMap[id];
132
+ delete lynxWorkletImpl._refImpl._workletRefMap[id];
94
133
  }
95
134
  function hydrateWorkletValue(handle, value) {
96
135
  assertCompatibleWorkletValue(handle, value, 'hydration');
97
- impl._workletRefMap[handle._wvid] = value;
98
- }
99
- /**
100
- * Create an element instance of the given element node, then set the worklet value to it.
101
- * This is called in `snapshotContextUpdateWorkletRef`.
102
- * @param handle handle of the worklet value.
103
- * @param element the element node.
104
- */
105
- function updateWorkletRef(handle, element) {
106
- getFromWorkletRefMap(handle).current = element
107
- ? new Element(element)
108
- : null;
136
+ lynxWorkletImpl._refImpl._workletRefMap[handle._wvid] = value;
109
137
  }
110
138
  function updateWorkletRefInitValueChanges(patch) {
111
- profile('updateWorkletRefInitValueChanges', () => {
112
- let firstError;
113
- let hasError = false;
114
- patch.forEach(([id, value, type]) => {
115
- try {
116
- const handle = {
117
- _wvid: id,
118
- _initValue: value,
119
- _type: type,
120
- };
121
- const existing = impl._workletRefMap[id];
122
- if (existing) {
123
- assertCompatibleWorkletValue(handle, existing, 'initialization patch');
124
- }
125
- else {
126
- impl._workletRefMap[id] = createWorkletValue(handle);
127
- }
128
- }
129
- catch (error) {
130
- if (!hasError) {
131
- firstError = error;
132
- hasError = true;
133
- }
134
- }
135
- });
136
- if (hasError) {
137
- throw firstError;
138
- }
139
- });
140
- }
141
- function clearFirstScreenWorkletRefMap() {
142
- impl._firstScreenWorkletRefMap = {};
139
+ lynxWorkletImpl._refImpl.updateWorkletRefInitValueChanges(patch);
143
140
  }
144
141
  export { createWorkletRef, initWorkletRef, getFromWorkletRefMap, removeValueFromWorkletRefMap, hydrateWorkletValue, isHydratedWorkletValue, updateWorkletRefInitValueChanges, };
145
142
  //# sourceMappingURL=workletRef.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"workletRef.js","sourceRoot":"","sources":["../../src/worklet-runtime/workletRef.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,yEAAyE;AACzE,0DAA0D;AAC1D,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAQ3C,OAAO,EACL,gCAAgC,EAChC,sBAAsB,EACtB,qBAAqB,EACrB,0BAA0B,EAC1B,4BAA4B,GAC7B,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EAAE,uBAAuB,EAAE,MAAM,qCAAqC,CAAC;AAC9E,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAmB7C,IAAI,IAAyB,CAAC;AAC9B,SAAS,cAAc;IACrB,qBAAqB,EAAE,CAAC;IACxB,OAAO,CAAC,IAAI,GAAG;QACb,cAAc,EAAE,EAAE;QAClB;;;;;WAKG;QACH,yBAAyB,EAAE,EAAE;QAC7B,gBAAgB;QAChB,gCAAgC;QAChC,4BAA4B;QAC5B,6BAA6B;KAC9B,CAAC,CAAC;AACL,CAAC;AAED,MAAM,gBAAgB,GAAG,CACvB,EAAgB,EAChB,KAAQ,EACO,EAAE;IACjB,MAAM,GAAG,GAAG;QACV,OAAO,EAAE,KAAK;QACd,KAAK,EAAE,EAAE;KACV,CAAC;IACF,OAAO,GAAG,CAAC;AACb,CAAC,CAAC;AAEF,SAAS,kBAAkB,CAAC,OAAgC;IAC1D,OAAO,CAAC,OAAO,CAAC,KAAK,IAAI,OAAO,CAAC,KAAK,KAAK,aAAa;QACtD,CAAC,CAAC,gBAAgB,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,UAAU,CAAC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Content":["// Copyright 2024 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\nimport { Element } from './api/element.js';\nimport type {\n MainThreadRefInitValuePatch,\n Worklet,\n WorkletRef,\n WorkletRefId,\n WorkletRefImpl,\n} from './bindings/types.js';\nimport {\n assertCompatibleMainThreadObject,\n createMainThreadObject,\n initMainThreadObjects,\n isRealizedMainThreadObject,\n registerMainThreadObjectType,\n} from './mainThreadObject.js';\nimport type { MainThreadObjectFactory } from './mainThreadObject.js';\nimport { mainThreadFlushLoopMark } from './utils/mainThreadFlushLoopGuard.js';\nimport { profile } from './utils/profile.js';\n\ninterface RefImpl {\n _workletRefMap: Record<WorkletRefId, object>;\n _firstScreenWorkletRefMap: Record<WorkletRefId, object>;\n updateWorkletRef(\n refImpl: WorkletRefImpl<Element | null>,\n element: ElementNode | null,\n ): void;\n updateWorkletRefInitValueChanges(\n patch: MainThreadRefInitValuePatch,\n ): void;\n registerMainThreadObjectType(\n type: string,\n create: MainThreadObjectFactory | Worklet,\n ): void;\n clearFirstScreenWorkletRefMap(): void;\n}\n\nlet impl: RefImpl | undefined;\nfunction initWorkletRef(): RefImpl {\n initMainThreadObjects();\n return (impl = {\n _workletRefMap: {},\n /**\n * Map of worklet refs that are created during first screen rendering.\n * These refs are created with negative IDs and need to be hydrated\n * when the app starts. The map is cleared after hydration is complete\n * to free up memory.\n */\n _firstScreenWorkletRefMap: {},\n updateWorkletRef,\n updateWorkletRefInitValueChanges,\n registerMainThreadObjectType,\n clearFirstScreenWorkletRefMap,\n });\n}\n\nconst createWorkletRef = <T>(\n id: WorkletRefId,\n value: T,\n): WorkletRef<T> => {\n const ref = {\n current: value,\n _wvid: id,\n };\n return ref;\n};\n\nfunction createWorkletValue(refImpl: WorkletRefImpl<unknown>): object {\n return !refImpl._type || refImpl._type === 'main-thread'\n ? createWorkletRef(refImpl._wvid, refImpl._initValue)\n : createMainThreadObject(refImpl);\n}\n\nfunction isMutableCell(value: unknown): value is WorkletRef<unknown> {\n return typeof value === 'object' && value !== null\n && typeof (value as Partial<WorkletRef<unknown>>)._wvid === 'number'\n && Object.prototype.hasOwnProperty.call(value, 'current');\n}\n\nfunction isHydratedWorkletValue(value: unknown): value is object {\n return typeof value === 'object' && value !== null\n && (isRealizedMainThreadObject(value) || isMutableCell(value));\n}\n\nfunction assertCompatibleWorkletValue(\n handle: WorkletRefImpl<unknown>,\n value: object,\n operation: 'hydration' | 'initialization patch',\n): void {\n let actualKind: 'typed-object' | 'mutable-cell' | undefined;\n if (isRealizedMainThreadObject(value)) {\n actualKind = 'typed-object';\n } else if (isMutableCell(value)) {\n actualKind = 'mutable-cell';\n }\n if (!actualKind) {\n throw new Error(\n `Cannot apply MainThreadObject ${operation} for handle ${handle._wvid}: the existing target has no worklet-value metadata.`,\n );\n }\n\n const expectedType = handle._type;\n const expectedKind = !expectedType || expectedType === 'main-thread'\n ? 'mutable-cell'\n : 'typed-object';\n if (actualKind !== expectedKind) {\n throw new Error(\n `Worklet value kind mismatch during ${operation} for handle ${handle._wvid}: background handle expects ${expectedKind}, but the main-thread target is ${actualKind}.`,\n );\n }\n if (actualKind === 'typed-object') {\n assertCompatibleMainThreadObject(handle, value, operation);\n }\n}\n\nconst getFromWorkletRefMap = (\n refImpl: WorkletRefImpl<unknown>,\n): object | undefined => {\n const id = refImpl._wvid;\n /* v8 ignore next 3 */\n if (__DEV__) {\n mainThreadFlushLoopMark(`MainThreadRef:get id=${id}`);\n }\n let value;\n if (id < 0) {\n // At the first screen rendering, the worklet ref is created with a negative ID.\n // Might be called in two scenarios:\n // 1. In MTS events\n // 2. In `main-thread:ref`\n value = impl!._firstScreenWorkletRefMap[id] ??= createWorkletValue(refImpl);\n } else {\n value = impl!._workletRefMap[id];\n }\n\n /* v8 ignore next 3 */\n if (__DEV__ && value === undefined) {\n throw new Error('MainThreadRef is not initialized: ' + id);\n }\n return value;\n};\n\nfunction removeValueFromWorkletRefMap(id: WorkletRefId): void {\n delete impl!._workletRefMap[id];\n}\n\nfunction hydrateWorkletValue(\n handle: WorkletRefImpl<unknown>,\n value: object,\n): void {\n assertCompatibleWorkletValue(handle, value, 'hydration');\n impl!._workletRefMap[handle._wvid] = value;\n}\n\n/**\n * Create an element instance of the given element node, then set the worklet value to it.\n * This is called in `snapshotContextUpdateWorkletRef`.\n * @param handle handle of the worklet value.\n * @param element the element node.\n */\nfunction updateWorkletRef(\n handle: WorkletRefImpl<Element | null>,\n element: ElementNode | null,\n): void {\n (getFromWorkletRefMap(handle) as WorkletRef<Element | null>).current = element\n ? new Element(element)\n : null;\n}\n\nfunction updateWorkletRefInitValueChanges(\n patch: MainThreadRefInitValuePatch,\n): void {\n profile('updateWorkletRefInitValueChanges', () => {\n let firstError: unknown;\n let hasError = false;\n patch.forEach(([id, value, type]) => {\n try {\n const handle = {\n _wvid: id,\n _initValue: value,\n _type: type,\n } as WorkletRefImpl<unknown>;\n const existing = impl!._workletRefMap[id];\n if (existing) {\n assertCompatibleWorkletValue(\n handle,\n existing,\n 'initialization patch',\n );\n } else {\n impl!._workletRefMap[id] = createWorkletValue(handle);\n }\n } catch (error) {\n if (!hasError) {\n firstError = error;\n hasError = true;\n }\n }\n });\n if (hasError) {\n throw firstError;\n }\n });\n}\n\nfunction clearFirstScreenWorkletRefMap(): void {\n impl!._firstScreenWorkletRefMap = {};\n}\n\nexport {\n type RefImpl,\n createWorkletRef,\n initWorkletRef,\n getFromWorkletRefMap,\n removeValueFromWorkletRefMap,\n hydrateWorkletValue,\n isHydratedWorkletValue,\n updateWorkletRefInitValueChanges,\n};\n"]}
1
+ {"version":3,"file":"workletRef.js","sourceRoot":"","sources":["../../src/worklet-runtime/workletRef.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,yEAAyE;AACzE,0DAA0D;AAC1D,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAQ3C,OAAO,EACL,gCAAgC,EAChC,sBAAsB,EACtB,qBAAqB,EACrB,0BAA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AAC,OAAO,CAAC,CAAC;IACrF,CAAC;SAAM,CAAC;QACN,KAAK,GAAG,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC,CAAC;IAC1C,CAAC;IAED,sBAAsB;IACtB,IAAI,OAAO,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QACnC,MAAM,IAAI,KAAK,CAAC,oCAAoC,GAAG,EAAE,CAAC,CAAC;IAC7D,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED,MAAM,oBAAoB,GAAG,CAC3B,OAAgC,EACZ,EAAE;IACtB,OAAO,sBAAsB,CAAC,eAAe,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;AACnE,CAAC,CAAC;AAEF,SAAS,4BAA4B,CAAC,EAAgB;IACpD,OAAO,eAAe,CAAC,QAAQ,CAAC,cAAc,CAAC,EAAE,CAAC,CAAC;AACrD,CAAC;AAED,SAAS,mBAAmB,CAC1B,MAA+B,EAC/B,KAAa;IAEb,4BAA4B,CAAC,MAAM,EAAE,KAAK,EAAE,WAAW,CAAC,CAAC;IACzD,eAAe,CAAC,QAAQ,CAAC,cAAc,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC;AAChE,CAAC;AAED,SAAS,gCAAgC,CACvC,KAAkC;IAElC,eAAe,CAAC,QAAQ,CAAC,gCAAgC,CAAC,KAAK,CAAC,CAAC;AACnE,CAAC;AAED,OAAO,EAEL,gBAAgB,EAChB,cAAc,EACd,oBAAoB,EACpB,4BAA4B,EAC5B,mBAAmB,EACnB,sBAAsB,EACtB,gCAAgC,GACjC,CAAC","sourcesContent":["// Copyright 2024 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\nimport { Element } from './api/element.js';\nimport type {\n MainThreadRefInitValuePatch,\n Worklet,\n WorkletRef,\n WorkletRefId,\n WorkletRefImpl,\n} from './bindings/types.js';\nimport {\n assertCompatibleMainThreadObject,\n createMainThreadObject,\n initMainThreadObjects,\n isRealizedMainThreadObject,\n registerMainThreadObjectType,\n} from './mainThreadObject.js';\nimport type { MainThreadObjectFactory } from './mainThreadObject.js';\nimport { mainThreadFlushLoopMark } from './utils/mainThreadFlushLoopGuard.js';\nimport { profile } from './utils/profile.js';\n\ninterface RefImpl {\n _workletRefMap: Record<WorkletRefId, object>;\n _firstScreenWorkletRefMap: Record<WorkletRefId, object>;\n updateWorkletRef(\n refImpl: WorkletRefImpl<Element | null>,\n element: ElementNode | null,\n ): void;\n updateWorkletRefInitValueChanges(\n patch: MainThreadRefInitValuePatch,\n ): void;\n registerMainThreadObjectType(\n type: string,\n create: MainThreadObjectFactory | Worklet,\n ): void;\n clearFirstScreenWorkletRefMap(): void;\n}\n\nfunction initWorkletRef(): RefImpl {\n initMainThreadObjects();\n const instance: RefImpl = {\n _workletRefMap: {},\n /**\n * Map of worklet refs that are created during first screen rendering.\n * These refs are created with negative IDs and need to be hydrated\n * when the app starts. The map is cleared after hydration is complete\n * to free up memory.\n */\n _firstScreenWorkletRefMap: {},\n updateWorkletRef(\n handle: WorkletRefImpl<Element | null>,\n element: ElementNode | null,\n ): void {\n (getFromWorkletRefMapOn(instance, handle) as WorkletRef<Element | null>).current = element\n ? new Element(element)\n : null;\n },\n updateWorkletRefInitValueChanges(\n patch: MainThreadRefInitValuePatch,\n ): void {\n profile('updateWorkletRefInitValueChanges', () => {\n let firstError: unknown;\n let hasError = false;\n patch.forEach(([id, value, type]) => {\n try {\n const handle = {\n _wvid: id,\n _initValue: value,\n _type: type,\n } as WorkletRefImpl<unknown>;\n const existing = instance._workletRefMap[id];\n if (existing) {\n assertCompatibleWorkletValue(\n handle,\n existing,\n 'initialization patch',\n );\n } else {\n instance._workletRefMap[id] = createWorkletValue(handle);\n }\n } catch (error) {\n if (!hasError) {\n firstError = error;\n hasError = true;\n }\n }\n });\n if (hasError) {\n throw firstError;\n }\n });\n },\n registerMainThreadObjectType,\n clearFirstScreenWorkletRefMap(): void {\n instance._firstScreenWorkletRefMap = {};\n },\n };\n return instance;\n}\n\nconst createWorkletRef = <T>(\n id: WorkletRefId,\n value: T,\n): WorkletRef<T> => {\n const ref = {\n current: value,\n _wvid: id,\n };\n return ref;\n};\n\nfunction createWorkletValue(refImpl: WorkletRefImpl<unknown>): object {\n return !refImpl._type || refImpl._type === 'main-thread'\n ? createWorkletRef(refImpl._wvid, refImpl._initValue)\n : createMainThreadObject(refImpl);\n}\n\nfunction isMutableCell(value: unknown): value is WorkletRef<unknown> {\n return typeof value === 'object' && value !== null\n && typeof (value as Partial<WorkletRef<unknown>>)._wvid === 'number'\n && Object.prototype.hasOwnProperty.call(value, 'current');\n}\n\nfunction isHydratedWorkletValue(value: unknown): value is object {\n return typeof value === 'object' && value !== null\n && (isRealizedMainThreadObject(value) || isMutableCell(value));\n}\n\nfunction assertCompatibleWorkletValue(\n handle: WorkletRefImpl<unknown>,\n value: object,\n operation: 'hydration' | 'initialization patch',\n): void {\n let actualKind: 'typed-object' | 'mutable-cell' | undefined;\n if (isRealizedMainThreadObject(value)) {\n actualKind = 'typed-object';\n } else if (isMutableCell(value)) {\n actualKind = 'mutable-cell';\n }\n if (!actualKind) {\n throw new Error(\n `Cannot apply MainThreadObject ${operation} for handle ${handle._wvid}: the existing target has no worklet-value metadata.`,\n );\n }\n\n const expectedType = handle._type;\n const expectedKind = !expectedType || expectedType === 'main-thread'\n ? 'mutable-cell'\n : 'typed-object';\n if (actualKind !== expectedKind) {\n throw new Error(\n `Worklet value kind mismatch during ${operation} for handle ${handle._wvid}: background handle expects ${expectedKind}, but the main-thread target is ${actualKind}.`,\n );\n }\n if (actualKind === 'typed-object') {\n assertCompatibleMainThreadObject(handle, value, operation);\n }\n}\n\nfunction getFromWorkletRefMapOn(\n refImplState: RefImpl,\n refImpl: WorkletRefImpl<unknown>,\n): object | undefined {\n const id = refImpl._wvid;\n /* v8 ignore next 3 */\n if (__DEV__) {\n mainThreadFlushLoopMark(`MainThreadRef:get id=${id}`);\n }\n let value;\n if (id < 0) {\n // At the first screen rendering, the worklet ref is created with a negative ID.\n // Might be called in two scenarios:\n // 1. In MTS events\n // 2. In `main-thread:ref`\n value = refImplState._firstScreenWorkletRefMap[id] ??= createWorkletValue(refImpl);\n } else {\n value = refImplState._workletRefMap[id];\n }\n\n /* v8 ignore next 3 */\n if (__DEV__ && value === undefined) {\n throw new Error('MainThreadRef is not initialized: ' + id);\n }\n return value;\n}\n\nconst getFromWorkletRefMap = (\n refImpl: WorkletRefImpl<unknown>,\n): object | undefined => {\n return getFromWorkletRefMapOn(lynxWorkletImpl._refImpl, refImpl);\n};\n\nfunction removeValueFromWorkletRefMap(id: WorkletRefId): void {\n delete lynxWorkletImpl._refImpl._workletRefMap[id];\n}\n\nfunction hydrateWorkletValue(\n handle: WorkletRefImpl<unknown>,\n value: object,\n): void {\n assertCompatibleWorkletValue(handle, value, 'hydration');\n lynxWorkletImpl._refImpl._workletRefMap[handle._wvid] = value;\n}\n\nfunction updateWorkletRefInitValueChanges(\n patch: MainThreadRefInitValuePatch,\n): void {\n lynxWorkletImpl._refImpl.updateWorkletRefInitValueChanges(patch);\n}\n\nexport {\n type RefImpl,\n createWorkletRef,\n initWorkletRef,\n getFromWorkletRefMap,\n removeValueFromWorkletRefMap,\n hydrateWorkletValue,\n isHydratedWorkletValue,\n updateWorkletRefInitValueChanges,\n};\n"]}
@@ -1,15 +1,15 @@
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 { isRunOnBackgroundEnabled } from '../shared/version.js';
4
5
  import { Element } from './api/element.js';
5
6
  import { RunWorkletSource } from './bindings/types.js';
6
- import { initRunOnBackgroundDelay } from './delayRunOnBackground.js';
7
- import { delayExecUntilJsReady, initEventDelay } from './delayWorkletEvent.js';
7
+ import { initEventDelay } from './delayWorkletEvent.js';
8
8
  import { initEomImpl } from './eomImpl.js';
9
9
  import { addEventMethodsIfNeeded } from './eventPropagation.js';
10
10
  import { hydrateCtx } from './hydrate.js';
11
- import { JsFunctionLifecycleManager, isRunOnBackgroundEnabled } from './jsFunctionLifecycle.js';
12
11
  import { isRealizedMainThreadObject } from './mainThreadObject.js';
12
+ import { JsFunctionLifecycleManager, bindCapturedJsFnHandle, initRunOnBackgroundDelay, } from './run-on-background/index.js';
13
13
  import { runRunOnMainThreadTask } from './runOnMainThread.js';
14
14
  import { mainThreadFlushLoopMark } from './utils/mainThreadFlushLoopGuard.js';
15
15
  import { profile } from './utils/profile.js';
@@ -65,7 +65,7 @@ function runWorklet(ctx, params, options) {
65
65
  mainThreadFlushLoopMark(`MainThreadFunction id=${String(ctx._wkltId)}`);
66
66
  }
67
67
  if ('_lepusWorkletHash' in ctx) {
68
- delayExecUntilJsReady(ctx._lepusWorkletHash, params);
68
+ lynxWorkletImpl._eventDelayImpl.delayExecUntilJsReady(ctx._lepusWorkletHash, params);
69
69
  return;
70
70
  }
71
71
  return runWorkletImpl(ctx, params, options);
@@ -170,8 +170,7 @@ const transformWorkletInner = (value, depth, ctx) => {
170
170
  }
171
171
  const isJsFn = '_jsFnId' in subObj;
172
172
  if (isJsFn) {
173
- subObj['_execId'] = ctx._execId;
174
- lynxWorkletImpl._jsFunctionLifecycleManager?.addRef(ctx._execId, subObj);
173
+ bindCapturedJsFnHandle(subObj, ctx);
175
174
  continue;
176
175
  }
177
176
  }
@@ -1 +1 @@
1
- {"version":3,"file":"workletRuntime.js","sourceRoot":"","sources":["../../src/worklet-runtime/workletRuntime.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,yEAAyE;AACzE,0DAA0D;AAC1D,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAE3C,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,EAAE,wBAAwB,EAAE,MAAM,2BAA2B,CAAC;AACrE,OAAO,EAAE,qBAAqB,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAC/E,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AM,CAAC,cAAc,CAAC,GAAG,CAAkB,CAAqC,EAClG,WAAW,CACwB,CAAC;AACxC,CAAC;AAED,SAAS,4BAA4B,CACnC,KAAa;IAEb,MAAM,UAAU,GAAG,KAAyC,CAAC;IAC7D,OAAO,OAAO,UAAU,CAAC,KAAK,KAAK,QAAQ;WACtC,OAAO,UAAU,CAAC,KAAK,KAAK,QAAQ;WACpC,UAAU,CAAC,KAAK,KAAK,aAAa,CAAC;AAC1C,CAAC;AACD,OAAO,EAAE,WAAW,EAAE,CAAC","sourcesContent":["// Copyright 2024 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\nimport { Element } from './api/element.js';\nimport type { ClosureValueType, RunWorkletOptions, Worklet, WorkletRefImpl } from './bindings/types.js';\nimport { RunWorkletSource } from './bindings/types.js';\nimport { initRunOnBackgroundDelay } from './delayRunOnBackground.js';\nimport { delayExecUntilJsReady, initEventDelay } from './delayWorkletEvent.js';\nimport { initEomImpl } from './eomImpl.js';\nimport { addEventMethodsIfNeeded } from './eventPropagation.js';\nimport { hydrateCtx } from './hydrate.js';\nimport { JsFunctionLifecycleManager, isRunOnBackgroundEnabled } from './jsFunctionLifecycle.js';\nimport { isRealizedMainThreadObject } from './mainThreadObject.js';\nimport { runRunOnMainThreadTask } from './runOnMainThread.js';\nimport { mainThreadFlushLoopMark } from './utils/mainThreadFlushLoopGuard.js';\nimport { profile } from './utils/profile.js';\nimport { getFromWorkletRefMap, initWorkletRef } from './workletRef.js';\n\nfunction initWorklet(): void {\n const previousManager = globalThis.lynxWorkletImpl?._jsFunctionLifecycleManager;\n previousManager?.dispose();\n globalThis.lynxWorkletImpl = {\n _workletMap: {},\n _refImpl: initWorkletRef(),\n _runOnBackgroundDelayImpl: initRunOnBackgroundDelay(),\n _hydrateCtx: hydrateCtx,\n _eventDelayImpl: initEventDelay(),\n _eomImpl: initEomImpl(),\n _runRunOnMainThreadTask: runRunOnMainThreadTask,\n };\n\n if (isRunOnBackgroundEnabled()) {\n globalThis.lynxWorkletImpl._jsFunctionLifecycleManager = new JsFunctionLifecycleManager();\n }\n\n globalThis.registerWorklet = registerWorklet;\n globalThis.registerWorkletInternal = registerWorklet;\n globalThis.runWorklet = runWorklet;\n}\n\n/**\n * Register a worklet function, allowing it to be executed by `runWorklet()`.\n * This is called in lepus.js.\n * @param _type worklet type, 'main-thread' or 'ui'\n * @param id worklet hash\n * @param worklet worklet function\n */\nfunction registerWorklet(_type: string, id: string, worklet: (...args: unknown[]) => unknown): void {\n lynxWorkletImpl._workletMap[id] = worklet;\n}\n\n/**\n * Entrance of all worklet calls.\n * Native event touch handler will call this function.\n * @param ctx worklet object.\n * @param params worklet params.\n * @param options run worklet options.\n */\nfunction runWorklet(ctx: Worklet, params: ClosureValueType[], options?: RunWorkletOptions): unknown {\n if (!validateWorklet(ctx)) {\n return;\n }\n\n if (__DEV__) {\n if (options?.source === RunWorkletSource.EVENT && Array.isArray(params)) {\n const first = params[0];\n const t = (first as { type?: unknown }).type;\n if (typeof t === 'string') {\n mainThreadFlushLoopMark(`event:${t}`);\n }\n }\n\n mainThreadFlushLoopMark(`MainThreadFunction id=${String(ctx._wkltId)}`);\n }\n\n if ('_lepusWorkletHash' in ctx) {\n delayExecUntilJsReady(ctx._lepusWorkletHash, params);\n return;\n }\n return runWorkletImpl(ctx, params, options);\n}\n\nfunction runWorkletImpl(ctx: Worklet, params: ClosureValueType[], options?: RunWorkletOptions): unknown {\n const worklet: (...args: unknown[]) => unknown = profile(\n 'transformWorkletCtx ' + ctx._wkltId,\n () => transformWorklet(ctx, true),\n );\n const params_: ClosureValueType[] = profile(\n 'transformWorkletParams',\n () => transformWorklet(params || [], false),\n );\n\n const [hasEventMethods, eventCtx] = addEventMethodsIfNeeded(params_, options);\n\n const result = profile('runWorklet', () => worklet(...params_));\n\n if (hasEventMethods) {\n return {\n returnValue: result,\n eventReturnResult: eventCtx._eventReturnResult,\n };\n }\n\n return result;\n}\n\nfunction validateWorklet(ctx: unknown): ctx is Worklet {\n return typeof ctx === 'object' && ctx !== null && ('_wkltId' in ctx || '_lepusWorkletHash' in ctx);\n}\n\nconst workletCache = /*#__PURE__*/ new WeakMap<object, ClosureValueType | ((...args: unknown[]) => unknown)>();\n\nfunction transformWorklet(ctx: Worklet, isWorklet: true): (...args: unknown[]) => unknown;\nfunction transformWorklet(\n ctx: ClosureValueType[],\n isWorklet: false,\n): ClosureValueType[];\n\nfunction transformWorklet(\n ctx: ClosureValueType,\n isWorklet: boolean,\n): ClosureValueType | ((...args: unknown[]) => unknown) {\n /* v8 ignore next 3 */\n if (typeof ctx !== 'object' || ctx === null) {\n return ctx;\n }\n\n if (isWorklet) {\n const res = workletCache.get(ctx);\n if (res) {\n return res;\n }\n }\n\n const worklet = transformWorkletInner({ main: ctx }, 0, ctx) as { main: ClosureValueType };\n\n if (isWorklet) {\n workletCache.set(ctx, worklet.main);\n }\n\n return worklet.main;\n}\n\nconst transformWorkletInner = (\n value: ClosureValueType,\n depth: number,\n ctx: unknown,\n): ClosureValueType => {\n const limit = 1000;\n if (++depth >= limit) {\n throw new Error('Depth of value exceeds limit of ' + limit + '.');\n }\n /* v8 ignore next 3 */\n if (typeof value !== 'object' || value === null) {\n return value;\n }\n let obj = value as Record<string, ClosureValueType>;\n\n for (const key in obj) {\n const subObj: ClosureValueType = obj[key];\n if (typeof subObj !== 'object' || subObj === null) {\n continue;\n }\n\n // A MainThreadObject initialization payload is user data. Resolve the\n // typed descriptor before walking it so payload properties that resemble\n // worklet metadata remain untouched. Legacy MainThreadRef descriptors retain\n // the existing recursive path.\n if (isRealizedMainThreadObject(subObj)) {\n continue;\n }\n if (isMainThreadObjectDescriptor(subObj)) {\n obj = replaceCapturedProperty(obj, value, key, getFromWorkletRefMap(subObj));\n continue;\n }\n\n if (/** isEventTarget */ 'elementRefptr' in subObj) {\n obj[key] = new Element(subObj.elementRefptr as ElementNode);\n continue;\n } else if (subObj instanceof Element) {\n continue;\n }\n\n const transformedSubObj = transformWorkletInner(subObj, depth, ctx) as Record<string, ClosureValueType>;\n\n const isWorkletRef = '_wvid' in (subObj as object);\n if (isWorkletRef) {\n obj[key] = getFromWorkletRefMap(\n subObj as unknown as WorkletRefImpl<unknown>,\n );\n continue;\n }\n const isWorklet = '_wkltId' in subObj;\n if (isWorklet) {\n const isRootWorklet = subObj === ctx;\n const boundCtx = { ...transformedSubObj };\n // Keep the original context collectible. PrimJS traces WeakMap values even\n // when their keys are otherwise unreachable, so the cached function must not point back to its key.\n const boundWorklet: ((...args: unknown[]) => unknown) & { boundCtx?: object } = lynxWorkletImpl\n ._workletMap[(subObj as Worklet)._wkltId]!\n .bind(boundCtx);\n if (transformedSubObj !== subObj && obj === value && obj !== ctx) obj = copyAccessorCapture(obj);\n obj = replaceCapturedProperty(obj, value, key, boundWorklet);\n if (!isRootWorklet) {\n // Hydration needs the same context that the function already owns through bind().\n // The original nested context can disappear after its parent replaces it with this function.\n boundWorklet.boundCtx = boundCtx;\n }\n continue;\n }\n if (transformedSubObj !== subObj) {\n // The root context is runtime-owned: hydration must see its captured copy.\n if (obj === value && obj !== ctx) obj = copyAccessorCapture(obj);\n obj = replaceCapturedProperty(obj, value, key, transformedSubObj);\n }\n const isJsFn = '_jsFnId' in subObj;\n if (isJsFn) {\n (subObj as Record<string, ClosureValueType>)['_execId'] = (ctx as Worklet)._execId;\n lynxWorkletImpl._jsFunctionLifecycleManager?.addRef(\n (ctx as Worklet)._execId!,\n subObj,\n );\n continue;\n }\n }\n return obj;\n};\n\nfunction replaceCapturedProperty(\n obj: Record<string, ClosureValueType>,\n source: object,\n key: string,\n value: ClosureValueType,\n): Record<string, ClosureValueType> {\n const descriptor = Object.getOwnPropertyDescriptor(obj, key);\n if (descriptor?.get) {\n // Getters can refresh handles or method receivers. Materialize the captured\n // value without consuming the source getter or invoking its setter.\n const capturedDescriptor = {\n value,\n writable: true,\n enumerable: descriptor.enumerable!,\n configurable: descriptor.configurable!,\n };\n if (obj === source || !descriptor.configurable) {\n // Replace before defining the copy, including non-configurable getters.\n const descriptors = Object.getOwnPropertyDescriptors(obj);\n descriptors[key] = capturedDescriptor;\n return copyAccessorCapture(obj, descriptors);\n }\n Object.defineProperty(obj, key, capturedDescriptor);\n } else {\n obj[key] = value;\n }\n return obj;\n}\n\nfunction copyAccessorCapture(\n obj: Record<string, ClosureValueType>,\n descriptors: PropertyDescriptorMap = Object.getOwnPropertyDescriptors(obj),\n): Record<string, ClosureValueType> {\n // Copy descriptors without evaluating getters a second time. Array ancestors\n // retain their array identity when a nested accessor requires a captured copy.\n return Object.defineProperties(\n Array.isArray(obj)\n ? []\n : Object.create(Object.getPrototypeOf(obj) as object | null) as Record<string, ClosureValueType>,\n descriptors,\n ) as Record<string, ClosureValueType>;\n}\n\nfunction isMainThreadObjectDescriptor(\n value: object,\n): value is WorkletRefImpl<unknown> {\n const descriptor = value as Partial<WorkletRefImpl<unknown>>;\n return typeof descriptor._wvid === 'number'\n && typeof descriptor._type === 'string'\n && descriptor._type !== 'main-thread';\n}\nexport { initWorklet };\n"]}
1
+ {"version":3,"file":"workletRuntime.js","sourceRoot":"","sources":["../../src/worklet-runtime/workletRuntime.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,yEAAyE;AACzE,0DAA0D;AAC1D,OAAO,EAAE,wBAAwB,EAAE,MAAM,sBAAsB,CAAC;AAChE,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAE3C,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AACxD,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAE,uBAAuB,EAAE,MAAM,uBAAuB,CAAC;AAChE,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,0BAA0B,EAAE,MAAM,uBAAuB,CAAC;AACnE,OAAO,EACL,0BAA0B,EAC1B,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1C,CAAC;AACD,OAAO,EAAE,WAAW,EAAE,CAAC","sourcesContent":["// Copyright 2024 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\nimport { isRunOnBackgroundEnabled } from '../shared/version.js';\nimport { Element } from './api/element.js';\nimport type { ClosureValueType, JsFnHandle, RunWorkletOptions, Worklet, WorkletRefImpl } from './bindings/types.js';\nimport { RunWorkletSource } from './bindings/types.js';\nimport { initEventDelay } from './delayWorkletEvent.js';\nimport { initEomImpl } from './eomImpl.js';\nimport { addEventMethodsIfNeeded } from './eventPropagation.js';\nimport { hydrateCtx } from './hydrate.js';\nimport { isRealizedMainThreadObject } from './mainThreadObject.js';\nimport {\n JsFunctionLifecycleManager,\n bindCapturedJsFnHandle,\n initRunOnBackgroundDelay,\n} from './run-on-background/index.js';\nimport { runRunOnMainThreadTask } from './runOnMainThread.js';\nimport { mainThreadFlushLoopMark } from './utils/mainThreadFlushLoopGuard.js';\nimport { profile } from './utils/profile.js';\nimport { getFromWorkletRefMap, initWorkletRef } from './workletRef.js';\n\nfunction initWorklet(): void {\n const previousManager = globalThis.lynxWorkletImpl?._jsFunctionLifecycleManager;\n previousManager?.dispose();\n globalThis.lynxWorkletImpl = {\n _workletMap: {},\n _refImpl: initWorkletRef(),\n _runOnBackgroundDelayImpl: initRunOnBackgroundDelay(),\n _hydrateCtx: hydrateCtx,\n _eventDelayImpl: initEventDelay(),\n _eomImpl: initEomImpl(),\n _runRunOnMainThreadTask: runRunOnMainThreadTask,\n };\n\n if (isRunOnBackgroundEnabled()) {\n globalThis.lynxWorkletImpl._jsFunctionLifecycleManager = new JsFunctionLifecycleManager();\n }\n\n globalThis.registerWorklet = registerWorklet;\n globalThis.registerWorkletInternal = registerWorklet;\n globalThis.runWorklet = runWorklet;\n}\n\n/**\n * Register a worklet function, allowing it to be executed by `runWorklet()`.\n * This is called in lepus.js.\n * @param _type worklet type, 'main-thread' or 'ui'\n * @param id worklet hash\n * @param worklet worklet function\n */\nfunction registerWorklet(_type: string, id: string, worklet: (...args: unknown[]) => unknown): void {\n lynxWorkletImpl._workletMap[id] = worklet;\n}\n\n/**\n * Entrance of all worklet calls.\n * Native event touch handler will call this function.\n * @param ctx worklet object.\n * @param params worklet params.\n * @param options run worklet options.\n */\nfunction runWorklet(ctx: Worklet, params: ClosureValueType[], options?: RunWorkletOptions): unknown {\n if (!validateWorklet(ctx)) {\n return;\n }\n\n if (__DEV__) {\n if (options?.source === RunWorkletSource.EVENT && Array.isArray(params)) {\n const first = params[0];\n const t = (first as { type?: unknown }).type;\n if (typeof t === 'string') {\n mainThreadFlushLoopMark(`event:${t}`);\n }\n }\n\n mainThreadFlushLoopMark(`MainThreadFunction id=${String(ctx._wkltId)}`);\n }\n\n if ('_lepusWorkletHash' in ctx) {\n lynxWorkletImpl._eventDelayImpl.delayExecUntilJsReady(ctx._lepusWorkletHash, params);\n return;\n }\n return runWorkletImpl(ctx, params, options);\n}\n\nfunction runWorkletImpl(ctx: Worklet, params: ClosureValueType[], options?: RunWorkletOptions): unknown {\n const worklet: (...args: unknown[]) => unknown = profile(\n 'transformWorkletCtx ' + ctx._wkltId,\n () => transformWorklet(ctx, true),\n );\n const params_: ClosureValueType[] = profile(\n 'transformWorkletParams',\n () => transformWorklet(params || [], false),\n );\n\n const [hasEventMethods, eventCtx] = addEventMethodsIfNeeded(params_, options);\n\n const result = profile('runWorklet', () => worklet(...params_));\n\n if (hasEventMethods) {\n return {\n returnValue: result,\n eventReturnResult: eventCtx._eventReturnResult,\n };\n }\n\n return result;\n}\n\nfunction validateWorklet(ctx: unknown): ctx is Worklet {\n return typeof ctx === 'object' && ctx !== null && ('_wkltId' in ctx || '_lepusWorkletHash' in ctx);\n}\n\nconst workletCache = /*#__PURE__*/ new WeakMap<object, ClosureValueType | ((...args: unknown[]) => unknown)>();\n\nfunction transformWorklet(ctx: Worklet, isWorklet: true): (...args: unknown[]) => unknown;\nfunction transformWorklet(\n ctx: ClosureValueType[],\n isWorklet: false,\n): ClosureValueType[];\n\nfunction transformWorklet(\n ctx: ClosureValueType,\n isWorklet: boolean,\n): ClosureValueType | ((...args: unknown[]) => unknown) {\n /* v8 ignore next 3 */\n if (typeof ctx !== 'object' || ctx === null) {\n return ctx;\n }\n\n if (isWorklet) {\n const res = workletCache.get(ctx);\n if (res) {\n return res;\n }\n }\n\n const worklet = transformWorkletInner({ main: ctx }, 0, ctx) as { main: ClosureValueType };\n\n if (isWorklet) {\n workletCache.set(ctx, worklet.main);\n }\n\n return worklet.main;\n}\n\nconst transformWorkletInner = (\n value: ClosureValueType,\n depth: number,\n ctx: unknown,\n): ClosureValueType => {\n const limit = 1000;\n if (++depth >= limit) {\n throw new Error('Depth of value exceeds limit of ' + limit + '.');\n }\n /* v8 ignore next 3 */\n if (typeof value !== 'object' || value === null) {\n return value;\n }\n let obj = value as Record<string, ClosureValueType>;\n\n for (const key in obj) {\n const subObj: ClosureValueType = obj[key];\n if (typeof subObj !== 'object' || subObj === null) {\n continue;\n }\n\n // A MainThreadObject initialization payload is user data. Resolve the\n // typed descriptor before walking it so payload properties that resemble\n // worklet metadata remain untouched. Legacy MainThreadRef descriptors retain\n // the existing recursive path.\n if (isRealizedMainThreadObject(subObj)) {\n continue;\n }\n if (isMainThreadObjectDescriptor(subObj)) {\n obj = replaceCapturedProperty(obj, value, key, getFromWorkletRefMap(subObj));\n continue;\n }\n\n if (/** isEventTarget */ 'elementRefptr' in subObj) {\n obj[key] = new Element(subObj.elementRefptr as ElementNode);\n continue;\n } else if (subObj instanceof Element) {\n continue;\n }\n\n const transformedSubObj = transformWorkletInner(subObj, depth, ctx) as Record<string, ClosureValueType>;\n\n const isWorkletRef = '_wvid' in (subObj as object);\n if (isWorkletRef) {\n obj[key] = getFromWorkletRefMap(\n subObj as unknown as WorkletRefImpl<unknown>,\n );\n continue;\n }\n const isWorklet = '_wkltId' in subObj;\n if (isWorklet) {\n const isRootWorklet = subObj === ctx;\n const boundCtx = { ...transformedSubObj };\n // Keep the original context collectible. PrimJS traces WeakMap values even\n // when their keys are otherwise unreachable, so the cached function must not point back to its key.\n const boundWorklet: ((...args: unknown[]) => unknown) & { boundCtx?: object } = lynxWorkletImpl\n ._workletMap[(subObj as Worklet)._wkltId]!\n .bind(boundCtx);\n if (transformedSubObj !== subObj && obj === value && obj !== ctx) obj = copyAccessorCapture(obj);\n obj = replaceCapturedProperty(obj, value, key, boundWorklet);\n if (!isRootWorklet) {\n // Hydration needs the same context that the function already owns through bind().\n // The original nested context can disappear after its parent replaces it with this function.\n boundWorklet.boundCtx = boundCtx;\n }\n continue;\n }\n if (transformedSubObj !== subObj) {\n // The root context is runtime-owned: hydration must see its captured copy.\n if (obj === value && obj !== ctx) obj = copyAccessorCapture(obj);\n obj = replaceCapturedProperty(obj, value, key, transformedSubObj);\n }\n const isJsFn = '_jsFnId' in subObj;\n if (isJsFn) {\n bindCapturedJsFnHandle(subObj as JsFnHandle, ctx as Worklet);\n continue;\n }\n }\n return obj;\n};\n\nfunction replaceCapturedProperty(\n obj: Record<string, ClosureValueType>,\n source: object,\n key: string,\n value: ClosureValueType,\n): Record<string, ClosureValueType> {\n const descriptor = Object.getOwnPropertyDescriptor(obj, key);\n if (descriptor?.get) {\n // Getters can refresh handles or method receivers. Materialize the captured\n // value without consuming the source getter or invoking its setter.\n const capturedDescriptor = {\n value,\n writable: true,\n enumerable: descriptor.enumerable!,\n configurable: descriptor.configurable!,\n };\n if (obj === source || !descriptor.configurable) {\n // Replace before defining the copy, including non-configurable getters.\n const descriptors = Object.getOwnPropertyDescriptors(obj);\n descriptors[key] = capturedDescriptor;\n return copyAccessorCapture(obj, descriptors);\n }\n Object.defineProperty(obj, key, capturedDescriptor);\n } else {\n obj[key] = value;\n }\n return obj;\n}\n\nfunction copyAccessorCapture(\n obj: Record<string, ClosureValueType>,\n descriptors: PropertyDescriptorMap = Object.getOwnPropertyDescriptors(obj),\n): Record<string, ClosureValueType> {\n // Copy descriptors without evaluating getters a second time. Array ancestors\n // retain their array identity when a nested accessor requires a captured copy.\n return Object.defineProperties(\n Array.isArray(obj)\n ? []\n : Object.create(Object.getPrototypeOf(obj) as object | null) as Record<string, ClosureValueType>,\n descriptors,\n ) as Record<string, ClosureValueType>;\n}\n\nfunction isMainThreadObjectDescriptor(\n value: object,\n): value is WorkletRefImpl<unknown> {\n const descriptor = value as Partial<WorkletRefImpl<unknown>>;\n return typeof descriptor._wvid === 'number'\n && typeof descriptor._type === 'string'\n && descriptor._type !== 'main-thread';\n}\nexport { initWorklet };\n"]}
@@ -3,8 +3,8 @@
3
3
  // LICENSE file in the root directory of this source tree.
4
4
 
5
5
  import { BackgroundFunctionExecMap } from './exec-map.js';
6
- import { isSdkVersionGt } from '../../utils.js';
7
- import { delayRunOnBackground } from '../../worklet-runtime/bindings/bindings.js';
6
+ import { isSdkVersionGt } from '../../shared/version.js';
7
+ import { delayRunOnBackground, shouldDelayRunOnBackground } from '../../worklet-runtime/bindings/bindings.js';
8
8
  import { WorkletEvents } from '../../worklet-runtime/bindings/events.js';
9
9
  import type { RunWorkletCtxRetData } from '../../worklet-runtime/bindings/events.js';
10
10
  import type { ClosureValueType, JsFnHandle, Worklet } from '../../worklet-runtime/bindings/types.js';
@@ -131,16 +131,15 @@ export function runOnBackground<R, Fn extends (...args: any[]) => R>(
131
131
  return async (...params: ClosureValueType[]): Promise<R> => {
132
132
  return new Promise((resolve) => {
133
133
  if (isRuntimeDestroyed()) return;
134
- const resolveId = onFunctionCall(resolve);
135
134
 
136
- if (obj._isFirstScreen) {
135
+ if (shouldDelayRunOnBackground(obj)) {
137
136
  delayRunOnBackground(obj, (fnId: number, execId: number) => {
138
- dispatchRunBackgroundFunctionEvent(fnId, params, execId, resolveId);
137
+ dispatchRunBackgroundFunctionEvent(fnId, params, execId, resolve);
139
138
  });
140
139
  return;
141
140
  }
142
141
 
143
- dispatchRunBackgroundFunctionEvent(obj._jsFnId!, params, obj._execId!, resolveId);
142
+ dispatchRunBackgroundFunctionEvent(obj._jsFnId!, params, obj._execId!, resolve);
144
143
  });
145
144
  };
146
145
  }
@@ -149,8 +148,9 @@ function dispatchRunBackgroundFunctionEvent(
149
148
  fnId: number,
150
149
  params: ClosureValueType[],
151
150
  execId: number,
152
- resolveId: number,
151
+ resolve: (value: any) => void,
153
152
  ): void {
153
+ const resolveId = onFunctionCall(resolve);
154
154
  dispatchReloadScopedEvent(lynx.getJSContext(), {
155
155
  type: WorkletEvents.runOnBackground,
156
156
  data: {
@@ -30,7 +30,7 @@ SOFTWARE.
30
30
  import { Fragment, options } from 'preact';
31
31
  import type { VNode } from 'preact';
32
32
 
33
- import { DIFF, DIFFED, RENDER, ROOT } from './render-constants.js';
33
+ import { DIFF, DIFFED, RENDER, ROOT } from '../shared/render-constants.js';
34
34
 
35
35
  interface PatchedVNode extends VNode {
36
36
  _owner?: PatchedVNode | null;
@@ -0,0 +1,33 @@
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 { getCurrentVNode, getOwnerStack } from './component-stack.js';
6
+
7
+ export function isDirectOrDeepEqual(a: unknown, b: unknown): boolean {
8
+ if (a === b) {
9
+ return true;
10
+ }
11
+ try {
12
+ if (
13
+ typeof a == 'object' && a !== null && typeof b == 'object' && b !== null
14
+ && JSON.stringify(a) === JSON.stringify(b)
15
+ ) {
16
+ return true;
17
+ }
18
+ } catch (error) {
19
+ if (__DEV__ && /circular|cyclic/i.test((error as Error).message)) {
20
+ // JavaScript engines give this different errors name and messages:
21
+ // PrimJS: "circular reference"
22
+ // JavaScriptCore: "JSON.stringify cannot serialize cyclic structures"
23
+ // V8: "Converting circular structure to JSON"
24
+ const vnode = getCurrentVNode();
25
+ if (vnode) {
26
+ const stack = getOwnerStack(vnode);
27
+ (error as Error).message += `\n\n${stack}`;
28
+ }
29
+ }
30
+ throw error;
31
+ }
32
+ return false;
33
+ }
@@ -35,7 +35,7 @@ import {
35
35
  VALUE,
36
36
  VNODE,
37
37
  } from '../../shared/render-constants.js';
38
- import { noop } from '../../utils.js';
38
+ import { noop } from '../../shared/utils.js';
39
39
 
40
40
  let currentIndex: number;
41
41
  let currentComponent: Component | null | undefined;
@@ -2,7 +2,7 @@
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 { RESET_WITH_INIT_DATA_IN_STATE_ERROR, hasWithInitDataInStateUsage } from './initData.js';
5
- import { isEmptyObject } from '../utils.js';
5
+ import { isEmptyObject } from '../shared/utils.js';
6
6
 
7
7
  let hasReportedResetWithInitDataInState = false;
8
8
 
@@ -2,7 +2,7 @@
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 { isSdkVersionGt } from '../utils.js';
5
+ import { isSdkVersionGt } from '../shared/version.js';
6
6
 
7
7
  let mtsEnabled: boolean | undefined;
8
8
 
@@ -4,7 +4,8 @@
4
4
  import { options } from 'preact';
5
5
 
6
6
  import { RENDER_COMPONENT, ROOT } from '../shared/render-constants.js';
7
- import { hook, isSdkVersionGt } from '../utils.js';
7
+ import { hook } from '../shared/utils.js';
8
+ import { isSdkVersionGt } from '../shared/version.js';
8
9
 
9
10
  const PerformanceTimingKeys = [
10
11
  'updateSetStateTrigger',