@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,110 @@
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 type { DataProcessorDefinition } from '../lynx-api.js';
5
+
6
+ export function setupLynxEnv(): void {
7
+ if (!__LEPUS__) {
8
+ const { initData, updateData } = lynxCoreInject.tt._params;
9
+ // @ts-ignore
10
+ lynx.__initData = { ...initData, ...updateData };
11
+ lynx.registerDataProcessors = function() {};
12
+ }
13
+
14
+ if (__LEPUS__) {
15
+ // @ts-ignore
16
+ lynx.__initData = {
17
+ /* available only in renderPage */
18
+ };
19
+ // @ts-ignore
20
+ globalThis.SystemInfo = lynx.SystemInfo || {};
21
+
22
+ lynx.reportError = function(e: any) {
23
+ _ReportError(e, {
24
+ errorCode: 1101, // ErrCode::LYNX_ERROR_CODE_LEPUS in Lynx/base/debug/error_code.h
25
+ });
26
+ };
27
+
28
+ lynx.triggerGlobalEventFromLepus = function(
29
+ eventName: string,
30
+ params: any,
31
+ ) {
32
+ __OnLifecycleEvent(['globalEventFromLepus', [eventName, params]]);
33
+ };
34
+
35
+ {
36
+ function __name(empty: string) {
37
+ return `Native${empty}Modules`;
38
+ }
39
+ // TODO(hongzhiyuan.hzy): make sure this is run before any other code (especially code access `NativeModules`)
40
+ // @ts-ignore
41
+ if (typeof globalThis[__name('')] === 'undefined') {
42
+ // @ts-ignore
43
+ globalThis[__name('')] = undefined;
44
+ }
45
+ }
46
+
47
+ lynx.registerDataProcessors = function(
48
+ dataProcessorDefinition?: DataProcessorDefinition,
49
+ ) {
50
+ let hasDefaultDataProcessorExecuted = false;
51
+ // @ts-ignore
52
+ globalThis.processData = (data, processorName) => {
53
+ if (__PROFILE__) {
54
+ console.profile('processData');
55
+ }
56
+
57
+ let r;
58
+ try {
59
+ if (processorName) {
60
+ r = dataProcessorDefinition?.dataProcessors?.[processorName]?.(data)
61
+ ?? data;
62
+ } else {
63
+ r = dataProcessorDefinition?.defaultDataProcessor?.(data) ?? data;
64
+ }
65
+ } catch (e: any) {
66
+ lynx.reportError(e);
67
+ // when there is an error
68
+ // we should perform like dataProcessor returns nothing
69
+ // so use `{}` rather than `data`
70
+ r = {};
71
+ }
72
+
73
+ if (__PROFILE__) {
74
+ console.profileEnd();
75
+ }
76
+
77
+ if (hasDefaultDataProcessorExecuted === false) {
78
+ // @ts-ignore
79
+ if (globalThis.__I18N_RESOURCE_TRANSLATION__) {
80
+ r = {
81
+ ...r,
82
+ // @ts-ignore
83
+ __I18N_RESOURCE_TRANSLATION__: globalThis.__I18N_RESOURCE_TRANSLATION__,
84
+ };
85
+ }
86
+
87
+ // @ts-ignore
88
+ if (__EXTRACT_STR__) {
89
+ r = {
90
+ ...r,
91
+ // @ts-ignore
92
+ _EXTRACT_STR: __EXTRACT_STR_IDENT_FLAG__,
93
+ };
94
+ }
95
+ }
96
+
97
+ if (processorName) {
98
+ } else {
99
+ hasDefaultDataProcessorExecuted = true;
100
+ }
101
+
102
+ return r;
103
+ // TODO
104
+ };
105
+ };
106
+
107
+ // register empty DataProcessors to make sure `globalThis.processData` is set
108
+ lynx.registerDataProcessors();
109
+ }
110
+ }
@@ -0,0 +1,53 @@
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 { snapshotInstanceManager } from '../snapshot.js';
5
+
6
+ function injectLepusMethods(): void {
7
+ Object.assign(globalThis, {
8
+ getUniqueIdListBySnapshotId,
9
+ getSnapshotIdByUniqueId,
10
+ });
11
+ }
12
+
13
+ /**
14
+ * Get the list of `unique_id` of the fiber element by the SnapshotInstance `__id`.
15
+ */
16
+ function getUniqueIdListBySnapshotId({ snapshotId }: { snapshotId: number }) {
17
+ const si = snapshotInstanceManager.values.get(snapshotId);
18
+ if (si?.__elements?.length) {
19
+ const uniqueIdList = [];
20
+ for (const element of si.__elements) {
21
+ const uniqueId = __GetElementUniqueID(element);
22
+ uniqueIdList.push(uniqueId);
23
+ }
24
+ return {
25
+ uniqueIdList,
26
+ };
27
+ }
28
+ return null;
29
+ }
30
+
31
+ /**
32
+ * Get the SnapshotInstance `__id` of the fiber element by the `unique_id`.
33
+ */
34
+ function getSnapshotIdByUniqueId({ uniqueId }: { uniqueId: number }) {
35
+ for (const si of snapshotInstanceManager.values.values()) {
36
+ if (si?.__elements?.length) {
37
+ for (const element of si.__elements) {
38
+ const unique_id = __GetElementUniqueID(element);
39
+ if (unique_id === uniqueId) {
40
+ return {
41
+ snapshotId: si.__id,
42
+ };
43
+ }
44
+ }
45
+ }
46
+ }
47
+ return null;
48
+ }
49
+
50
+ /**
51
+ * @internal
52
+ */
53
+ export { injectLepusMethods };
@@ -0,0 +1,126 @@
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 { Fragment, lazy as backgroundLazy, createElement } from 'preact/compat';
5
+
6
+ /**
7
+ * To make code below works
8
+ * const App1 = lazy(() => import("./x").then(({App1}) => ({default: App1})))
9
+ * const App2 = lazy(() => import("./x").then(({App2}) => ({default: App2})))
10
+ * @internal
11
+ */
12
+ export const makeSyncThen = function<T>(result: T) {
13
+ return function(this: Promise<T>, onF?: Function): Promise<T> {
14
+ if (onF) {
15
+ let ret;
16
+ try {
17
+ ret = onF(result);
18
+ } catch (e) {
19
+ return Promise.reject(e);
20
+ }
21
+
22
+ // @ts-ignore
23
+ if (ret && typeof ret.then === 'function' /* `thenable` object */) {
24
+ // lazy(() =>
25
+ // import("./x").then(() => new Promise(...))
26
+ // )
27
+ // Calling `then` and passing a callback is standard behavior
28
+ // but in Lepus runtime the callback will never be called
29
+ // So can be simplified to code below
30
+ return new Promise(() => {});
31
+
32
+ // TODO(hongzhiyuan.hzy): Avoid warning that cannot be turned-off, so the warning is commented
33
+ // lynx.reportError(
34
+ // new Error(
35
+ // 'You returned a Promise in promise-chain of lazy-bundle import (eg. `import("./x").then(() => new Promise(...))`), which will cause related Component unavailable at first-screen, '
36
+ // ),
37
+ // { level: "warning" }
38
+ // );
39
+ }
40
+
41
+ const p = Promise.resolve(ret);
42
+ // @ts-ignore
43
+ p.then = makeSyncThen(ret);
44
+ return p;
45
+ }
46
+
47
+ return this;
48
+ };
49
+ };
50
+
51
+ /**
52
+ * Load dynamic component from source. Designed to be used with `lazy`.
53
+ * @param source - where dynamic component template.js locates
54
+ * @returns
55
+ * @public
56
+ */
57
+ export function loadLazyBundle<
58
+ T extends { default: React.ComponentType<any> },
59
+ >(source: string): Promise<T> {
60
+ if (__LEPUS__) {
61
+ const query = __QueryComponent(source);
62
+ let result: T;
63
+ try {
64
+ result = query.evalResult;
65
+ } catch (e) {
66
+ // Here we cannot return a rejected promise
67
+ // (which will eventually be an unhandled rejection and cause unnecessary redbox)
68
+ // But we still need a object in shape of Promise
69
+ // So we return a Promise which will never resolve or reject,
70
+ // which fit our principle "lepus run only once at first-screen" better
71
+ return new Promise(() => {});
72
+ }
73
+ const r: Promise<T> = Promise.resolve(result);
74
+ // Why we should modify the implementation of `then`?
75
+ // We should make it `sync` so lepus first-screen render can use result above instantly
76
+ // We also should keep promise shape
77
+ // @ts-ignore
78
+ r.then = makeSyncThen(result);
79
+ return r;
80
+ } else if (__JS__) {
81
+ return new Promise((resolve, reject) => {
82
+ const callback: (result: any) => void = result => {
83
+ const { code, detail } = result;
84
+ if (code === 0) {
85
+ const { schema } = detail;
86
+ const exports = lynxCoreInject.tt.getDynamicComponentExports(schema);
87
+ // `code === 0` means that the lazy bundle has been successfully parsed. However,
88
+ // its javascript files may still fail to run, which would prevent the retrieval of the exports object.
89
+ if (exports) {
90
+ resolve(exports);
91
+ return;
92
+ }
93
+ }
94
+ reject(new Error('Lazy bundle load failed: ' + JSON.stringify(result)));
95
+ };
96
+ if (typeof lynx.QueryComponent === 'function') {
97
+ lynx.QueryComponent(source, callback);
98
+ } else {
99
+ lynx.getNativeLynx().QueryComponent(source, callback);
100
+ }
101
+ });
102
+ }
103
+
104
+ throw new Error('unreachable');
105
+ }
106
+
107
+ lynx.loadLazyBundle = loadLazyBundle;
108
+
109
+ /**
110
+ * @internal
111
+ */
112
+ export function mainThreadLazy<T>(loader: () => Promise<{ default: T } | T>) {
113
+ const Lazy = backgroundLazy<T>(loader);
114
+
115
+ function _Lazy(props: any) {
116
+ try {
117
+ // @ts-expect-error `Lazy` returned from `backgroundLazy` should be a FC
118
+ return Lazy(props);
119
+ } catch (e) {
120
+ // We should never throw at mainThread
121
+ return createElement(Fragment, {});
122
+ }
123
+ }
124
+
125
+ return _Lazy as T;
126
+ }
@@ -0,0 +1,117 @@
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
+ import { DIFF } from '../renderToOpcodes/constants.js';
7
+ import { __globalSnapshotPatch } from '../snapshotPatch.js';
8
+
9
+ enum PerformanceTimingKeys {
10
+ update_set_state_trigger,
11
+ update_diff_vdom_start,
12
+ update_diff_vdom_end,
13
+ // update_set_state_trigger, update_diff_vdom_start and update_diff_vdom_end is deprecated
14
+ diff_vdom_start,
15
+ diff_vdom_end,
16
+ pack_changes_start,
17
+ pack_changes_end,
18
+ parse_changes_start,
19
+ parse_changes_end,
20
+ patch_changes_start,
21
+ patch_changes_end,
22
+ hydrate_parse_snapshot_start,
23
+ hydrate_parse_snapshot_end,
24
+ }
25
+
26
+ /**
27
+ * @deprecated used by old timing api(setState timing flag)
28
+ */
29
+ const PerfSpecificKey = '__lynx_timing_flag';
30
+ let timingFlag: string | undefined;
31
+ let shouldMarkDiffVdomStart = false;
32
+ let shouldMarkDiffVdomEnd = false;
33
+
34
+ let globalPipelineOptions: PipelineOptions | undefined;
35
+
36
+ /**
37
+ * @deprecated used by old timing api(setState timing flag)
38
+ */
39
+ function markTimingLegacy(key: PerformanceTimingKeys, timingFlag_?: string): void {
40
+ switch (key) {
41
+ case PerformanceTimingKeys.update_set_state_trigger: {
42
+ shouldMarkDiffVdomStart = true;
43
+ shouldMarkDiffVdomEnd = true;
44
+ timingFlag = timingFlag_;
45
+ break;
46
+ }
47
+ case PerformanceTimingKeys.update_diff_vdom_start: {
48
+ /* v8 ignore start */
49
+ if (!shouldMarkDiffVdomStart) {
50
+ return;
51
+ }
52
+ /* v8 ignore stop */
53
+ shouldMarkDiffVdomStart = false;
54
+ break;
55
+ }
56
+ case PerformanceTimingKeys.update_diff_vdom_end: {
57
+ if (!shouldMarkDiffVdomEnd) {
58
+ return;
59
+ }
60
+ shouldMarkDiffVdomEnd = false;
61
+ break;
62
+ }
63
+ }
64
+ lynx.getNativeApp().markTiming?.(timingFlag, PerformanceTimingKeys[key]);
65
+ }
66
+
67
+ function beginPipeline(needTimestamps: boolean, timingFlag?: string): void {
68
+ globalPipelineOptions = lynx.performance?._generatePipelineOptions?.();
69
+ if (globalPipelineOptions) {
70
+ globalPipelineOptions.needTimestamps = needTimestamps;
71
+ lynx.performance?._onPipelineStart?.(globalPipelineOptions.pipelineID);
72
+ if (timingFlag) {
73
+ lynx.performance?._bindPipelineIdWithTimingFlag?.(globalPipelineOptions.pipelineID, timingFlag);
74
+ }
75
+ }
76
+ }
77
+
78
+ function setPipeline(pipeline: PipelineOptions | undefined): void {
79
+ globalPipelineOptions = pipeline;
80
+ }
81
+
82
+ function markTiming(timestampKey: PerformanceTimingKeys, force?: boolean): void {
83
+ if (globalPipelineOptions && (force || globalPipelineOptions.needTimestamps)) {
84
+ lynx.performance?._markTiming?.(globalPipelineOptions.pipelineID, PerformanceTimingKeys[timestampKey]);
85
+ }
86
+ }
87
+
88
+ function initTimingAPI(): void {
89
+ const oldDiff = options[DIFF];
90
+ options[DIFF] = (vnode: VNode) => {
91
+ // check `__globalSnapshotPatch` to make sure this only runs after hydrate
92
+ if (__JS__ && __globalSnapshotPatch) {
93
+ if (!globalPipelineOptions) {
94
+ beginPipeline(false);
95
+ markTiming(PerformanceTimingKeys.diff_vdom_start, true);
96
+ }
97
+ if (shouldMarkDiffVdomStart) {
98
+ markTimingLegacy(PerformanceTimingKeys.update_diff_vdom_start);
99
+ }
100
+ }
101
+ oldDiff?.(vnode);
102
+ };
103
+ }
104
+
105
+ /**
106
+ * @internal
107
+ */
108
+ export {
109
+ PerformanceTimingKeys,
110
+ PerfSpecificKey,
111
+ markTimingLegacy,
112
+ initTimingAPI,
113
+ beginPipeline,
114
+ markTiming,
115
+ setPipeline,
116
+ globalPipelineOptions,
117
+ };
@@ -0,0 +1,212 @@
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 { LifecycleConstant, NativeUpdateDataType } from '../lifecycleConstant.js';
7
+ import { PerformanceTimingKeys, beginPipeline, markTiming } from './performance.js';
8
+ import { BackgroundSnapshotInstance, hydrate } from '../backgroundSnapshot.js';
9
+ import { destroyBackground } from '../lifecycle/destroy.js';
10
+ import { commitPatchUpdate, genCommitTaskId, globalCommitTaskMap } from '../lifecycle/patchUpdate.js';
11
+ import { reloadBackground } from '../lifecycle/reload.js';
12
+ import { runWithForce } from '../lynx.js';
13
+ import { CHILDREN } from '../renderToOpcodes/constants.js';
14
+ import { __root } from '../root.js';
15
+ import { globalRefsToSet, updateBackgroundRefs } from '../snapshot/ref.js';
16
+ import { backgroundSnapshotInstanceManager } from '../snapshot.js';
17
+ import { destroyWorklet } from '../worklet/jsImpl.js';
18
+
19
+ function injectTt(): void {
20
+ // @ts-ignore
21
+ const tt = lynxCoreInject.tt;
22
+ tt.OnLifecycleEvent = OnLifecycleEvent;
23
+ tt.publishEvent = delayedPublishEvent;
24
+ tt.publicComponentEvent = delayedPublicComponentEvent;
25
+ tt.callDestroyLifetimeFun = () => {
26
+ destroyWorklet();
27
+ destroyBackground();
28
+ };
29
+ tt.updateGlobalProps = updateGlobalProps;
30
+ tt.updateCardData = updateCardData;
31
+ tt.onAppReload = reloadBackground;
32
+ tt.processCardConfig = () => {
33
+ // used to updateTheme, no longer rely on this function
34
+ };
35
+ }
36
+
37
+ let delayedLifecycleEvents: [type: string, data: any][];
38
+ async function OnLifecycleEvent([type, data]: [string, any]) {
39
+ const hasRootRendered = CHILDREN in __root;
40
+ // never called `render(<App/>, __root)`
41
+ // happens if user call `root.render()` async
42
+ if (!hasRootRendered) {
43
+ delayedLifecycleEvents ??= [];
44
+ delayedLifecycleEvents.push([type, data]);
45
+ return;
46
+ }
47
+
48
+ if (__PROFILE__) {
49
+ console.profile(`OnLifecycleEvent::${type}`);
50
+ }
51
+ switch (type) {
52
+ case LifecycleConstant.firstScreen: {
53
+ const { root: lepusSide, refPatch, jsReadyEventIdSwap } = data;
54
+ if (__PROFILE__) {
55
+ console.profile('hydrate');
56
+ }
57
+ beginPipeline(true, 'react_lynx_hydrate');
58
+ markTiming(PerformanceTimingKeys.hydrate_parse_snapshot_start);
59
+ const before = JSON.parse(lepusSide);
60
+ markTiming(PerformanceTimingKeys.hydrate_parse_snapshot_end);
61
+ markTiming(PerformanceTimingKeys.diff_vdom_start);
62
+ const snapshotPatch = hydrate(
63
+ before,
64
+ __root as BackgroundSnapshotInstance,
65
+ );
66
+ if (__PROFILE__) {
67
+ console.profileEnd();
68
+ }
69
+ markTiming(PerformanceTimingKeys.diff_vdom_end);
70
+
71
+ if (delayedEvents) {
72
+ delayedEvents.forEach((args) => {
73
+ const [handlerName, data] = args;
74
+ let [idStr, ...rest] = handlerName.split(':');
75
+ while (jsReadyEventIdSwap[idStr!]) idStr = jsReadyEventIdSwap[idStr!];
76
+ try {
77
+ publishEvent([idStr, ...rest].join(':'), data);
78
+ } catch (e) {
79
+ lynx.reportError(e);
80
+ }
81
+ });
82
+ delayedEvents.length = 0;
83
+ }
84
+ lynxCoreInject.tt.publishEvent = publishEvent;
85
+ lynxCoreInject.tt.publicComponentEvent = publicComponentEvent;
86
+
87
+ if (__PROFILE__) {
88
+ console.profile('patchRef');
89
+ }
90
+ if (refPatch) {
91
+ globalRefsToSet.set(0, JSON.parse(refPatch));
92
+ updateBackgroundRefs(0);
93
+ }
94
+ if (__PROFILE__) {
95
+ console.profileEnd();
96
+ }
97
+ markTiming(PerformanceTimingKeys.pack_changes_start);
98
+ // console.debug("********** After hydration:");
99
+ // printSnapshotInstance(__root as BackgroundSnapshotInstance);
100
+ if (__PROFILE__) {
101
+ console.profile('commitChanges');
102
+ }
103
+ const commitTaskId = genCommitTaskId();
104
+ await commitPatchUpdate({ snapshotPatch }, { commitTaskId, isHydration: true });
105
+ updateBackgroundRefs(commitTaskId);
106
+ globalCommitTaskMap.forEach((commitTask, id) => {
107
+ if (id > commitTaskId) {
108
+ return;
109
+ }
110
+ commitTask();
111
+ globalCommitTaskMap.delete(id);
112
+ });
113
+ break;
114
+ }
115
+ case LifecycleConstant.globalEventFromLepus: {
116
+ const [eventName, params] = data;
117
+ lynx.getJSModule('GlobalEventEmitter').trigger(eventName, params);
118
+ break;
119
+ }
120
+ case LifecycleConstant.ref: {
121
+ const { refPatch, commitTaskId } = data;
122
+ if (commitTaskId) {
123
+ globalRefsToSet.set(commitTaskId, JSON.parse(refPatch));
124
+ } else {
125
+ globalRefsToSet.set(0, JSON.parse(refPatch));
126
+ updateBackgroundRefs(0);
127
+ }
128
+ break;
129
+ }
130
+ }
131
+ if (__PROFILE__) {
132
+ console.profileEnd();
133
+ }
134
+ }
135
+
136
+ function flushDelayedLifecycleEvents(): void {
137
+ if (delayedLifecycleEvents) {
138
+ delayedLifecycleEvents.forEach((e) => {
139
+ try {
140
+ OnLifecycleEvent(e);
141
+ } catch (e) {
142
+ lynx.reportError(e);
143
+ }
144
+ });
145
+ delayedLifecycleEvents.length = 0;
146
+ }
147
+ }
148
+
149
+ function publishEvent(handlerName: string, data: unknown) {
150
+ // TODO: delay js events until js ready
151
+ lynxCoreInject.tt.callBeforePublishEvent?.(data);
152
+ const eventHandler = backgroundSnapshotInstanceManager.getValueBySign(
153
+ handlerName,
154
+ );
155
+ if (eventHandler) {
156
+ try {
157
+ (eventHandler as Function)(data);
158
+ } catch (e) {
159
+ lynx.reportError(e as Error);
160
+ }
161
+ }
162
+ }
163
+
164
+ function publicComponentEvent(_componentId: string, handlerName: string, data: unknown) {
165
+ publishEvent(handlerName, data);
166
+ }
167
+
168
+ let delayedEvents: [handlerName: string, data: unknown][];
169
+ function delayedPublishEvent(handlerName: string, data: unknown) {
170
+ delayedEvents ??= [];
171
+ delayedEvents.push([handlerName, data]);
172
+ }
173
+
174
+ function delayedPublicComponentEvent(_componentId: string, handlerName: string, data: unknown) {
175
+ delayedPublishEvent(handlerName, data);
176
+ }
177
+
178
+ function updateGlobalProps(newData: Record<string, any>): void {
179
+ Object.assign(lynx.__globalProps, newData);
180
+
181
+ // Our purpose is to make sure SYNC setState inside `emit`'s listeners
182
+ // can be batched with updateFromRoot
183
+ // This is already done because updateFromRoot will consume all dirty flags marked by
184
+ // the setState, and setState's flush will be a noop. No extra diffs will be needed.
185
+ Promise.resolve().then(() => {
186
+ runWithForce(() => render(__root.__jsx, __root as any));
187
+ });
188
+ lynxCoreInject.tt.GlobalEventEmitter.emit('onGlobalPropsChanged');
189
+ }
190
+
191
+ function updateCardData(newData: Record<string, any>, options?: Record<string, any>): void {
192
+ const { ['__lynx_timing_flag']: performanceTimingFlag, ...restNewData } = newData;
193
+ if (performanceTimingFlag) {
194
+ lynx.reportError(
195
+ new Error(
196
+ `Received unsupported updateData with \`__lynx_timing_flag\` (value "${performanceTimingFlag}"), the timing flag is ignored`,
197
+ ),
198
+ );
199
+ }
200
+ const { type = NativeUpdateDataType.UPDATE } = options || {};
201
+ if (type == NativeUpdateDataType.RESET) {
202
+ // @ts-ignore
203
+ lynx.__initData = {};
204
+ }
205
+
206
+ // COW when modify `lynx.__initData` to make sure Provider & Consumer works
207
+ // @ts-ignore
208
+ lynx.__initData = Object.assign({}, lynx.__initData, restNewData);
209
+ lynxCoreInject.tt.GlobalEventEmitter.emit('onDataChanged');
210
+ }
211
+
212
+ export { injectTt, flushDelayedLifecycleEvents, delayedLifecycleEvents };