@lynx-js/web-worker-runtime 0.10.1 → 0.12.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,46 @@
1
1
  # @lynx-js/web-worker-runtime
2
2
 
3
+ ## 0.12.0
4
+
5
+ ### Patch Changes
6
+
7
+ - feat: fully support MTS ([#569](https://github.com/lynx-family/lynx-stack/pull/569))
8
+
9
+ Now use support the following usage
10
+
11
+ - mainthread event
12
+ - mainthread ref
13
+ - runOnMainThread/runOnBackground
14
+ - ref.current.xx
15
+
16
+ - Updated dependencies [[`f1ca29b`](https://github.com/lynx-family/lynx-stack/commit/f1ca29bd766377dd46583f15e1e75bca447699cd), [`8ca9fcb`](https://github.com/lynx-family/lynx-stack/commit/8ca9fcbbc86b0f0ac05ee4319876cdd5dd08d668), [`efe6fd7`](https://github.com/lynx-family/lynx-stack/commit/efe6fd7de8a3d8119ea550f4d4e939d1fbfee4f0)]:
17
+ - @lynx-js/web-mainthread-apis@0.12.0
18
+ - @lynx-js/web-constants@0.12.0
19
+ - @lynx-js/web-worker-rpc@0.12.0
20
+
21
+ ## 0.11.0
22
+
23
+ ### Patch Changes
24
+
25
+ - feat: support mts event handler (1/n) ([#495](https://github.com/lynx-family/lynx-stack/pull/495))
26
+
27
+ now the main-thread:bind handler could be invoked. The params of the handler will be implemented later.
28
+
29
+ - feat: allow multi lynx-view to share bts worker ([#520](https://github.com/lynx-family/lynx-stack/pull/520))
30
+
31
+ Now we allow users to enable so-called "shared-context" feature on the Web Platform.
32
+
33
+ Similar to the same feature for Lynx iOS/Android, this feature let multi lynx cards to share one js context.
34
+
35
+ The `lynx.getSharedData` and `lynx.setSharedData` are also supported in this commit.
36
+
37
+ To enable this feature, set property `lynxGroupId` or attribute `lynx-group-id` before a lynx-view starts rendering. Those card with same context id will share one web worker for the bts scripts.
38
+
39
+ - Updated dependencies [[`ea42e62`](https://github.com/lynx-family/lynx-stack/commit/ea42e62fbcd5c743132c3e6e7c4851770742d544), [`a0f5ca4`](https://github.com/lynx-family/lynx-stack/commit/a0f5ca4ea0895ccbaa6aa63f449f53a677a1cf73)]:
40
+ - @lynx-js/web-mainthread-apis@0.11.0
41
+ - @lynx-js/web-constants@0.11.0
42
+ - @lynx-js/web-worker-rpc@0.11.0
43
+
3
44
  ## 0.10.1
4
45
 
5
46
  ### Patch Changes
@@ -1,5 +1,6 @@
1
- import type { Cloneable, NativeApp } from '@lynx-js/web-constants';
1
+ import { type Cloneable, type NativeApp } from '@lynx-js/web-constants';
2
2
  import type { Rpc } from '@lynx-js/web-worker-rpc';
3
+ import { LynxCrossThreadContext } from '../../common/LynxCrossThreadContext.js';
3
4
  export interface CreateLynxConfig {
4
5
  globalProps: unknown;
5
6
  customSections: Record<string, Cloneable>;
@@ -8,7 +9,7 @@ export declare function createBackgroundLynx(config: CreateLynxConfig, nativeApp
8
9
  __globalProps: unknown;
9
10
  getJSModule(_moduleName: string): any;
10
11
  getNativeApp(): NativeApp;
11
- getCoreContext(): any;
12
+ getCoreContext(): LynxCrossThreadContext;
12
13
  getCustomSectionSync(key: string): Cloneable;
13
14
  getCustomSection: (key: string, callback: (object: Cloneable) => void) => void;
14
15
  };
@@ -1,8 +1,15 @@
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 { dispatchCoreContextOnBackgroundEndpoint, dispatchJSContextOnMainThreadEndpoint, } from '@lynx-js/web-constants';
4
5
  import { createGetCustomSection } from './crossThreadHandlers/createGetCustomSection.js';
6
+ import { LynxCrossThreadContext } from '../../common/LynxCrossThreadContext.js';
5
7
  export function createBackgroundLynx(config, nativeApp, mainThreadRpc) {
8
+ const coreContext = new LynxCrossThreadContext({
9
+ rpc: mainThreadRpc,
10
+ receiveEventEndpoint: dispatchCoreContextOnBackgroundEndpoint,
11
+ sendEventEndpoint: dispatchJSContextOnMainThreadEndpoint,
12
+ });
6
13
  return {
7
14
  __globalProps: config.globalProps,
8
15
  getJSModule(_moduleName) {
@@ -11,10 +18,7 @@ export function createBackgroundLynx(config, nativeApp, mainThreadRpc) {
11
18
  return nativeApp;
12
19
  },
13
20
  getCoreContext() {
14
- return {
15
- addInternalEventListener() { },
16
- addEventListener() { },
17
- };
21
+ return coreContext;
18
22
  },
19
23
  getCustomSectionSync(key) {
20
24
  return config.customSections[key];
@@ -1,16 +1,15 @@
1
1
  import { callLepusMethodEndpoint, setNativePropsEndpoint, triggerComponentEventEndpoint, selectComponentEndpoint, } from '@lynx-js/web-constants';
2
2
  import { createInvokeUIMethod } from './crossThreadHandlers/createInvokeUIMethod.js';
3
- import { registerOnLifecycleEventHandler } from './crossThreadHandlers/registerOnLifecycleEventHandler.js';
4
3
  import { registerPublicComponentEventHandler } from './crossThreadHandlers/registerPublicComponentEventHandler.js';
5
4
  import { registerGlobalExposureEventHandler } from './crossThreadHandlers/registerGlobalExposureEventHandler.js';
6
5
  import { createNativeModules } from './createNativeModules.js';
7
6
  import { registerUpdateDataHandler } from './crossThreadHandlers/registerUpdateDataHandler.js';
8
7
  import { registerPublishEventHandler } from './crossThreadHandlers/registerPublishEventHandler.js';
9
8
  import { createPerformanceApis } from './createPerformanceApis.js';
10
- import { registerOnNativeAppReadyHandler } from './crossThreadHandlers/registerOnNativeAppReadyHandler.js';
11
9
  import { registerSendGlobalEventHandler } from './crossThreadHandlers/registerSendGlobalEvent.js';
12
10
  import { createJSObjectDestructionObserver } from './crossThreadHandlers/createJSObjectDestructionObserver.js';
13
11
  let nativeAppCount = 0;
12
+ const sharedData = {};
14
13
  export async function createNativeApp(config) {
15
14
  const { mainThreadRpc, uiThreadRpc, template, nativeModulesMap, timingSystem, } = config;
16
15
  const performanceApis = createPerformanceApis(timingSystem);
@@ -52,6 +51,8 @@ export async function createNativeApp(config) {
52
51
  const entry = globalThis.module.exports;
53
52
  return {
54
53
  init: (lynxCoreInject) => {
54
+ lynxCoreInject.tt.lynxCoreInject = lynxCoreInject;
55
+ lynxCoreInject.tt.globalThis ??= lynxCoreInject;
55
56
  return entry?.(lynxCoreInject.tt);
56
57
  },
57
58
  };
@@ -66,18 +67,23 @@ export async function createNativeApp(config) {
66
67
  setNativeProps,
67
68
  invokeUIMethod: createInvokeUIMethod(uiThreadRpc),
68
69
  setCard(tt) {
69
- registerOnLifecycleEventHandler(mainThreadRpc, tt);
70
70
  registerPublicComponentEventHandler(mainThreadRpc, tt);
71
71
  registerPublishEventHandler(mainThreadRpc, tt);
72
72
  registerGlobalExposureEventHandler(mainThreadRpc, tt);
73
73
  registerUpdateDataHandler(uiThreadRpc, tt);
74
- registerOnNativeAppReadyHandler(uiThreadRpc, tt);
75
74
  registerSendGlobalEventHandler(uiThreadRpc, tt);
76
75
  timingSystem.registerGlobalEmitter(tt.GlobalEventEmitter);
76
+ tt.lynx.getCoreContext().__start();
77
77
  },
78
78
  triggerComponentEvent,
79
79
  selectComponent,
80
80
  createJSObjectDestructionObserver: createJSObjectDestructionObserver(),
81
+ setSharedData(dataKey, dataVal) {
82
+ sharedData[dataKey] = dataVal;
83
+ },
84
+ getSharedData(dataKey) {
85
+ return sharedData[dataKey];
86
+ },
81
87
  };
82
88
  return nativeApp;
83
89
  }
@@ -1,3 +1,3 @@
1
1
  import type { NativeApp } from '@lynx-js/web-constants';
2
2
  import type { TimingSystem } from './createTimingSystem.js';
3
- export declare function createPerformanceApis(timingSystem: TimingSystem): Pick<NativeApp, 'generatePipelineOptions' | 'onPipelineStart' | 'markPipelineTiming' | 'bindPipelineIdWithTimingFlag'>;
3
+ export declare function createPerformanceApis(timingSystem: TimingSystem): Pick<NativeApp, 'generatePipelineOptions' | 'onPipelineStart' | 'markPipelineTiming' | 'bindPipelineIdWithTimingFlag' | 'profileStart' | 'profileEnd'>;
@@ -24,6 +24,12 @@ export function createPerformanceApis(timingSystem) {
24
24
  const timingFlags = timingSystem.pipelineIdToTimingFlags.get(pipelineId);
25
25
  timingFlags.push(timingFlag);
26
26
  },
27
+ profileStart: () => {
28
+ console.error('NYI: profileStart. This is an issue of lynx-core.');
29
+ },
30
+ profileEnd: () => {
31
+ console.error('NYI: profileEnd. This is an issue of lynx-core.');
32
+ },
27
33
  };
28
34
  return performanceApis;
29
35
  }
@@ -0,0 +1,12 @@
1
+ import { type dispatchCoreContextOnBackgroundEndpoint, type LynxContextEventTarget, type Rpc } from '@lynx-js/web-constants';
2
+ export declare class LynxCrossThreadContext extends EventTarget implements LynxContextEventTarget {
3
+ private _config;
4
+ constructor(_config: {
5
+ rpc: Rpc;
6
+ receiveEventEndpoint: typeof dispatchCoreContextOnBackgroundEndpoint;
7
+ sendEventEndpoint: typeof dispatchCoreContextOnBackgroundEndpoint;
8
+ });
9
+ postMessage(...args: any[]): void;
10
+ dispatchEvent(event: ContextCrossThreadEvent): 3;
11
+ __start(): void;
12
+ }
@@ -0,0 +1,24 @@
1
+ import { DispatchEventResult, } from '@lynx-js/web-constants';
2
+ export class LynxCrossThreadContext extends EventTarget {
3
+ _config;
4
+ constructor(_config) {
5
+ super();
6
+ this._config = _config;
7
+ }
8
+ postMessage(...args) {
9
+ console.error('[lynx-web] postMessage not implemented, args:', ...args);
10
+ }
11
+ // @ts-expect-error
12
+ dispatchEvent(event) {
13
+ const { rpc, sendEventEndpoint } = this._config;
14
+ rpc.invoke(sendEventEndpoint, [event]);
15
+ return DispatchEventResult.CanceledBeforeDispatch;
16
+ }
17
+ __start() {
18
+ const { rpc, receiveEventEndpoint } = this._config;
19
+ rpc.registerHandler(receiveEventEndpoint, ({ type, data }) => {
20
+ super.dispatchEvent(new MessageEvent(type, { data: data ?? {} }));
21
+ });
22
+ }
23
+ }
24
+ //# sourceMappingURL=LynxCrossThreadContext.js.map
package/dist/index.js CHANGED
@@ -15,7 +15,7 @@ self.onmessage = (ev) => {
15
15
  }
16
16
  };
17
17
  Object.assign(globalThis, {
18
- SystemInfo: { platform: 'web' },
18
+ SystemInfo: { platform: 'web', lynxSdkVersion: '3.0' },
19
19
  module: { exports: null },
20
20
  });
21
21
  //# sourceMappingURL=index.js.map
@@ -1,7 +1,7 @@
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 { BackgroundThreadStartEndpoint, mainThreadChunkReadyEndpoint, mainThreadStartEndpoint, onLifecycleEventEndpoint, flushElementTreeEndpoint, reportErrorEndpoint, publishEventEndpoint, publicComponentEventEndpoint, postExposureEndpoint, postOffscreenEventEndpoint, switchExposureServiceEndpoint, postTimingFlagsEndpoint, } from '@lynx-js/web-constants';
4
+ import { BackgroundThreadStartEndpoint, mainThreadStartEndpoint, flushElementTreeEndpoint, reportErrorEndpoint, publishEventEndpoint, publicComponentEventEndpoint, postExposureEndpoint, postOffscreenEventEndpoint, switchExposureServiceEndpoint, postTimingFlagsEndpoint, dispatchCoreContextOnBackgroundEndpoint, dispatchJSContextOnMainThreadEndpoint, } from '@lynx-js/web-constants';
5
5
  import { Rpc } from '@lynx-js/web-worker-rpc';
6
6
  import { MainThreadRuntime, switchExposureService, } from '@lynx-js/web-mainthread-apis';
7
7
  import { registerCallLepusMethodHandler } from './crossThreadHandlers/registerCallLepusMethodHandler.js';
@@ -10,22 +10,22 @@ import { createMarkTimingInternal } from './crossThreadHandlers/createMainthread
10
10
  import { registerUpdateDataHandler } from './crossThreadHandlers/registerUpdateDataHandler.js';
11
11
  import { OffscreenDocument } from '@lynx-js/offscreen-document/webworker';
12
12
  import { _onEvent, } from '@lynx-js/offscreen-document/webworker';
13
+ import { LynxCrossThreadContext } from '../common/LynxCrossThreadContext.js';
13
14
  export function startMainThread(uiThreadPort, backgroundThreadPort) {
14
15
  const uiThreadRpc = new Rpc(uiThreadPort, 'main-to-ui');
15
16
  const backgroundThreadRpc = new Rpc(backgroundThreadPort, 'main-to-bg');
16
17
  const markTimingInternal = createMarkTimingInternal(backgroundThreadRpc);
17
18
  const postTimingFlags = backgroundThreadRpc.createCall(postTimingFlagsEndpoint);
18
19
  const backgroundStart = backgroundThreadRpc.createCall(BackgroundThreadStartEndpoint);
19
- const __OnLifecycleEvent = backgroundThreadRpc.createCall(onLifecycleEventEndpoint);
20
20
  const publishEvent = backgroundThreadRpc.createCall(publishEventEndpoint);
21
21
  const publicComponentEvent = backgroundThreadRpc.createCall(publicComponentEventEndpoint);
22
22
  const postExposure = backgroundThreadRpc.createCall(postExposureEndpoint);
23
- const mainThreadChunkReady = uiThreadRpc.createCall(mainThreadChunkReadyEndpoint);
24
23
  let operations = [];
25
24
  const flushElementTree = uiThreadRpc.createCall(flushElementTreeEndpoint);
26
25
  const reportError = uiThreadRpc.createCall(reportErrorEndpoint);
27
26
  markTimingInternal('lepus_excute_start');
28
27
  uiThreadRpc.registerHandler(mainThreadStartEndpoint, async (config) => {
28
+ let isFp = true;
29
29
  const { globalProps, template, browserConfig, nativeModulesMap, napiModulesMap, tagMap, } = config;
30
30
  const { styleInfo, pageConfig, customSections, cardType, lepusCode } = template;
31
31
  markTimingInternal('decode_start');
@@ -38,7 +38,13 @@ export function startMainThread(uiThreadPort, backgroundThreadPort) {
38
38
  },
39
39
  });
40
40
  uiThreadRpc.registerHandler(postOffscreenEventEndpoint, docu[_onEvent]);
41
+ const jsContext = new LynxCrossThreadContext({
42
+ rpc: backgroundThreadRpc,
43
+ receiveEventEndpoint: dispatchJSContextOnMainThreadEndpoint,
44
+ sendEventEndpoint: dispatchCoreContextOnBackgroundEndpoint,
45
+ });
41
46
  const runtime = new MainThreadRuntime({
47
+ jsContext,
42
48
  tagMap,
43
49
  browserConfig,
44
50
  customSections,
@@ -48,8 +54,7 @@ export function startMainThread(uiThreadPort, backgroundThreadPort) {
48
54
  lepusCode,
49
55
  docu,
50
56
  callbacks: {
51
- mainChunkReady: function () {
52
- mainThreadChunkReady();
57
+ mainChunkReady: () => {
53
58
  markTimingInternal('data_processor_start');
54
59
  const initData = runtime.processData
55
60
  ? runtime.processData(config.initData)
@@ -75,6 +80,13 @@ export function startMainThread(uiThreadPort, backgroundThreadPort) {
75
80
  const pipelineId = options?.pipelineOptions?.pipelineID;
76
81
  markTimingInternal('dispatch_start', pipelineId);
77
82
  docu.commit();
83
+ if (isFp) {
84
+ isFp = false;
85
+ jsContext.dispatchEvent({
86
+ type: '__OnNativeAppReady',
87
+ data: undefined,
88
+ });
89
+ }
78
90
  markTimingInternal('layout_start', pipelineId);
79
91
  markTimingInternal('ui_operation_flush_start', pipelineId);
80
92
  await flushElementTree(operations);
@@ -84,7 +96,12 @@ export function startMainThread(uiThreadPort, backgroundThreadPort) {
84
96
  postTimingFlags(timingFlags, pipelineId);
85
97
  },
86
98
  _ReportError: reportError,
87
- __OnLifecycleEvent,
99
+ __OnLifecycleEvent: (data) => {
100
+ jsContext.dispatchEvent({
101
+ type: '__OnLifecycleEvent',
102
+ data,
103
+ });
104
+ },
88
105
  /**
89
106
  * Note :
90
107
  * The parameter of lynx.performance.markTiming is (pipelineId:string, timingFlag:string)=>void
@@ -98,6 +115,7 @@ export function startMainThread(uiThreadPort, backgroundThreadPort) {
98
115
  }).globalThis;
99
116
  markTimingInternal('decode_end');
100
117
  entry(runtime);
118
+ jsContext.__start(); // start the jsContext after the runtime is created
101
119
  });
102
120
  }
103
121
  //# sourceMappingURL=startMainThread.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lynx-js/web-worker-runtime",
3
- "version": "0.10.1",
3
+ "version": "0.12.0",
4
4
  "private": false,
5
5
  "description": "",
6
6
  "keywords": [],
@@ -23,14 +23,14 @@
23
23
  ],
24
24
  "dependencies": {
25
25
  "@lynx-js/offscreen-document": "0.0.0",
26
- "@lynx-js/web-constants": "0.10.1",
27
- "@lynx-js/web-mainthread-apis": "0.10.1",
28
- "@lynx-js/web-worker-rpc": "0.10.1"
26
+ "@lynx-js/web-constants": "0.12.0",
27
+ "@lynx-js/web-mainthread-apis": "0.12.0",
28
+ "@lynx-js/web-worker-rpc": "0.12.0"
29
29
  },
30
30
  "devDependencies": {
31
- "@lynx-js/lynx-core": "0.1.0"
31
+ "@lynx-js/lynx-core": "0.1.2"
32
32
  },
33
33
  "peerDependencies": {
34
- "@lynx-js/lynx-core": "0.1.0"
34
+ "@lynx-js/lynx-core": "0.1.2"
35
35
  }
36
36
  }
@@ -1,3 +0,0 @@
1
- import { type NativeTTObject } from '@lynx-js/web-constants';
2
- import type { Rpc } from '@lynx-js/web-worker-rpc';
3
- export declare function registerOnLifecycleEventHandler(rpc: Rpc, tt: NativeTTObject): void;
@@ -1,8 +0,0 @@
1
- // Copyright 2023 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 { onLifecycleEventEndpoint, } from '@lynx-js/web-constants';
5
- export function registerOnLifecycleEventHandler(rpc, tt) {
6
- rpc.registerHandlerLazy(onLifecycleEventEndpoint, tt, 'OnLifecycleEvent');
7
- }
8
- //# sourceMappingURL=registerOnLifecycleEventHandler.js.map
@@ -1,3 +0,0 @@
1
- import { type NativeTTObject } from '@lynx-js/web-constants';
2
- import type { Rpc } from '@lynx-js/web-worker-rpc';
3
- export declare function registerOnNativeAppReadyHandler(uiThreadRpc: Rpc, tt: NativeTTObject): void;
@@ -1,8 +0,0 @@
1
- // Copyright 2023 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 { uiThreadFpReadyEndpoint, } from '@lynx-js/web-constants';
5
- export function registerOnNativeAppReadyHandler(uiThreadRpc, tt) {
6
- uiThreadRpc.registerHandlerLazy(uiThreadFpReadyEndpoint, tt, 'onNativeAppReady');
7
- }
8
- //# sourceMappingURL=registerOnNativeAppReadyHandler.js.map