@lynx-js/web-mainthread-apis 0.13.4 → 0.13.5
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 +31 -0
- package/dist/createMainThreadGlobalThis.d.ts +24 -0
- package/dist/createMainThreadGlobalThis.js +450 -0
- package/dist/createMainThreadLynx.d.ts +3 -0
- package/dist/{MainThreadLynx.js → createMainThreadLynx.js} +2 -5
- package/dist/crossThreadHandlers/registerCallLepusMethodHandler.d.ts +2 -3
- package/dist/crossThreadHandlers/registerCallLepusMethodHandler.js +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/prepareMainThreadAPIs.d.ts +2 -3
- package/dist/prepareMainThreadAPIs.js +17 -14
- package/dist/pureElementPAPIs.d.ts +32 -0
- package/dist/pureElementPAPIs.js +140 -0
- package/dist/utils/createCrossThreadEvent.d.ts +2 -3
- package/dist/utils/createCrossThreadEvent.js +23 -12
- package/dist/utils/createExposureService.d.ts +2 -2
- package/dist/utils/createExposureService.js +4 -5
- package/package.json +2 -2
- package/dist/MainThreadLynx.d.ts +0 -10
- package/dist/MainThreadRuntime.d.ts +0 -92
- package/dist/MainThreadRuntime.js +0 -186
- package/dist/elementAPI/ElementThreadElement.d.ts +0 -27
- package/dist/elementAPI/ElementThreadElement.js +0 -2
- package/dist/elementAPI/attributeAndProperty/attributeAndPropertyFunctions.d.ts +0 -30
- package/dist/elementAPI/attributeAndProperty/attributeAndPropertyFunctions.js +0 -127
- package/dist/elementAPI/domTree/domTreeFunctions.d.ts +0 -11
- package/dist/elementAPI/domTree/domTreeFunctions.js +0 -49
- package/dist/elementAPI/elementCreating/elementCreatingFunctions.d.ts +0 -15
- package/dist/elementAPI/elementCreating/elementCreatingFunctions.js +0 -123
- package/dist/elementAPI/event/eventFunctions.d.ts +0 -25
- package/dist/elementAPI/event/eventFunctions.js +0 -142
- package/dist/elementAPI/style/styleFunctions.d.ts +0 -10
- package/dist/elementAPI/style/styleFunctions.js +0 -89
- /package/dist/{elementAPI/style → style}/cssPropertyMap.d.ts +0 -0
- /package/dist/{elementAPI/style → style}/cssPropertyMap.js +0 -0
- /package/dist/{elementAPI/style → style}/transformInlineStyle.d.ts +0 -0
- /package/dist/{elementAPI/style → style}/transformInlineStyle.js +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,36 @@
|
|
|
1
1
|
# @lynx-js/web-mainthread-apis
|
|
2
2
|
|
|
3
|
+
## 0.13.5
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- refactor: move some internal status to dom's attribute ([#945](https://github.com/lynx-family/lynx-stack/pull/945))
|
|
8
|
+
|
|
9
|
+
It's essential for SSR
|
|
10
|
+
|
|
11
|
+
- refactor: avoid to create many style element for cssog ([#1026](https://github.com/lynx-family/lynx-stack/pull/1026))
|
|
12
|
+
|
|
13
|
+
- fix: target.id is undefined ([#1016](https://github.com/lynx-family/lynx-stack/pull/1016))
|
|
14
|
+
|
|
15
|
+
- feat: add new pageConfig configuration: enableJSDataProcessor ([#886](https://github.com/lynx-family/lynx-stack/pull/886))
|
|
16
|
+
|
|
17
|
+
- refactor: move component config info to attribute ([#984](https://github.com/lynx-family/lynx-stack/pull/984))
|
|
18
|
+
|
|
19
|
+
- refactor: save dataset on an attribute ([#981](https://github.com/lynx-family/lynx-stack/pull/981))
|
|
20
|
+
|
|
21
|
+
On lynx, the `data-*` attributes have different behaviors than the HTMLElement has.
|
|
22
|
+
|
|
23
|
+
The dataset will be treated as properties, the key will not be applied the camel-case <-> hyphenate name transformation.
|
|
24
|
+
|
|
25
|
+
Before this commit we use it as a runtime data, but after this commit we will use encodeURI(JSON.stringify(dataset)) to encode it as a string.
|
|
26
|
+
|
|
27
|
+
- refactor: create elements of `elementToRuntimeInfoMap` on demand ([#986](https://github.com/lynx-family/lynx-stack/pull/986))
|
|
28
|
+
|
|
29
|
+
- refactor: implement mts apis in closure pattern ([#1004](https://github.com/lynx-family/lynx-stack/pull/1004))
|
|
30
|
+
|
|
31
|
+
- Updated dependencies [[`70b82d2`](https://github.com/lynx-family/lynx-stack/commit/70b82d23744d6b6ec945dff9f8895ab3488ba4c8), [`9499ea9`](https://github.com/lynx-family/lynx-stack/commit/9499ea91debdf73b2d31af0b31bcbc216135543b), [`50f0193`](https://github.com/lynx-family/lynx-stack/commit/50f01933942268b697bf5abe790da86c932f1dfc), [`57bf0ef`](https://github.com/lynx-family/lynx-stack/commit/57bf0ef19f1d79bc52ab6a4f0cd2939e7901d98b), [`0525fbf`](https://github.com/lynx-family/lynx-stack/commit/0525fbf38baa7a977a7a8c66e8a4d8bf34cc3b68), [`b6b87fd`](https://github.com/lynx-family/lynx-stack/commit/b6b87fd11dbc76c28f3b5022aa8c6afeb773d90f), [`c014327`](https://github.com/lynx-family/lynx-stack/commit/c014327ad0cf599b32d4182d95116b46c35f5fa5)]:
|
|
32
|
+
- @lynx-js/web-constants@0.13.5
|
|
33
|
+
|
|
3
34
|
## 0.13.4
|
|
4
35
|
|
|
5
36
|
### Patch Changes
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type LynxTemplate, type PageConfig, type StyleInfo, type FlushElementTreeOptions, type Cloneable, type BrowserConfig, type publishEventEndpoint, type publicComponentEventEndpoint, type reportErrorEndpoint, type RpcCallType, type LynxContextEventTarget, type LynxJSModule, type MainThreadGlobalThis, type WebFiberElementImpl } from '@lynx-js/web-constants';
|
|
2
|
+
export interface MainThreadRuntimeCallbacks {
|
|
3
|
+
mainChunkReady: () => void;
|
|
4
|
+
flushElementTree: (options: FlushElementTreeOptions, timingFlags: string[]) => void;
|
|
5
|
+
_ReportError: RpcCallType<typeof reportErrorEndpoint>;
|
|
6
|
+
__OnLifecycleEvent: (lifeCycleEvent: Cloneable) => void;
|
|
7
|
+
markTiming: (pipelineId: string, timingKey: string) => void;
|
|
8
|
+
publishEvent: RpcCallType<typeof publishEventEndpoint>;
|
|
9
|
+
publicComponentEvent: RpcCallType<typeof publicComponentEventEndpoint>;
|
|
10
|
+
createElement: (tag: string) => WebFiberElementImpl;
|
|
11
|
+
}
|
|
12
|
+
export interface MainThreadRuntimeConfig {
|
|
13
|
+
pageConfig: PageConfig;
|
|
14
|
+
globalProps: unknown;
|
|
15
|
+
callbacks: MainThreadRuntimeCallbacks;
|
|
16
|
+
styleInfo: StyleInfo;
|
|
17
|
+
customSections: LynxTemplate['customSections'];
|
|
18
|
+
lepusCode: Record<string, LynxJSModule>;
|
|
19
|
+
browserConfig: BrowserConfig;
|
|
20
|
+
tagMap: Record<string, string>;
|
|
21
|
+
rootDom: Pick<Element, 'append' | 'addEventListener'>;
|
|
22
|
+
jsContext: LynxContextEventTarget;
|
|
23
|
+
}
|
|
24
|
+
export declare function createMainThreadGlobalThis(config: MainThreadRuntimeConfig): MainThreadGlobalThis;
|
|
@@ -0,0 +1,450 @@
|
|
|
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 { lynxUniqueIdAttribute, systemInfo, parentComponentUniqueIdAttribute, componentIdAttribute, LynxEventNameToW3cByTagName, LynxEventNameToW3cCommon, lynxTagAttribute, W3cEventNameToLynx, cssIdAttribute, lynxDefaultDisplayLinearAttribute, __lynx_timing_flag, } from '@lynx-js/web-constants';
|
|
5
|
+
import { globalMuteableVars } from '@lynx-js/web-constants';
|
|
6
|
+
import { createMainThreadLynx } from './createMainThreadLynx.js';
|
|
7
|
+
import { flattenStyleInfo, genCssContent, genCssInJsInfo, transformToWebCss, } from './utils/processStyleInfo.js';
|
|
8
|
+
import { __AddClass, __AddConfig, __AddDataset, __AddInlineStyle, __AppendElement, __ElementIsEqual, __FirstElement, __GetAttributes, __GetChildren, __GetClasses, __GetComponentID, __GetDataByKey, __GetDataset, __GetElementConfig, __GetElementUniqueID, __GetID, __GetParent, __GetTag, __InsertElementBefore, __LastElement, __NextElement, __RemoveElement, __ReplaceElement, __ReplaceElements, __SetClasses, __SetConfig, __SetCSSId, __SetDataset, __SetID, __SetInlineStyles, __UpdateComponentID, } from './pureElementPAPIs.js';
|
|
9
|
+
import { createCrossThreadEvent } from './utils/createCrossThreadEvent.js';
|
|
10
|
+
import { decodeCssInJs } from './utils/decodeCssInJs.js';
|
|
11
|
+
export function createMainThreadGlobalThis(config) {
|
|
12
|
+
let pageElement;
|
|
13
|
+
let uniqueIdInc = 1;
|
|
14
|
+
let timingFlags = [];
|
|
15
|
+
let renderPage;
|
|
16
|
+
const { callbacks, tagMap, pageConfig, lepusCode, rootDom, globalProps, styleInfo, } = config;
|
|
17
|
+
const lynxUniqueIdToElement = [];
|
|
18
|
+
const elementToRuntimeInfoMap = new WeakMap();
|
|
19
|
+
const lynxUniqueIdToStyleRulesIndex = [];
|
|
20
|
+
/**
|
|
21
|
+
* for "update" the globalThis.val in the main thread
|
|
22
|
+
*/
|
|
23
|
+
const varsUpdateHandlers = [];
|
|
24
|
+
const lynxGlobalBindingValues = {};
|
|
25
|
+
/**
|
|
26
|
+
* now create the style content
|
|
27
|
+
* 1. flatten the styleInfo
|
|
28
|
+
* 2. transform the styleInfo to web css
|
|
29
|
+
* 3. generate the css in js info
|
|
30
|
+
* 4. create the style element
|
|
31
|
+
* 5. append the style element to the root dom
|
|
32
|
+
*/
|
|
33
|
+
flattenStyleInfo(styleInfo, pageConfig.enableCSSSelector);
|
|
34
|
+
transformToWebCss(styleInfo);
|
|
35
|
+
const cssInJsInfo = pageConfig.enableCSSSelector
|
|
36
|
+
? {}
|
|
37
|
+
: genCssInJsInfo(styleInfo);
|
|
38
|
+
const cardStyleElement = callbacks.createElement('style');
|
|
39
|
+
cardStyleElement.innerHTML = genCssContent(styleInfo, pageConfig);
|
|
40
|
+
// @ts-expect-error
|
|
41
|
+
rootDom.append(cardStyleElement);
|
|
42
|
+
const cardStyleElementSheet = cardStyleElement.sheet;
|
|
43
|
+
const updateCSSInJsStyle = (uniqueId, newStyles) => {
|
|
44
|
+
if (lynxUniqueIdToStyleRulesIndex[uniqueId] !== undefined) {
|
|
45
|
+
const rule = cardStyleElementSheet
|
|
46
|
+
.cssRules[lynxUniqueIdToStyleRulesIndex[uniqueId]];
|
|
47
|
+
rule.style.cssText = newStyles;
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
const index = cardStyleElementSheet.insertRule(`[${lynxUniqueIdAttribute}="${uniqueId}"]{${newStyles}}`, cardStyleElementSheet.cssRules.length);
|
|
51
|
+
lynxUniqueIdToStyleRulesIndex[uniqueId] = index;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
const commonHandler = (event) => {
|
|
55
|
+
if (!event.currentTarget) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const currentTarget = event.currentTarget;
|
|
59
|
+
const isCapture = event.eventPhase === Event.CAPTURING_PHASE;
|
|
60
|
+
const lynxEventName = W3cEventNameToLynx[event.type] ?? event.type;
|
|
61
|
+
const runtimeInfo = elementToRuntimeInfoMap.get(currentTarget);
|
|
62
|
+
if (runtimeInfo) {
|
|
63
|
+
const hname = isCapture
|
|
64
|
+
? runtimeInfo.eventHandlerMap[lynxEventName]?.capture
|
|
65
|
+
?.handler
|
|
66
|
+
: runtimeInfo.eventHandlerMap[lynxEventName]?.bind
|
|
67
|
+
?.handler;
|
|
68
|
+
const crossThreadEvent = createCrossThreadEvent(event, lynxEventName);
|
|
69
|
+
if (typeof hname === 'string') {
|
|
70
|
+
const parentComponentUniqueId = Number(currentTarget.getAttribute(parentComponentUniqueIdAttribute));
|
|
71
|
+
const parentComponent = lynxUniqueIdToElement[parentComponentUniqueId]
|
|
72
|
+
.deref();
|
|
73
|
+
const componentId = parentComponent?.getAttribute(lynxTagAttribute) !== 'page'
|
|
74
|
+
? parentComponent?.getAttribute(componentIdAttribute) ?? undefined
|
|
75
|
+
: undefined;
|
|
76
|
+
if (componentId) {
|
|
77
|
+
callbacks.publicComponentEvent(componentId, hname, crossThreadEvent);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
callbacks.publishEvent(hname, crossThreadEvent);
|
|
81
|
+
}
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
else if (hname) {
|
|
85
|
+
crossThreadEvent.target.elementRefptr =
|
|
86
|
+
event.target;
|
|
87
|
+
if (crossThreadEvent.currentTarget) {
|
|
88
|
+
crossThreadEvent.currentTarget
|
|
89
|
+
.elementRefptr = event.currentTarget;
|
|
90
|
+
}
|
|
91
|
+
mtsGlobalThis.runWorklet?.(hname.value, [crossThreadEvent]);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return false;
|
|
95
|
+
};
|
|
96
|
+
const commonCatchHandler = (event) => {
|
|
97
|
+
const handlerTriggered = commonHandler(event);
|
|
98
|
+
if (handlerTriggered)
|
|
99
|
+
event.stopPropagation();
|
|
100
|
+
};
|
|
101
|
+
const __AddEvent = (element, eventType, eventName, newEventHandler) => {
|
|
102
|
+
eventName = eventName.toLowerCase();
|
|
103
|
+
const isCatch = eventType === 'catchEvent' || eventType === 'capture-catch';
|
|
104
|
+
const isCapture = eventType.startsWith('capture');
|
|
105
|
+
const runtimeInfo = elementToRuntimeInfoMap.get(element) ?? {
|
|
106
|
+
eventHandlerMap: {},
|
|
107
|
+
componentAtIndex: undefined,
|
|
108
|
+
enqueueComponent: undefined,
|
|
109
|
+
};
|
|
110
|
+
const currentHandler = isCapture
|
|
111
|
+
? runtimeInfo.eventHandlerMap[eventName]?.capture
|
|
112
|
+
: runtimeInfo.eventHandlerMap[eventName]?.bind;
|
|
113
|
+
const currentRegisteredHandler = isCatch
|
|
114
|
+
? commonCatchHandler
|
|
115
|
+
: commonHandler;
|
|
116
|
+
if (currentHandler) {
|
|
117
|
+
if (!newEventHandler) {
|
|
118
|
+
/**
|
|
119
|
+
* remove handler
|
|
120
|
+
*/
|
|
121
|
+
element.removeEventListener(eventName, currentRegisteredHandler, {
|
|
122
|
+
capture: isCapture,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
/**
|
|
128
|
+
* append new handler
|
|
129
|
+
*/
|
|
130
|
+
if (newEventHandler) {
|
|
131
|
+
const htmlEventName = LynxEventNameToW3cByTagName[element.tagName]?.[eventName]
|
|
132
|
+
?? LynxEventNameToW3cCommon[eventName] ?? eventName;
|
|
133
|
+
element.addEventListener(htmlEventName, currentRegisteredHandler, {
|
|
134
|
+
capture: isCapture,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (newEventHandler) {
|
|
139
|
+
const info = {
|
|
140
|
+
type: eventType,
|
|
141
|
+
handler: newEventHandler,
|
|
142
|
+
};
|
|
143
|
+
if (!runtimeInfo.eventHandlerMap[eventName]) {
|
|
144
|
+
runtimeInfo.eventHandlerMap[eventName] = {
|
|
145
|
+
capture: undefined,
|
|
146
|
+
bind: undefined,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
if (isCapture) {
|
|
150
|
+
runtimeInfo.eventHandlerMap[eventName].capture = info;
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
runtimeInfo.eventHandlerMap[eventName].bind = info;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
elementToRuntimeInfoMap.set(element, runtimeInfo);
|
|
157
|
+
};
|
|
158
|
+
const __GetEvent = (element, eventName, eventType) => {
|
|
159
|
+
const runtimeInfo = elementToRuntimeInfoMap.get(element);
|
|
160
|
+
if (runtimeInfo) {
|
|
161
|
+
eventName = eventName.toLowerCase();
|
|
162
|
+
const isCapture = eventType.startsWith('capture');
|
|
163
|
+
const handler = isCapture
|
|
164
|
+
? runtimeInfo.eventHandlerMap[eventName]?.capture
|
|
165
|
+
: runtimeInfo.eventHandlerMap[eventName]?.bind;
|
|
166
|
+
return handler?.handler;
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
return undefined;
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
const __GetEvents = (element) => {
|
|
173
|
+
const eventHandlerMap = elementToRuntimeInfoMap.get(element)?.eventHandlerMap ?? {};
|
|
174
|
+
const eventInfos = [];
|
|
175
|
+
for (const [lynxEventName, info] of Object.entries(eventHandlerMap)) {
|
|
176
|
+
for (const atomInfo of [info.bind, info.capture]) {
|
|
177
|
+
if (atomInfo) {
|
|
178
|
+
const { type, handler } = atomInfo;
|
|
179
|
+
if (handler) {
|
|
180
|
+
eventInfos.push({
|
|
181
|
+
type: type,
|
|
182
|
+
name: lynxEventName,
|
|
183
|
+
function: handler,
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return eventInfos;
|
|
190
|
+
};
|
|
191
|
+
const __SetEvents = (element, listeners) => {
|
|
192
|
+
for (const { type: eventType, name: lynxEventName, function: eventHandler } of listeners) {
|
|
193
|
+
__AddEvent(element, eventType, lynxEventName, eventHandler);
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
const __CreateElement = (tag, parentComponentUniqueId) => {
|
|
197
|
+
const uniqueId = uniqueIdInc++;
|
|
198
|
+
const htmlTag = tagMap[tag] ?? tag;
|
|
199
|
+
const element = callbacks.createElement(htmlTag);
|
|
200
|
+
lynxUniqueIdToElement[uniqueId] = new WeakRef(element);
|
|
201
|
+
const parentComponentCssID = lynxUniqueIdToElement[parentComponentUniqueId]
|
|
202
|
+
?.deref()?.getAttribute(cssIdAttribute);
|
|
203
|
+
parentComponentCssID && parentComponentCssID !== '0'
|
|
204
|
+
&& element.setAttribute(cssIdAttribute, parentComponentCssID);
|
|
205
|
+
element.setAttribute(lynxTagAttribute, tag);
|
|
206
|
+
element.setAttribute(lynxUniqueIdAttribute, uniqueId + '');
|
|
207
|
+
element.setAttribute(parentComponentUniqueIdAttribute, parentComponentUniqueId + '');
|
|
208
|
+
return element;
|
|
209
|
+
};
|
|
210
|
+
const __CreateView = (parentComponentUniqueId) => __CreateElement('view', parentComponentUniqueId);
|
|
211
|
+
const __CreateText = (parentComponentUniqueId) => __CreateElement('text', parentComponentUniqueId);
|
|
212
|
+
const __CreateRawText = (text) => {
|
|
213
|
+
const element = __CreateElement('raw-text', -1);
|
|
214
|
+
element.setAttribute('text', text);
|
|
215
|
+
return element;
|
|
216
|
+
};
|
|
217
|
+
const __CreateImage = (parentComponentUniqueId) => __CreateElement('image', parentComponentUniqueId);
|
|
218
|
+
const __CreateScrollView = (parentComponentUniqueId) => __CreateElement('scroll-view', parentComponentUniqueId);
|
|
219
|
+
const __CreateWrapperElement = (parentComponentUniqueId) => __CreateElement('lynx-wrapper', parentComponentUniqueId);
|
|
220
|
+
const __CreatePage = (componentID, cssID) => {
|
|
221
|
+
const page = __CreateElement('page', 0);
|
|
222
|
+
page.setAttribute('part', 'page');
|
|
223
|
+
page.setAttribute(cssIdAttribute, cssID + '');
|
|
224
|
+
page.setAttribute(parentComponentUniqueIdAttribute, '0');
|
|
225
|
+
page.setAttribute(componentIdAttribute, componentID);
|
|
226
|
+
if (pageConfig.defaultDisplayLinear === false) {
|
|
227
|
+
page.setAttribute(lynxDefaultDisplayLinearAttribute, 'false');
|
|
228
|
+
}
|
|
229
|
+
if (pageConfig.defaultOverflowVisible === true) {
|
|
230
|
+
page.setAttribute('lynx-default-overflow-visible', 'true');
|
|
231
|
+
}
|
|
232
|
+
pageElement = page;
|
|
233
|
+
return page;
|
|
234
|
+
};
|
|
235
|
+
const __CreateList = (parentComponentUniqueId, componentAtIndex, enqueueComponent) => {
|
|
236
|
+
const list = __CreateElement('list', parentComponentUniqueId);
|
|
237
|
+
const runtimeInfo = {
|
|
238
|
+
eventHandlerMap: {},
|
|
239
|
+
componentAtIndex: componentAtIndex,
|
|
240
|
+
enqueueComponent: enqueueComponent,
|
|
241
|
+
};
|
|
242
|
+
elementToRuntimeInfoMap.set(list, runtimeInfo);
|
|
243
|
+
return list;
|
|
244
|
+
};
|
|
245
|
+
const __CreateComponent = (componentParentUniqueID, componentID, cssID, _, name) => {
|
|
246
|
+
const component = __CreateElement('view', componentParentUniqueID);
|
|
247
|
+
component.setAttribute(cssIdAttribute, cssID + '');
|
|
248
|
+
component.setAttribute(componentIdAttribute, componentID);
|
|
249
|
+
component.setAttribute('name', name);
|
|
250
|
+
return component;
|
|
251
|
+
};
|
|
252
|
+
const __SetAttribute = (element, key, value) => {
|
|
253
|
+
const tag = element.getAttribute(lynxTagAttribute);
|
|
254
|
+
if (tag === 'list' && key === 'update-list-info') {
|
|
255
|
+
const listInfo = value;
|
|
256
|
+
const { insertAction, removeAction } = listInfo;
|
|
257
|
+
queueMicrotask(() => {
|
|
258
|
+
const runtimeInfo = elementToRuntimeInfoMap.get(element);
|
|
259
|
+
if (runtimeInfo) {
|
|
260
|
+
const componentAtIndex = runtimeInfo.componentAtIndex;
|
|
261
|
+
const enqueueComponent = runtimeInfo.enqueueComponent;
|
|
262
|
+
const uniqueId = __GetElementUniqueID(element);
|
|
263
|
+
for (const action of insertAction) {
|
|
264
|
+
componentAtIndex?.(element, uniqueId, action.position, 0, false);
|
|
265
|
+
}
|
|
266
|
+
for (const action of removeAction) {
|
|
267
|
+
enqueueComponent?.(element, uniqueId, action.position);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
else {
|
|
273
|
+
value == null
|
|
274
|
+
? element.removeAttribute(key)
|
|
275
|
+
: element.setAttribute(key, value + '');
|
|
276
|
+
if (key === __lynx_timing_flag && value) {
|
|
277
|
+
timingFlags.push(value);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
const __UpdateListCallbacks = (element, componentAtIndex, enqueueComponent) => {
|
|
282
|
+
const runtimeInfo = elementToRuntimeInfoMap.get(element) ?? {
|
|
283
|
+
eventHandlerMap: {},
|
|
284
|
+
componentAtIndex: componentAtIndex,
|
|
285
|
+
enqueueComponent: enqueueComponent,
|
|
286
|
+
uniqueId: __GetElementUniqueID(element),
|
|
287
|
+
};
|
|
288
|
+
runtimeInfo.componentAtIndex = componentAtIndex;
|
|
289
|
+
runtimeInfo.enqueueComponent = enqueueComponent;
|
|
290
|
+
elementToRuntimeInfoMap.set(element, runtimeInfo);
|
|
291
|
+
};
|
|
292
|
+
const __SwapElement = (childA, childB) => {
|
|
293
|
+
const temp = callbacks.createElement('div');
|
|
294
|
+
childA.replaceWith(temp);
|
|
295
|
+
childB.replaceWith(childA);
|
|
296
|
+
temp.replaceWith(childB);
|
|
297
|
+
};
|
|
298
|
+
const __SetCSSIdForCSSOG = (elements, cssId) => {
|
|
299
|
+
for (const element of elements) {
|
|
300
|
+
element.setAttribute(cssIdAttribute, cssId + '');
|
|
301
|
+
const cls = element.getAttribute('class');
|
|
302
|
+
cls && __SetClassesForCSSOG(element, cls);
|
|
303
|
+
}
|
|
304
|
+
};
|
|
305
|
+
const __AddClassForCSSOG = (element, className) => {
|
|
306
|
+
const newClassName = ((element.getAttribute('class') ?? '') + ' ' + className)
|
|
307
|
+
.trim();
|
|
308
|
+
element.setAttribute('class', newClassName);
|
|
309
|
+
const newStyleStr = decodeCssInJs(newClassName, cssInJsInfo, element.getAttribute(cssIdAttribute));
|
|
310
|
+
updateCSSInJsStyle(Number(element.getAttribute(lynxUniqueIdAttribute)), newStyleStr);
|
|
311
|
+
};
|
|
312
|
+
const __SetClassesForCSSOG = (element, classNames) => {
|
|
313
|
+
__SetClasses(element, classNames);
|
|
314
|
+
const newStyleStr = decodeCssInJs(classNames ?? '', cssInJsInfo, element.getAttribute(cssIdAttribute));
|
|
315
|
+
updateCSSInJsStyle(Number(element.getAttribute(lynxUniqueIdAttribute)), newStyleStr ?? '');
|
|
316
|
+
};
|
|
317
|
+
const __LoadLepusChunk = (path) => {
|
|
318
|
+
const lepusModule = lepusCode[`${path}`];
|
|
319
|
+
if (lepusModule) {
|
|
320
|
+
const entry = lepusModule.exports;
|
|
321
|
+
entry?.(mtsGlobalThis);
|
|
322
|
+
return true;
|
|
323
|
+
}
|
|
324
|
+
else {
|
|
325
|
+
return false;
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
const __FlushElementTree = (_subTree, options) => {
|
|
329
|
+
const timingFlagsCopied = timingFlags;
|
|
330
|
+
timingFlags = [];
|
|
331
|
+
if (pageElement && !pageElement.parentNode) {
|
|
332
|
+
// @ts-expect-error
|
|
333
|
+
rootDom.append(pageElement);
|
|
334
|
+
}
|
|
335
|
+
callbacks.flushElementTree(options, timingFlagsCopied);
|
|
336
|
+
};
|
|
337
|
+
const __GetTemplateParts = () => {
|
|
338
|
+
return undefined;
|
|
339
|
+
};
|
|
340
|
+
const __GetPageElement = () => {
|
|
341
|
+
return pageElement;
|
|
342
|
+
};
|
|
343
|
+
const isCSSOG = !pageConfig.enableCSSSelector;
|
|
344
|
+
const mtsGlobalThis = {
|
|
345
|
+
__AddEvent,
|
|
346
|
+
__GetEvent,
|
|
347
|
+
__GetEvents,
|
|
348
|
+
__SetEvents,
|
|
349
|
+
__AppendElement,
|
|
350
|
+
__ElementIsEqual,
|
|
351
|
+
__FirstElement,
|
|
352
|
+
__GetChildren,
|
|
353
|
+
__GetParent,
|
|
354
|
+
__InsertElementBefore,
|
|
355
|
+
__LastElement,
|
|
356
|
+
__NextElement,
|
|
357
|
+
__RemoveElement,
|
|
358
|
+
__ReplaceElement,
|
|
359
|
+
__ReplaceElements,
|
|
360
|
+
__AddConfig,
|
|
361
|
+
__AddDataset,
|
|
362
|
+
__GetAttributes,
|
|
363
|
+
__GetComponentID,
|
|
364
|
+
__GetDataByKey,
|
|
365
|
+
__GetDataset,
|
|
366
|
+
__GetElementConfig,
|
|
367
|
+
__GetElementUniqueID,
|
|
368
|
+
__GetID,
|
|
369
|
+
__GetTag,
|
|
370
|
+
__SetConfig,
|
|
371
|
+
__SetDataset,
|
|
372
|
+
__SetID,
|
|
373
|
+
__UpdateComponentID,
|
|
374
|
+
__CreateElement,
|
|
375
|
+
__CreateView,
|
|
376
|
+
__CreateText,
|
|
377
|
+
__CreateComponent,
|
|
378
|
+
__CreatePage,
|
|
379
|
+
__CreateRawText,
|
|
380
|
+
__CreateImage,
|
|
381
|
+
__CreateScrollView,
|
|
382
|
+
__CreateWrapperElement,
|
|
383
|
+
__CreateList,
|
|
384
|
+
__SetAttribute,
|
|
385
|
+
__SwapElement,
|
|
386
|
+
__UpdateListCallbacks,
|
|
387
|
+
__GetConfig: __GetElementConfig,
|
|
388
|
+
__GetClasses,
|
|
389
|
+
__AddClass: isCSSOG ? __AddClassForCSSOG : __AddClass,
|
|
390
|
+
__SetClasses: isCSSOG ? __SetClassesForCSSOG : __SetClasses,
|
|
391
|
+
__AddInlineStyle,
|
|
392
|
+
__SetCSSId: isCSSOG ? __SetCSSIdForCSSOG : __SetCSSId,
|
|
393
|
+
__SetInlineStyles,
|
|
394
|
+
__LoadLepusChunk,
|
|
395
|
+
__GetPageElement,
|
|
396
|
+
__GetTemplateParts,
|
|
397
|
+
__globalProps: globalProps,
|
|
398
|
+
SystemInfo: {
|
|
399
|
+
...systemInfo,
|
|
400
|
+
...config.browserConfig,
|
|
401
|
+
},
|
|
402
|
+
lynx: createMainThreadLynx(config),
|
|
403
|
+
_ReportError: callbacks._ReportError,
|
|
404
|
+
__OnLifecycleEvent: callbacks.__OnLifecycleEvent,
|
|
405
|
+
__FlushElementTree,
|
|
406
|
+
__lynxGlobalBindingValues: lynxGlobalBindingValues,
|
|
407
|
+
set _updateVars(handler) {
|
|
408
|
+
varsUpdateHandlers.push(handler);
|
|
409
|
+
},
|
|
410
|
+
set renderPage(foo) {
|
|
411
|
+
renderPage = foo;
|
|
412
|
+
queueMicrotask(callbacks.mainChunkReady);
|
|
413
|
+
},
|
|
414
|
+
get renderPage() {
|
|
415
|
+
return renderPage;
|
|
416
|
+
},
|
|
417
|
+
};
|
|
418
|
+
mtsGlobalThis.globalThis = new Proxy(mtsGlobalThis, {
|
|
419
|
+
get: (target, prop) => {
|
|
420
|
+
if (prop === 'globalThis') {
|
|
421
|
+
return target;
|
|
422
|
+
}
|
|
423
|
+
// @ts-expect-error
|
|
424
|
+
return target[prop] ?? globalThis[prop];
|
|
425
|
+
},
|
|
426
|
+
set: (target, prop, value) => {
|
|
427
|
+
// @ts-expect-error
|
|
428
|
+
target[prop] = value;
|
|
429
|
+
return true;
|
|
430
|
+
},
|
|
431
|
+
ownKeys(target) {
|
|
432
|
+
return Reflect.ownKeys(target).filter((key) => key !== 'globalThis');
|
|
433
|
+
},
|
|
434
|
+
});
|
|
435
|
+
for (const nm of globalMuteableVars) {
|
|
436
|
+
Object.defineProperty(mtsGlobalThis, nm, {
|
|
437
|
+
get: () => {
|
|
438
|
+
return lynxGlobalBindingValues[nm];
|
|
439
|
+
},
|
|
440
|
+
set: (v) => {
|
|
441
|
+
lynxGlobalBindingValues[nm] = v;
|
|
442
|
+
for (const handler of varsUpdateHandlers) {
|
|
443
|
+
handler();
|
|
444
|
+
}
|
|
445
|
+
},
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
return mtsGlobalThis;
|
|
449
|
+
}
|
|
450
|
+
//# sourceMappingURL=createMainThreadGlobalThis.js.map
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
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 {} from './MainThreadRuntime.js';
|
|
1
|
+
import {} from './createMainThreadGlobalThis.js';
|
|
5
2
|
export function createMainThreadLynx(config) {
|
|
6
3
|
return {
|
|
7
4
|
getJSContext() {
|
|
@@ -20,4 +17,4 @@ export function createMainThreadLynx(config) {
|
|
|
20
17
|
markPipelineTiming: config.callbacks.markTiming,
|
|
21
18
|
};
|
|
22
19
|
}
|
|
23
|
-
//# sourceMappingURL=
|
|
20
|
+
//# sourceMappingURL=createMainThreadLynx.js.map
|
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
import { type Rpc } from '@lynx-js/web-constants';
|
|
2
|
-
|
|
3
|
-
export declare function registerCallLepusMethodHandler(rpc: Rpc, runtime: MainThreadRuntime): void;
|
|
1
|
+
import { type MainThreadGlobalThis, type Rpc } from '@lynx-js/web-constants';
|
|
2
|
+
export declare function registerCallLepusMethodHandler(rpc: Rpc, runtime: MainThreadGlobalThis): void;
|
|
@@ -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 { callLepusMethodEndpoint } from '@lynx-js/web-constants';
|
|
4
|
+
import { callLepusMethodEndpoint, } from '@lynx-js/web-constants';
|
|
5
5
|
export function registerCallLepusMethodHandler(rpc, runtime) {
|
|
6
6
|
rpc.registerHandler(callLepusMethodEndpoint, (methodName, data) => {
|
|
7
7
|
(runtime[methodName])(data);
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { prepareMainThreadAPIs } from './prepareMainThreadAPIs.js';
|
|
2
|
-
export * from './
|
|
2
|
+
export * from './createMainThreadGlobalThis.js';
|
package/dist/index.js
CHANGED
|
@@ -2,5 +2,5 @@
|
|
|
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
|
export { prepareMainThreadAPIs } from './prepareMainThreadAPIs.js';
|
|
5
|
-
export * from './
|
|
5
|
+
export * from './createMainThreadGlobalThis.js';
|
|
6
6
|
//# sourceMappingURL=index.js.map
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { type Rpc, type StartMainThreadContextConfig, type RpcCallType, type reportErrorEndpoint } from '@lynx-js/web-constants';
|
|
2
|
-
import { MainThreadRuntime } from './MainThreadRuntime.js';
|
|
1
|
+
import { type Rpc, type StartMainThreadContextConfig, type RpcCallType, type reportErrorEndpoint, type MainThreadGlobalThis } from '@lynx-js/web-constants';
|
|
3
2
|
export declare function prepareMainThreadAPIs(backgroundThreadRpc: Rpc, rootDom: Document | ShadowRoot, createElement: Document['createElement'], commitDocument: () => Promise<void> | void, markTimingInternal: (timingKey: string, pipelineId?: string) => void, reportError: RpcCallType<typeof reportErrorEndpoint>): {
|
|
4
|
-
startMainThread: (config: StartMainThreadContextConfig) => Promise<
|
|
3
|
+
startMainThread: (config: StartMainThreadContextConfig) => Promise<MainThreadGlobalThis>;
|
|
5
4
|
};
|
|
@@ -1,10 +1,11 @@
|
|
|
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, publishEventEndpoint, publicComponentEventEndpoint, postExposureEndpoint,
|
|
4
|
+
import { BackgroundThreadStartEndpoint, publishEventEndpoint, publicComponentEventEndpoint, postExposureEndpoint, postTimingFlagsEndpoint, dispatchCoreContextOnBackgroundEndpoint, dispatchJSContextOnMainThreadEndpoint, LynxCrossThreadContext, switchExposureServiceEndpoint, } from '@lynx-js/web-constants';
|
|
5
5
|
import { registerCallLepusMethodHandler } from './crossThreadHandlers/registerCallLepusMethodHandler.js';
|
|
6
6
|
import { registerGetCustomSectionHandler } from './crossThreadHandlers/registerGetCustomSectionHandler.js';
|
|
7
|
-
import {
|
|
7
|
+
import { createMainThreadGlobalThis } from './createMainThreadGlobalThis.js';
|
|
8
|
+
import { createExposureService } from './utils/createExposureService.js';
|
|
8
9
|
const moduleCache = {};
|
|
9
10
|
export function prepareMainThreadAPIs(backgroundThreadRpc, rootDom, createElement, commitDocument, markTimingInternal, reportError) {
|
|
10
11
|
const postTimingFlags = backgroundThreadRpc.createCall(postTimingFlagsEndpoint);
|
|
@@ -39,7 +40,7 @@ export function prepareMainThreadAPIs(backgroundThreadRpc, rootDom, createElemen
|
|
|
39
40
|
receiveEventEndpoint: dispatchJSContextOnMainThreadEndpoint,
|
|
40
41
|
sendEventEndpoint: dispatchCoreContextOnBackgroundEndpoint,
|
|
41
42
|
});
|
|
42
|
-
const
|
|
43
|
+
const mtsGlobalThis = createMainThreadGlobalThis({
|
|
43
44
|
jsContext,
|
|
44
45
|
tagMap,
|
|
45
46
|
browserConfig,
|
|
@@ -48,18 +49,20 @@ export function prepareMainThreadAPIs(backgroundThreadRpc, rootDom, createElemen
|
|
|
48
49
|
pageConfig,
|
|
49
50
|
styleInfo,
|
|
50
51
|
lepusCode: lepusCodeLoaded,
|
|
51
|
-
createElement,
|
|
52
52
|
rootDom,
|
|
53
53
|
callbacks: {
|
|
54
54
|
mainChunkReady: () => {
|
|
55
55
|
markTimingInternal('data_processor_start');
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
56
|
+
let initData = config.initData;
|
|
57
|
+
if (pageConfig.enableJSDataProcessor !== true
|
|
58
|
+
&& mtsGlobalThis.processData) {
|
|
59
|
+
initData = mtsGlobalThis.processData(config.initData);
|
|
60
|
+
}
|
|
59
61
|
markTimingInternal('data_processor_end');
|
|
60
|
-
registerCallLepusMethodHandler(backgroundThreadRpc,
|
|
62
|
+
registerCallLepusMethodHandler(backgroundThreadRpc, mtsGlobalThis);
|
|
61
63
|
registerGetCustomSectionHandler(backgroundThreadRpc, customSections);
|
|
62
|
-
|
|
64
|
+
const { switchExposureService } = createExposureService(rootDom, postExposure);
|
|
65
|
+
backgroundThreadRpc.registerHandler(switchExposureServiceEndpoint, switchExposureService);
|
|
63
66
|
backgroundStart({
|
|
64
67
|
initData,
|
|
65
68
|
globalProps,
|
|
@@ -70,8 +73,8 @@ export function prepareMainThreadAPIs(backgroundThreadRpc, rootDom, createElemen
|
|
|
70
73
|
napiModulesMap,
|
|
71
74
|
browserConfig,
|
|
72
75
|
});
|
|
73
|
-
|
|
74
|
-
|
|
76
|
+
mtsGlobalThis.renderPage(initData);
|
|
77
|
+
mtsGlobalThis.__FlushElementTree(undefined, {});
|
|
75
78
|
},
|
|
76
79
|
flushElementTree: async (options, timingFlags) => {
|
|
77
80
|
const pipelineId = options?.pipelineOptions?.pipelineID;
|
|
@@ -108,13 +111,13 @@ export function prepareMainThreadAPIs(backgroundThreadRpc, rootDom, createElemen
|
|
|
108
111
|
markTiming: (a, b) => markTimingInternal(b, a),
|
|
109
112
|
publishEvent,
|
|
110
113
|
publicComponentEvent,
|
|
111
|
-
|
|
114
|
+
createElement,
|
|
112
115
|
},
|
|
113
116
|
});
|
|
114
117
|
markTimingInternal('decode_end');
|
|
115
|
-
entry(
|
|
118
|
+
entry(mtsGlobalThis);
|
|
116
119
|
jsContext.__start(); // start the jsContext after the runtime is created
|
|
117
|
-
return
|
|
120
|
+
return mtsGlobalThis;
|
|
118
121
|
}
|
|
119
122
|
return { startMainThread };
|
|
120
123
|
}
|