@lynx-js/react 0.106.1 → 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.
@@ -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