@lynx-js/react 0.106.0 → 0.106.2

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.
@@ -1,19 +1,18 @@
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 { __page, setupPage, SnapshotInstance } from '../snapshot.js';
5
- // @ts-ignore
6
- import { render as renderToString } from '../renderToOpcodes/index.js';
7
- import { LifecycleConstant } from '../lifecycleConstant.js';
8
- import { takeGlobalRefPatchMap } from '../snapshot/ref.js';
9
- import { isEmptyObject } from '../utils.js';
10
- import { __root, setRoot } from '../root.js';
4
+ import { hydrate } from '../hydrate.js';
5
+ import { isJSReady, jsReady, jsReadyEventIdSwap, resetJSReady } from '../lifecycle/event/jsReady.js';
11
6
  import { reloadMainThread } from '../lifecycle/reload.js';
12
7
  import { renderMainThread } from '../lifecycle/render.js';
13
- import { hydrate } from '../hydrate.js';
14
- import { markTiming, PerformanceTimingKeys, setPipeline } from './performance.js';
8
+ import { LifecycleConstant } from '../lifecycleConstant.js';
15
9
  import { __pendingListUpdates } from '../list.js';
16
10
  import { ssrHydrateByOpcodes } from '../opcodes.js';
11
+ import { __root, setRoot } from '../root.js';
12
+ import { takeGlobalRefPatchMap } from '../snapshot/ref.js';
13
+ import { SnapshotInstance, __page, setupPage } from '../snapshot.js';
14
+ import { isEmptyObject } from '../utils.js';
15
+ import { PerformanceTimingKeys, markTiming, setPipeline } from './performance.js';
17
16
 
18
17
  function ssrEncode() {
19
18
  const { __opcodes } = __root;
@@ -52,10 +51,8 @@ function ssrHydrate(info: string) {
52
51
  }
53
52
 
54
53
  function injectCalledByNative(): void {
55
- if (process.env['NODE_ENV'] !== 'test') {
56
- if (__FIRST_SCREEN_SYNC_TIMING__ !== 'jsReady' && __ENABLE_SSR__) {
57
- throw new Error('`firstScreenSyncTiming` must be `jsReady` when SSR is enabled');
58
- }
54
+ if (process.env['NODE_ENV'] !== 'test' && __FIRST_SCREEN_SYNC_TIMING__ !== 'jsReady' && __ENABLE_SSR__) {
55
+ throw new Error('`firstScreenSyncTiming` must be `jsReady` when SSR is enabled');
59
56
  }
60
57
 
61
58
  const calledByNative: LynxCallByNative = {
@@ -77,8 +74,7 @@ function injectCalledByNative(): void {
77
74
 
78
75
  function renderPage(data: any): void {
79
76
  // reset `jsReady` state
80
- isJSReady = false;
81
- jsReadyEventIdSwap = {};
77
+ resetJSReady();
82
78
 
83
79
  lynx.__initData = data || {};
84
80
 
@@ -96,14 +92,13 @@ function renderPage(data: any): void {
96
92
  }
97
93
  }
98
94
 
99
- function updatePage(data: any, options?: UpdatePageOption | undefined): void {
95
+ function updatePage(data: any, options?: UpdatePageOption): void {
100
96
  if (options?.reloadTemplate) {
101
97
  reloadMainThread(data, options);
102
98
  return;
103
99
  }
104
100
 
105
101
  if (options?.resetPageData) {
106
- // @ts-ignore
107
102
  lynx.__initData = {};
108
103
  }
109
104
 
@@ -146,7 +141,7 @@ function updatePage(data: any, options?: UpdatePageOption | undefined): void {
146
141
  }
147
142
  }
148
143
 
149
- function updateGlobalProps(_data: any, options?: UpdatePageOption | undefined): void {
144
+ function updateGlobalProps(_data: any, options?: UpdatePageOption): void {
150
145
  if (options) {
151
146
  __FlushElementTree(__page, options);
152
147
  } else {
@@ -154,21 +149,6 @@ function updateGlobalProps(_data: any, options?: UpdatePageOption | undefined):
154
149
  }
155
150
  }
156
151
 
157
- let isJSReady: boolean;
158
- let jsReadyEventIdSwap: Record<number, number>;
159
- function jsReady() {
160
- __OnLifecycleEvent([
161
- LifecycleConstant.firstScreen, /* FIRST_SCREEN */
162
- {
163
- root: JSON.stringify(__root),
164
- refPatch: JSON.stringify(takeGlobalRefPatchMap()),
165
- jsReadyEventIdSwap,
166
- },
167
- ]);
168
- isJSReady = true;
169
- jsReadyEventIdSwap = {};
170
- }
171
-
172
152
  /**
173
153
  * @internal
174
154
  */
@@ -54,57 +54,63 @@ export const makeSyncThen = function<T>(result: T) {
54
54
  * @returns
55
55
  * @public
56
56
  */
57
- export function loadLazyBundle<
57
+ export const loadLazyBundle: <
58
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;
59
+ >(source: string) => Promise<T> = /*#__PURE__*/ (() => {
60
+ lynx.loadLazyBundle = loadLazyBundle;
61
+
62
+ function loadLazyBundle<
63
+ T extends { default: React.ComponentType<any> },
64
+ >(source: string): Promise<T> {
65
+ if (__LEPUS__) {
66
+ const query = __QueryComponent(source);
67
+ let result: T;
68
+ try {
69
+ result = query.evalResult;
70
+ } catch (e) {
71
+ // Here we cannot return a rejected promise
72
+ // (which will eventually be an unhandled rejection and cause unnecessary redbox)
73
+ // But we still need a object in shape of Promise
74
+ // So we return a Promise which will never resolve or reject,
75
+ // which fit our principle "lepus run only once at first-screen" better
76
+ return new Promise(() => {});
77
+ }
78
+ const r: Promise<T> = Promise.resolve(result);
79
+ // Why we should modify the implementation of `then`?
80
+ // We should make it `sync` so lepus first-screen render can use result above instantly
81
+ // We also should keep promise shape
82
+ // @ts-ignore
83
+ r.then = makeSyncThen(result);
84
+ return r;
85
+ } else if (__JS__) {
86
+ return new Promise((resolve, reject) => {
87
+ const callback: (result: any) => void = result => {
88
+ const { code, detail } = result;
89
+ if (code === 0) {
90
+ const { schema } = detail;
91
+ const exports = lynxCoreInject.tt.getDynamicComponentExports(schema);
92
+ // `code === 0` means that the lazy bundle has been successfully parsed. However,
93
+ // its javascript files may still fail to run, which would prevent the retrieval of the exports object.
94
+ if (exports) {
95
+ resolve(exports);
96
+ return;
97
+ }
92
98
  }
99
+ reject(new Error('Lazy bundle load failed: ' + JSON.stringify(result)));
100
+ };
101
+ if (typeof lynx.QueryComponent === 'function') {
102
+ lynx.QueryComponent(source, callback);
103
+ } else {
104
+ lynx.getNativeLynx().QueryComponent!(source, callback);
93
105
  }
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
- }
106
+ });
107
+ }
103
108
 
104
- throw new Error('unreachable');
105
- }
109
+ throw new Error('unreachable');
110
+ }
106
111
 
107
- lynx.loadLazyBundle = loadLazyBundle;
112
+ return loadLazyBundle;
113
+ })();
108
114
 
109
115
  /**
110
116
  * @internal