@lynx-js/web-worker-runtime 0.15.7 → 0.16.1

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,40 @@
1
1
  # @lynx-js/web-worker-runtime
2
2
 
3
+ ## 0.16.1
4
+
5
+ ### Patch Changes
6
+
7
+ - feat: supports lazy bundle. (This feature requires `@lynx-js/lynx-core >= 0.1.3`) ([#1235](https://github.com/lynx-family/lynx-stack/pull/1235))
8
+
9
+ - Updated dependencies [[`608f375`](https://github.com/lynx-family/lynx-stack/commit/608f375e20732cc4c9f141bfbf9800ba6896100b)]:
10
+ - @lynx-js/web-mainthread-apis@0.16.1
11
+ - @lynx-js/web-constants@0.16.1
12
+ - @lynx-js/web-worker-rpc@0.16.1
13
+
14
+ ## 0.16.0
15
+
16
+ ### Minor Changes
17
+
18
+ - refactor: provide the mts a real globalThis ([#1589](https://github.com/lynx-family/lynx-stack/pull/1589))
19
+
20
+ Before this change, We create a function wrapper and a fake globalThis for Javascript code.
21
+
22
+ This caused some issues.
23
+
24
+ After this change, we will create an iframe for createing an isolated Javascript context.
25
+
26
+ This means the globalThis will be the real one.
27
+
28
+ ### Patch Changes
29
+
30
+ - fix: the SystemInfo in bts should be assigned to the globalThis ([#1599](https://github.com/lynx-family/lynx-stack/pull/1599))
31
+
32
+ - Updated dependencies [[`1a32dd8`](https://github.com/lynx-family/lynx-stack/commit/1a32dd886fe736c95639f67028cf7685377d9769), [`bb53d9a`](https://github.com/lynx-family/lynx-stack/commit/bb53d9a035f607e7c89952098d4ed77877a2e3c1), [`1a32dd8`](https://github.com/lynx-family/lynx-stack/commit/1a32dd886fe736c95639f67028cf7685377d9769), [`c1f8715`](https://github.com/lynx-family/lynx-stack/commit/c1f8715a81b2e69ff46fc363013626db4468c209)]:
33
+ - @lynx-js/web-mainthread-apis@0.16.0
34
+ - @lynx-js/web-constants@0.16.0
35
+ - @lynx-js/offscreen-document@0.1.4
36
+ - @lynx-js/web-worker-rpc@0.16.0
37
+
3
38
  ## 0.15.7
4
39
 
5
40
  ### Patch Changes
@@ -12,4 +12,12 @@ export declare function createBackgroundLynx(config: BackMainThreadContextConfig
12
12
  animate(operation: import("@lynx-js/web-constants").AnimationOperation, id: string, keyframes?: Record<string, any>[], timingOptions?: Record<string, any>): void;
13
13
  };
14
14
  getI18nResource: () => import("@lynx-js/web-constants").Cloneable;
15
+ QueryComponent: (source: string, callback: (ret: {
16
+ __hasReady: boolean;
17
+ } | {
18
+ code: number;
19
+ detail?: {
20
+ schema: string;
21
+ };
22
+ }) => void) => void;
15
23
  };
@@ -31,6 +31,7 @@ export function createBackgroundLynx(config, nativeApp, mainThreadRpc, uiThreadR
31
31
  return createElement(id, uiThreadRpc);
32
32
  },
33
33
  getI18nResource: () => nativeApp.i18nResource.data,
34
+ QueryComponent: (source, callback) => nativeApp.queryComponent(source, callback),
34
35
  };
35
36
  }
36
37
  //# sourceMappingURL=createBackgroundLynx.js.map
@@ -1,4 +1,4 @@
1
- import { callLepusMethodEndpoint, setNativePropsEndpoint, triggerComponentEventEndpoint, selectComponentEndpoint, systemInfo, I18nResource, reportErrorEndpoint, globalDisallowedVars, } from '@lynx-js/web-constants';
1
+ import { callLepusMethodEndpoint, setNativePropsEndpoint, triggerComponentEventEndpoint, selectComponentEndpoint, I18nResource, reportErrorEndpoint, queryComponentEndpoint, updateBTSTemplateCacheEndpoint, } from '@lynx-js/web-constants';
2
2
  import { createInvokeUIMethod } from './crossThreadHandlers/createInvokeUIMethod.js';
3
3
  import { registerPublicComponentEventHandler } from './crossThreadHandlers/registerPublicComponentEventHandler.js';
4
4
  import { registerGlobalExposureEventHandler } from './crossThreadHandlers/registerGlobalExposureEventHandler.js';
@@ -14,43 +14,24 @@ import { createGetPathInfo } from './crossThreadHandlers/createGetPathInfo.js';
14
14
  let nativeAppCount = 0;
15
15
  const sharedData = {};
16
16
  export async function createNativeApp(config) {
17
- const { mainThreadRpc, uiThreadRpc, template, nativeModulesMap, timingSystem, browserConfig, } = config;
17
+ const { mainThreadRpc, uiThreadRpc, template, nativeModulesMap, timingSystem, } = config;
18
18
  const performanceApis = createPerformanceApis(timingSystem);
19
19
  const callLepusMethod = mainThreadRpc.createCallbackify(callLepusMethodEndpoint, 2);
20
20
  const setNativeProps = uiThreadRpc.createCall(setNativePropsEndpoint);
21
21
  const triggerComponentEvent = uiThreadRpc.createCall(triggerComponentEventEndpoint);
22
22
  const selectComponent = uiThreadRpc.createCallbackify(selectComponentEndpoint, 3);
23
+ const queryComponent = mainThreadRpc.createCall(queryComponentEndpoint);
23
24
  const reportError = uiThreadRpc.createCall(reportErrorEndpoint);
24
25
  const createBundleInitReturnObj = () => {
25
- const entry = globalThis.module.exports;
26
- return {
27
- init: (lynxCoreInject) => {
28
- lynxCoreInject.tt.lynxCoreInject = lynxCoreInject;
29
- lynxCoreInject.tt.globalThis ??= new Proxy(lynxCoreInject, {
30
- get(target, prop) {
31
- if (typeof prop === 'string' && globalDisallowedVars.includes(prop)) {
32
- return undefined;
33
- }
34
- // @ts-expect-error
35
- return target[prop] ?? globalThis[prop];
36
- },
37
- set(target, prop, value) {
38
- // @ts-expect-error
39
- target[prop] = value;
40
- return true;
41
- },
42
- ownKeys(target) {
43
- return Reflect.ownKeys(target).filter((key) => key !== 'globalThis');
44
- },
45
- });
46
- Object.assign(lynxCoreInject.tt, {
47
- SystemInfo: { ...systemInfo, ...browserConfig },
48
- });
49
- const ret = entry?.(lynxCoreInject.tt);
50
- return ret;
51
- },
52
- };
26
+ const ret = globalThis.module.exports ?? globalThis.__bundle__holder;
27
+ globalThis.module.exports = null;
28
+ globalThis.__bundle__holder = null;
29
+ return ret;
53
30
  };
31
+ const templateCache = new Map([['__Card__', template]]);
32
+ mainThreadRpc.registerHandler(updateBTSTemplateCacheEndpoint, (url, template) => {
33
+ templateCache.set(url, template);
34
+ });
54
35
  const i18nResource = new I18nResource();
55
36
  let release = '';
56
37
  const nativeApp = {
@@ -61,20 +42,28 @@ export async function createNativeApp(config) {
61
42
  clearTimeout: clearTimeout,
62
43
  clearInterval: clearInterval,
63
44
  nativeModuleProxy: await createNativeModules(uiThreadRpc, mainThreadRpc, nativeModulesMap),
64
- loadScriptAsync: function (sourceURL, callback) {
65
- const manifestUrl = template.manifest[`/${sourceURL}`];
45
+ loadScriptAsync: function (sourceURL, callback, entryName) {
46
+ entryName = entryName ?? '__Card__';
47
+ const manifestUrl = templateCache.get(entryName)
48
+ ?.manifest[`/${sourceURL}`];
66
49
  if (manifestUrl)
67
50
  sourceURL = manifestUrl;
51
+ globalThis.module.exports = null;
52
+ globalThis.__bundle__holder = null;
68
53
  import(
69
54
  /* webpackIgnore: true */
70
55
  sourceURL).catch(callback).then(async () => {
71
56
  callback(null, createBundleInitReturnObj());
72
57
  });
73
58
  },
74
- loadScript: (sourceURL) => {
75
- const manifestUrl = template.manifest[`/${sourceURL}`];
59
+ loadScript: (sourceURL, entryName) => {
60
+ entryName = entryName ?? '__Card__';
61
+ const manifestUrl = templateCache.get(entryName)
62
+ ?.manifest[`/${sourceURL}`];
76
63
  if (manifestUrl)
77
64
  sourceURL = manifestUrl;
65
+ globalThis.module.exports = null;
66
+ globalThis.__bundle__holder = null;
78
67
  importScripts(sourceURL);
79
68
  return createBundleInitReturnObj();
80
69
  },
@@ -88,6 +77,7 @@ export async function createNativeApp(config) {
88
77
  setNativeProps,
89
78
  getPathInfo: createGetPathInfo(uiThreadRpc),
90
79
  invokeUIMethod: createInvokeUIMethod(uiThreadRpc),
80
+ tt: null,
91
81
  setCard(tt) {
92
82
  registerPublicComponentEventHandler(mainThreadRpc, tt);
93
83
  registerPublishEventHandler(mainThreadRpc, tt);
@@ -98,6 +88,7 @@ export async function createNativeApp(config) {
98
88
  registerUpdateI18nResource(uiThreadRpc, mainThreadRpc, i18nResource, tt);
99
89
  timingSystem.registerGlobalEmitter(tt.GlobalEventEmitter);
100
90
  tt.lynx.getCoreContext().__start();
91
+ nativeApp.tt = tt;
101
92
  },
102
93
  triggerComponentEvent,
103
94
  selectComponent,
@@ -111,6 +102,16 @@ export async function createNativeApp(config) {
111
102
  i18nResource,
112
103
  reportException: (err, _) => reportError(err, _, release),
113
104
  __SetSourceMapRelease: (err) => release = err.message,
105
+ queryComponent: (source, callback) => {
106
+ if (templateCache.has(source)) {
107
+ callback({ __hasReady: true });
108
+ }
109
+ else {
110
+ queryComponent(source).then(res => {
111
+ callback?.(res);
112
+ });
113
+ }
114
+ },
114
115
  };
115
116
  return nativeApp;
116
117
  }
@@ -26,7 +26,11 @@ export function startBackgroundThread(uiThreadPort, mainThreadPort) {
26
26
  globalThis['napiLoaderOnRT' + nativeApp.id] =
27
27
  await createNapiLoader(uiThreadRpc, config.napiModulesMap);
28
28
  const nativeLynx = createBackgroundLynx(config, nativeApp, mainThreadRpc, uiThreadRpc);
29
- lynxCore.then(({ loadCard, destroyCard, callDestroyLifetimeFun }) => {
29
+ lynxCore.then(({ loadCard, destroyCard, callDestroyLifetimeFun, nativeGlobal, loadDynamicComponent, }) => {
30
+ // @lynx-js/lynx-core >= 0.1.3 will export nativeGlobal and loadDynamicComponent
31
+ if (nativeGlobal && loadDynamicComponent) {
32
+ nativeGlobal.loadDynamicComponent = loadDynamicComponent;
33
+ }
30
34
  loadCard(nativeApp, {
31
35
  ...config,
32
36
  // @ts-ignore
package/dist/index.d.ts CHANGED
@@ -2,4 +2,5 @@ export interface WorkerStartMessage {
2
2
  mode: 'main' | 'background';
3
3
  toPeerThread: MessagePort;
4
4
  toUIThread: MessagePort;
5
+ systemInfo?: Record<string, any>;
5
6
  }
package/dist/index.js CHANGED
@@ -2,11 +2,18 @@
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 { startBackgroundThread } from './backgroundThread/index.js';
5
- import { startMainThreadWorker } from './mainThread/startMainThread.js';
6
- globalThis.onmessage = (ev) => {
7
- const { mode, toPeerThread, toUIThread } = ev
5
+ globalThis.onmessage = async (ev) => {
6
+ const { mode, toPeerThread, toUIThread, systemInfo } = ev
8
7
  .data;
8
+ if (!globalThis.SystemInfo) {
9
+ globalThis.SystemInfo = systemInfo;
10
+ }
9
11
  if (mode === 'main') {
12
+ const { startMainThreadWorker } = await import(
13
+ /* webpackChunkName: "web-worker-runtime-main-thread" */
14
+ /* webpackMode: "lazy-once" */
15
+ /* webpackPreload: true */
16
+ './mainThread/startMainThread.js');
10
17
  startMainThreadWorker(toUIThread, toPeerThread);
11
18
  }
12
19
  else {
@@ -1,14 +1,44 @@
1
1
  // Copyright 2023 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 { flushElementTreeEndpoint, mainThreadStartEndpoint, postOffscreenEventEndpoint, reportErrorEndpoint, dispatchLynxViewEventEndpoint, i18nResourceMissedEventName, I18nResources, updateI18nResourcesEndpoint, multiThreadExposureChangedEndpoint, lynxUniqueIdAttribute, } from '@lynx-js/web-constants';
4
+ import { flushElementTreeEndpoint, mainThreadStartEndpoint, postOffscreenEventEndpoint, reportErrorEndpoint, dispatchLynxViewEventEndpoint, i18nResourceMissedEventName, I18nResources, updateI18nResourcesEndpoint, multiThreadExposureChangedEndpoint, lynxUniqueIdAttribute, loadTemplateMultiThread, } from '@lynx-js/web-constants';
5
5
  import { Rpc } from '@lynx-js/web-worker-rpc';
6
6
  import { createMarkTimingInternal } from './crossThreadHandlers/createMainthreadMarkTimingInternal.js';
7
7
  import { OffscreenDocument } from '@lynx-js/offscreen-document/webworker';
8
8
  import { _onEvent } from '@lynx-js/offscreen-document/webworker';
9
9
  import { registerUpdateDataHandler } from './crossThreadHandlers/registerUpdateDataHandler.js';
10
+ const { prepareMainThreadAPIs } = await import(
11
+ /* webpackChunkName: "web-core-main-thread-apis" */
12
+ /* webpackMode: "lazy-once" */
13
+ /* webpackPreload: true */
14
+ /* webpackPrefetch: true */
15
+ /* webpackFetchPriority: "high" */
16
+ '@lynx-js/web-mainthread-apis');
17
+ function loadScriptSync(url) {
18
+ globalThis.module.exports = null;
19
+ importScripts(url);
20
+ const ret = globalThis.module?.exports;
21
+ return ret;
22
+ }
23
+ function loadScript(url) {
24
+ return new Promise((resolve, reject) => {
25
+ fetch(url)
26
+ .then(() => {
27
+ globalThis.module.exports = null;
28
+ importScripts(url);
29
+ const ret = globalThis.module?.exports;
30
+ resolve(ret);
31
+ }).catch(reject);
32
+ });
33
+ }
34
+ function createCurrentWorkerRealm() {
35
+ return {
36
+ globalWindow: globalThis,
37
+ loadScript,
38
+ loadScriptSync,
39
+ };
40
+ }
10
41
  export async function startMainThreadWorker(uiThreadPort, backgroundThreadPort) {
11
- const { prepareMainThreadAPIs } = await import('@lynx-js/web-mainthread-apis');
12
42
  const uiThreadRpc = new Rpc(uiThreadPort, 'main-to-ui');
13
43
  const backgroundThreadRpc = new Rpc(backgroundThreadPort, 'main-to-bg');
14
44
  const { markTimingInternal, flushMarkTimingInternal } = createMarkTimingInternal(backgroundThreadRpc);
@@ -20,27 +50,32 @@ export async function startMainThreadWorker(uiThreadPort, backgroundThreadPort)
20
50
  options,
21
51
  ]);
22
52
  };
23
- const docu = new OffscreenDocument({
53
+ const document = new OffscreenDocument({
24
54
  onCommit: uiFlush,
25
55
  });
56
+ Object.assign(globalThis, {
57
+ document,
58
+ });
59
+ const mtsRealm = createCurrentWorkerRealm();
26
60
  const i18nResources = new I18nResources();
27
- uiThreadRpc.registerHandler(postOffscreenEventEndpoint, docu[_onEvent]);
61
+ uiThreadRpc.registerHandler(postOffscreenEventEndpoint, document[_onEvent]);
28
62
  const sendMultiThreadExposureChangedEndpoint = uiThreadRpc.createCall(multiThreadExposureChangedEndpoint);
29
- const { startMainThread } = prepareMainThreadAPIs(backgroundThreadRpc, docu, docu.createElement.bind(docu), (exposureChangedElementUniqueIds) => {
30
- docu.commit();
63
+ const loadTemplate = uiThreadRpc.createCall(loadTemplateMultiThread);
64
+ const { startMainThread } = prepareMainThreadAPIs(backgroundThreadRpc, document, // rootDom
65
+ document, mtsRealm, (exposureChangedElementUniqueIds) => {
66
+ document.commit();
31
67
  sendMultiThreadExposureChangedEndpoint(exposureChangedElementUniqueIds
32
68
  .map(e => e.getAttribute(lynxUniqueIdAttribute))
33
69
  .filter(id => id !== null));
34
70
  }, markTimingInternal, flushMarkTimingInternal, reportError, triggerI18nResourceFallback, (initI18nResources) => {
35
71
  i18nResources.setData(initI18nResources);
36
72
  return i18nResources;
73
+ }, loadTemplate);
74
+ uiThreadRpc.registerHandler(mainThreadStartEndpoint, async (config) => {
75
+ await startMainThread(config);
76
+ registerUpdateDataHandler(uiThreadRpc, globalThis);
37
77
  });
38
- uiThreadRpc.registerHandler(mainThreadStartEndpoint, (config) => {
39
- startMainThread(config).then((runtime) => {
40
- registerUpdateDataHandler(uiThreadRpc, runtime);
41
- });
42
- });
43
- uiThreadRpc?.registerHandler(updateI18nResourcesEndpoint, data => {
78
+ uiThreadRpc.registerHandler(updateI18nResourcesEndpoint, data => {
44
79
  i18nResources.setData(data);
45
80
  });
46
81
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lynx-js/web-worker-runtime",
3
- "version": "0.15.7",
3
+ "version": "0.16.1",
4
4
  "private": false,
5
5
  "description": "",
6
6
  "keywords": [],
@@ -22,10 +22,10 @@
22
22
  "README.md"
23
23
  ],
24
24
  "dependencies": {
25
- "@lynx-js/offscreen-document": "0.1.3",
26
- "@lynx-js/web-constants": "0.15.7",
27
- "@lynx-js/web-mainthread-apis": "0.15.7",
28
- "@lynx-js/web-worker-rpc": "0.15.7"
25
+ "@lynx-js/offscreen-document": "0.1.4",
26
+ "@lynx-js/web-constants": "0.16.1",
27
+ "@lynx-js/web-mainthread-apis": "0.16.1",
28
+ "@lynx-js/web-worker-rpc": "0.16.1"
29
29
  },
30
30
  "devDependencies": {
31
31
  "@lynx-js/lynx-core": "0.1.3"