@lynx-js/web-core 0.10.1 → 0.11.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,39 @@
1
1
  # @lynx-js/web-core
2
2
 
3
+ ## 0.11.0
4
+
5
+ ### Minor Changes
6
+
7
+ - feat: upgrade @lynx-js/lynx-core to 0.1.2 ([#465](https://github.com/lynx-family/lynx-stack/pull/465))
8
+
9
+ refactor some internal logic
10
+
11
+ - \_\_OnLifeCycleEvent
12
+ - \_\_OnNativeAppReady
13
+
14
+ ### Patch Changes
15
+
16
+ - feat: support mts event handler (1/n) ([#495](https://github.com/lynx-family/lynx-stack/pull/495))
17
+
18
+ now the main-thread:bind handler could be invoked. The params of the handler will be implemented later.
19
+
20
+ - feat: allow multi lynx-view to share bts worker ([#520](https://github.com/lynx-family/lynx-stack/pull/520))
21
+
22
+ Now we allow users to enable so-called "shared-context" feature on the Web Platform.
23
+
24
+ Similar to the same feature for Lynx iOS/Android, this feature let multi lynx cards to share one js context.
25
+
26
+ The `lynx.getSharedData` and `lynx.setSharedData` are also supported in this commit.
27
+
28
+ 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.
29
+
30
+ - perf: dispatchLynxViewEventEndpoint is a void call ([#506](https://github.com/lynx-family/lynx-stack/pull/506))
31
+
32
+ - Updated dependencies [[`ea42e62`](https://github.com/lynx-family/lynx-stack/commit/ea42e62fbcd5c743132c3e6e7c4851770742d544), [`a0f5ca4`](https://github.com/lynx-family/lynx-stack/commit/a0f5ca4ea0895ccbaa6aa63f449f53a677a1cf73)]:
33
+ - @lynx-js/web-worker-runtime@0.11.0
34
+ - @lynx-js/web-constants@0.11.0
35
+ - @lynx-js/web-worker-rpc@0.11.0
36
+
3
37
  ## 0.10.1
4
38
 
5
39
  ### Patch Changes
@@ -21,6 +21,7 @@ export type INapiModulesCall = (name: string, data: any, moduleName: string, lyn
21
21
  * @property {NapiModulesMap} napiModulesMap [optional] the napiModule which is called in lynx-core. key is module-name, value is esm url.
22
22
  * @property {INapiModulesCall} onNapiModulesCall [optional] the NapiModule value handler.
23
23
  * @property {"false" | "true" | null} injectHeadLinks [optional] @default true set it to "false" to disable injecting the <link href="" ref="stylesheet"> styles into shadowroot
24
+ * @property {number} lynxGroupId [optional] (attribute: "lynx-group-id") the background shared context id, which is used to share webworker between different lynx cards
24
25
  *
25
26
  * @event error lynx card fired an error
26
27
  *
@@ -102,6 +103,12 @@ export declare class LynxView extends HTMLElement {
102
103
  */
103
104
  get onNapiModulesCall(): NapiModulesCall | undefined;
104
105
  set onNapiModulesCall(handler: INapiModulesCall);
106
+ /**
107
+ * @param
108
+ * @property
109
+ */
110
+ get lynxGroupId(): number | undefined;
111
+ set lynxGroupId(val: number | undefined);
105
112
  /**
106
113
  * @public
107
114
  * @method
@@ -19,6 +19,7 @@ import { inShadowRootStyles } from './inShadowRootStyles.js';
19
19
  * @property {NapiModulesMap} napiModulesMap [optional] the napiModule which is called in lynx-core. key is module-name, value is esm url.
20
20
  * @property {INapiModulesCall} onNapiModulesCall [optional] the NapiModule value handler.
21
21
  * @property {"false" | "true" | null} injectHeadLinks [optional] @default true set it to "false" to disable injecting the <link href="" ref="stylesheet"> styles into shadowroot
22
+ * @property {number} lynxGroupId [optional] (attribute: "lynx-group-id") the background shared context id, which is used to share webworker between different lynx cards
22
23
  *
23
24
  * @event error lynx card fired an error
24
25
  *
@@ -168,6 +169,23 @@ export class LynxView extends HTMLElement {
168
169
  return handler(name, data, moduleName, this, dispatchNapiModules);
169
170
  };
170
171
  }
172
+ /**
173
+ * @param
174
+ * @property
175
+ */
176
+ get lynxGroupId() {
177
+ return this.getAttribute('lynx-group-id')
178
+ ? Number(this.getAttribute('lynx-group-id'))
179
+ : undefined;
180
+ }
181
+ set lynxGroupId(val) {
182
+ if (val) {
183
+ this.setAttribute('lynx-group-id', val.toString());
184
+ }
185
+ else {
186
+ this.removeAttribute('lynx-group-id');
187
+ }
188
+ }
171
189
  /**
172
190
  * @public
173
191
  * @method
@@ -262,6 +280,7 @@ export class LynxView extends HTMLElement {
262
280
  if (!this.shadowRoot) {
263
281
  this.attachShadow({ mode: 'open' });
264
282
  }
283
+ const lynxGroupId = this.lynxGroupId;
265
284
  const lynxView = createLynxView({
266
285
  tagMap,
267
286
  shadowRoot: this.shadowRoot,
@@ -270,6 +289,7 @@ export class LynxView extends HTMLElement {
270
289
  initData: this.#initData,
271
290
  nativeModulesMap: this.#nativeModulesMap,
272
291
  napiModulesMap: this.#napiModulesMap,
292
+ lynxGroupId,
273
293
  callbacks: {
274
294
  nativeModulesCall: (...args) => {
275
295
  if (this.#onNativeModulesCall) {
@@ -6,10 +6,11 @@ export interface LynxViewConfigs {
6
6
  initData: Cloneable;
7
7
  globalProps: Cloneable;
8
8
  shadowRoot: ShadowRoot;
9
- callbacks: Parameters<typeof startUIThread>[3];
9
+ callbacks: Parameters<typeof startUIThread>[4];
10
10
  nativeModulesMap: NativeModulesMap;
11
11
  napiModulesMap: NapiModulesMap;
12
12
  tagMap: Record<string, string>;
13
+ lynxGroupId: number | undefined;
13
14
  }
14
15
  export interface LynxView {
15
16
  updateData(data: Cloneable, updateDataType: UpdateDataType, callback?: () => void): void;
@@ -3,7 +3,7 @@
3
3
  // LICENSE file in the root directory of this source tree.
4
4
  import { startUIThread } from '../uiThread/startUIThread.js';
5
5
  export function createLynxView(configs) {
6
- const { shadowRoot, callbacks, templateUrl, globalProps, initData, nativeModulesMap, napiModulesMap, tagMap, } = configs;
6
+ const { shadowRoot, callbacks, templateUrl, globalProps, initData, nativeModulesMap, napiModulesMap, tagMap, lynxGroupId, } = configs;
7
7
  return startUIThread(templateUrl, {
8
8
  tagMap,
9
9
  initData,
@@ -11,6 +11,6 @@ export function createLynxView(configs) {
11
11
  nativeModulesMap,
12
12
  napiModulesMap,
13
13
  browserConfig: {},
14
- }, shadowRoot, callbacks);
14
+ }, shadowRoot, lynxGroupId, callbacks);
15
15
  }
16
16
  //# sourceMappingURL=createLynxView.js.map
@@ -4,5 +4,5 @@ interface LynxViewRpc {
4
4
  backgroundRpc: Rpc;
5
5
  terminateWorkers: () => void;
6
6
  }
7
- export declare function bootWorkers(): LynxViewRpc;
7
+ export declare function bootWorkers(lynxGroupId: number | undefined): LynxViewRpc;
8
8
  export {};
@@ -2,24 +2,41 @@
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 { Rpc } from '@lynx-js/web-worker-rpc';
5
+ const backgroundWorkerContextCount = [];
6
+ const contextIdToBackgroundWorker = [];
5
7
  let preHeatedMainWorker = createMainWorker();
6
- export function bootWorkers() {
8
+ export function bootWorkers(lynxGroupId) {
7
9
  const curMainWorker = preHeatedMainWorker;
8
- const curBackgroundWorker = createBackgroundWorker(curMainWorker.channelMainThreadWithBackground);
9
10
  preHeatedMainWorker = createMainWorker();
11
+ const curBackgroundWorker = createBackgroundWorker(lynxGroupId, curMainWorker.channelMainThreadWithBackground);
12
+ if (lynxGroupId !== undefined) {
13
+ if (backgroundWorkerContextCount[lynxGroupId]) {
14
+ backgroundWorkerContextCount[lynxGroupId]++;
15
+ }
16
+ else {
17
+ backgroundWorkerContextCount[lynxGroupId] = 1;
18
+ }
19
+ }
10
20
  return {
11
21
  mainThreadRpc: curMainWorker.mainThreadRpc,
12
22
  backgroundRpc: curBackgroundWorker.backgroundRpc,
13
23
  terminateWorkers: () => {
14
24
  curMainWorker.mainThreadWorker.terminate();
15
- curBackgroundWorker.backgroundThreadWorker.terminate();
25
+ if (lynxGroupId === undefined) {
26
+ curBackgroundWorker.backgroundThreadWorker.terminate();
27
+ }
28
+ else if (backgroundWorkerContextCount[lynxGroupId] === 1) {
29
+ curBackgroundWorker.backgroundThreadWorker.terminate();
30
+ backgroundWorkerContextCount[lynxGroupId] = 0;
31
+ contextIdToBackgroundWorker[lynxGroupId] = undefined;
32
+ }
16
33
  },
17
34
  };
18
35
  }
19
36
  function createMainWorker() {
20
37
  const channelToMainThread = new MessageChannel();
21
38
  const channelMainThreadWithBackground = new MessageChannel();
22
- const mainThreadWorker = createWebWorker();
39
+ const mainThreadWorker = createWebWorker('lynx-main');
23
40
  const mainThreadMessage = {
24
41
  mode: 'main',
25
42
  toUIThread: channelToMainThread.port2,
@@ -37,9 +54,17 @@ function createMainWorker() {
37
54
  channelMainThreadWithBackground,
38
55
  };
39
56
  }
40
- function createBackgroundWorker(channelMainThreadWithBackground) {
57
+ function createBackgroundWorker(lynxGroupId, channelMainThreadWithBackground) {
41
58
  const channelToBackground = new MessageChannel();
42
- const backgroundThreadWorker = createWebWorker();
59
+ let backgroundThreadWorker;
60
+ if (lynxGroupId) {
61
+ backgroundThreadWorker = contextIdToBackgroundWorker[lynxGroupId]
62
+ ?? createWebWorker('lynx-bg');
63
+ contextIdToBackgroundWorker[lynxGroupId] = backgroundThreadWorker;
64
+ }
65
+ else {
66
+ backgroundThreadWorker = createWebWorker('lynx-bg');
67
+ }
43
68
  const backgroundThreadMessage = {
44
69
  mode: 'background',
45
70
  toUIThread: channelToBackground.port2,
@@ -53,10 +78,10 @@ function createBackgroundWorker(channelMainThreadWithBackground) {
53
78
  const backgroundRpc = new Rpc(channelToBackground.port1, 'ui-to-bg');
54
79
  return { backgroundRpc, backgroundThreadWorker };
55
80
  }
56
- function createWebWorker() {
81
+ function createWebWorker(name) {
57
82
  return new Worker(new URL('@lynx-js/web-worker-runtime', import.meta.url), {
58
83
  type: 'module',
59
- name: `lynx-web`,
84
+ name,
60
85
  });
61
86
  }
62
87
  //# sourceMappingURL=bootWorkers.js.map
@@ -1,6 +1,4 @@
1
1
  import type { Rpc } from '@lynx-js/web-worker-rpc';
2
2
  export declare function registerFlushElementTreeHandler(mainThreadRpc: Rpc, options: {
3
3
  shadowRoot: ShadowRoot;
4
- }, onCommit: (info: {
5
- isFP: boolean;
6
- }) => void): void;
4
+ }): void;
@@ -3,22 +3,15 @@
3
3
  // LICENSE file in the root directory of this source tree.
4
4
  import { flushElementTreeEndpoint, postOffscreenEventEndpoint, } from '@lynx-js/web-constants';
5
5
  import { initOffscreenDocument } from '@lynx-js/offscreen-document/main';
6
- export function registerFlushElementTreeHandler(mainThreadRpc, options, onCommit) {
6
+ export function registerFlushElementTreeHandler(mainThreadRpc, options) {
7
7
  const { shadowRoot, } = options;
8
8
  const onEvent = mainThreadRpc.createCall(postOffscreenEventEndpoint);
9
9
  const { decodeOperation } = initOffscreenDocument({
10
10
  shadowRoot,
11
11
  onEvent,
12
12
  });
13
- let isFP = true;
14
13
  mainThreadRpc.registerHandler(flushElementTreeEndpoint, (operations) => {
15
14
  decodeOperation(operations);
16
- onCommit({
17
- isFP,
18
- });
19
- if (isFP) {
20
- isFP = false;
21
- }
22
15
  });
23
16
  }
24
17
  //# sourceMappingURL=registerFlushElementTreeHandler.js.map
@@ -1,6 +1,6 @@
1
1
  import type { LynxView } from '../apis/createLynxView.js';
2
2
  import { type MainThreadStartConfigs, type NapiModulesCall, type NativeModulesCall } from '@lynx-js/web-constants';
3
- export declare function startUIThread(templateUrl: string, configs: Omit<MainThreadStartConfigs, 'template'>, shadowRoot: ShadowRoot, callbacks: {
3
+ export declare function startUIThread(templateUrl: string, configs: Omit<MainThreadStartConfigs, 'template'>, shadowRoot: ShadowRoot, lynxGroupId: number | undefined, callbacks: {
4
4
  nativeModulesCall: NativeModulesCall;
5
5
  napiModulesCall: NapiModulesCall;
6
6
  onError?: () => void;
@@ -10,17 +10,16 @@ import { registerFlushElementTreeHandler } from './crossThreadHandlers/registerF
10
10
  import { createDispose } from './crossThreadHandlers/createDispose.js';
11
11
  import { registerTriggerComponentEventHandler } from './crossThreadHandlers/registerTriggerComponentEventHandler.js';
12
12
  import { registerSelectComponentHandler } from './crossThreadHandlers/registerSelectComponentHandler.js';
13
- import { mainThreadChunkReadyEndpoint, mainThreadStartEndpoint, markTimingEndpoint, sendGlobalEventEndpoint, uiThreadFpReadyEndpoint, } from '@lynx-js/web-constants';
13
+ import { mainThreadStartEndpoint, markTimingEndpoint, sendGlobalEventEndpoint, } from '@lynx-js/web-constants';
14
14
  import { loadTemplate } from '../utils/loadTemplate.js';
15
15
  import { createUpdateData } from './crossThreadHandlers/createUpdateData.js';
16
16
  import { registerNapiModulesCallHandler } from './crossThreadHandlers/registerNapiModulesCallHandler.js';
17
17
  import { registerDispatchLynxViewEventHandler } from './crossThreadHandlers/registerDispatchLynxViewEventHandler.js';
18
- export function startUIThread(templateUrl, configs, shadowRoot, callbacks) {
18
+ export function startUIThread(templateUrl, configs, shadowRoot, lynxGroupId, callbacks) {
19
19
  const createLynxStartTiming = performance.now() + performance.timeOrigin;
20
20
  const { nativeModulesMap, napiModulesMap } = configs;
21
- const { mainThreadRpc, backgroundRpc, terminateWorkers, } = bootWorkers();
21
+ const { mainThreadRpc, backgroundRpc, terminateWorkers, } = bootWorkers(lynxGroupId);
22
22
  const sendGlobalEvent = backgroundRpc.createCall(sendGlobalEventEndpoint);
23
- const uiThreadFpReady = backgroundRpc.createCall(uiThreadFpReadyEndpoint);
24
23
  const mainThreadStart = mainThreadRpc.createCall(mainThreadStartEndpoint);
25
24
  const markTiming = backgroundRpc.createCall(markTimingEndpoint);
26
25
  const markTimingInternal = (timingKey, pipelineId, timeStamp) => {
@@ -41,20 +40,13 @@ export function startUIThread(templateUrl, configs, shadowRoot, callbacks) {
41
40
  });
42
41
  registerReportErrorHandler(mainThreadRpc, callbacks.onError);
43
42
  registerDispatchLynxViewEventHandler(backgroundRpc, shadowRoot);
44
- mainThreadRpc.registerHandler(mainThreadChunkReadyEndpoint, () => {
45
- registerFlushElementTreeHandler(mainThreadRpc, {
46
- shadowRoot,
47
- }, (info) => {
48
- const { isFP } = info;
49
- if (isFP) {
50
- registerInvokeUIMethodHandler(backgroundRpc, shadowRoot);
51
- registerNativePropsHandler(backgroundRpc, shadowRoot);
52
- registerTriggerComponentEventHandler(backgroundRpc, shadowRoot);
53
- registerSelectComponentHandler(backgroundRpc, shadowRoot);
54
- uiThreadFpReady();
55
- }
56
- });
43
+ registerFlushElementTreeHandler(mainThreadRpc, {
44
+ shadowRoot,
57
45
  });
46
+ registerInvokeUIMethodHandler(backgroundRpc, shadowRoot);
47
+ registerNativePropsHandler(backgroundRpc, shadowRoot);
48
+ registerTriggerComponentEventHandler(backgroundRpc, shadowRoot);
49
+ registerSelectComponentHandler(backgroundRpc, shadowRoot);
58
50
  registerNativeModulesCallHandler(backgroundRpc, callbacks.nativeModulesCall);
59
51
  registerNapiModulesCallHandler(backgroundRpc, callbacks.napiModulesCall);
60
52
  return {
@@ -81,6 +81,7 @@ const mainThreadInjectVars = [
81
81
  '__SetCSSId',
82
82
  '__OnLifecycleEvent',
83
83
  '__FlushElementTree',
84
+ '__LoadLepusChunk',
84
85
  ];
85
86
  const backgroundInjectVars = [
86
87
  'NativeModules',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lynx-js/web-core",
3
- "version": "0.10.1",
3
+ "version": "0.11.0",
4
4
  "private": false,
5
5
  "description": "",
6
6
  "keywords": [],
@@ -25,16 +25,16 @@
25
25
  ],
26
26
  "dependencies": {
27
27
  "@lynx-js/offscreen-document": "0.0.0",
28
- "@lynx-js/web-constants": "0.10.1",
29
- "@lynx-js/web-worker-rpc": "0.10.1",
30
- "@lynx-js/web-worker-runtime": "0.10.1"
28
+ "@lynx-js/web-constants": "0.11.0",
29
+ "@lynx-js/web-worker-rpc": "0.11.0",
30
+ "@lynx-js/web-worker-runtime": "0.11.0"
31
31
  },
32
32
  "devDependencies": {
33
- "@lynx-js/lynx-core": "0.1.0",
34
- "@lynx-js/web-elements": "0.5.3"
33
+ "@lynx-js/lynx-core": "0.1.2",
34
+ "@lynx-js/web-elements": "0.5.4"
35
35
  },
36
36
  "peerDependencies": {
37
- "@lynx-js/lynx-core": "0.1.0",
37
+ "@lynx-js/lynx-core": "0.1.2",
38
38
  "@lynx-js/web-elements": ">=0.3.1"
39
39
  }
40
40
  }