airx 0.2.1 → 0.2.3
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/output/esm/index.js +1212 -0
- package/output/esm/index.js.map +1 -0
- package/output/umd/element.d.ts +46 -0
- package/output/umd/index.d.ts +13 -0
- package/output/umd/index.js +1229 -0
- package/output/umd/index.js.map +1 -0
- package/output/umd/logger.d.ts +3 -0
- package/output/umd/reactive.d.ts +10 -0
- package/output/umd/render/browser.d.ts +7 -0
- package/output/umd/render/common/hooks.d.ts +10 -0
- package/output/umd/render/common/index.d.ts +62 -0
- package/output/umd/render/common/plugins/context.d.ts +5 -0
- package/output/umd/render/common/plugins/index.d.ts +19 -0
- package/output/umd/render/common/plugins/internal/basic.d.ts +10 -0
- package/output/umd/render/common/plugins/internal/inject.d.ts +6 -0
- package/output/{render/index.js → umd/render/index.d.ts} +1 -2
- package/output/umd/render/server.d.ts +5 -0
- package/output/umd/symbol.d.ts +2 -0
- package/output/umd/types.d.ts +1172 -0
- package/package.json +16 -15
- package/output/element.js +0 -27
- package/output/element.js.map +0 -1
- package/output/index.js +0 -27
- package/output/index.js.map +0 -1
- package/output/logger.js +0 -13
- package/output/logger.js.map +0 -1
- package/output/reactive.js +0 -64
- package/output/reactive.js.map +0 -1
- package/output/render/browser.js +0 -194
- package/output/render/browser.js.map +0 -1
- package/output/render/common/hooks.js +0 -22
- package/output/render/common/hooks.js.map +0 -1
- package/output/render/common/index.js +0 -372
- package/output/render/common/index.js.map +0 -1
- package/output/render/common/plugins/context.js +0 -12
- package/output/render/common/plugins/context.js.map +0 -1
- package/output/render/common/plugins/index.js +0 -2
- package/output/render/common/plugins/index.js.map +0 -1
- package/output/render/common/plugins/internal/basic.js +0 -158
- package/output/render/common/plugins/internal/basic.js.map +0 -1
- package/output/render/common/plugins/internal/inject.js +0 -25
- package/output/render/common/plugins/internal/inject.js.map +0 -1
- package/output/render/index.js.map +0 -1
- package/output/render/server.js +0 -314
- package/output/render/server.js.map +0 -1
- package/output/symbol.js +0 -3
- package/output/symbol.js.map +0 -1
- package/output/types.js +0 -2
- package/output/types.js.map +0 -1
- /package/output/{element.d.ts → esm/element.d.ts} +0 -0
- /package/output/{index.d.ts → esm/index.d.ts} +0 -0
- /package/output/{logger.d.ts → esm/logger.d.ts} +0 -0
- /package/output/{reactive.d.ts → esm/reactive.d.ts} +0 -0
- /package/output/{render → esm/render}/browser.d.ts +0 -0
- /package/output/{render → esm/render}/common/hooks.d.ts +0 -0
- /package/output/{render → esm/render}/common/index.d.ts +0 -0
- /package/output/{render → esm/render}/common/plugins/context.d.ts +0 -0
- /package/output/{render → esm/render}/common/plugins/index.d.ts +0 -0
- /package/output/{render → esm/render}/common/plugins/internal/basic.d.ts +0 -0
- /package/output/{render → esm/render}/common/plugins/internal/inject.d.ts +0 -0
- /package/output/{render → esm/render}/index.d.ts +0 -0
- /package/output/{render → esm/render}/server.d.ts +0 -0
- /package/output/{symbol.d.ts → esm/symbol.d.ts} +0 -0
- /package/output/{types.d.ts → esm/types.d.ts} +0 -0
|
@@ -0,0 +1,1212 @@
|
|
|
1
|
+
const globalContext$1 = {
|
|
2
|
+
current: null
|
|
3
|
+
};
|
|
4
|
+
function useContext() {
|
|
5
|
+
if (globalContext$1.current == null) {
|
|
6
|
+
throw new Error('Unable to find a valid component context');
|
|
7
|
+
}
|
|
8
|
+
return globalContext$1.current;
|
|
9
|
+
}
|
|
10
|
+
const onMounted = (listener) => {
|
|
11
|
+
return useContext().onMounted(listener);
|
|
12
|
+
};
|
|
13
|
+
const onUnmounted = (listener) => {
|
|
14
|
+
return useContext().onUnmounted(listener);
|
|
15
|
+
};
|
|
16
|
+
const inject = (key) => {
|
|
17
|
+
return useContext().inject(key);
|
|
18
|
+
};
|
|
19
|
+
const provide = (key, value) => {
|
|
20
|
+
return useContext().provide(key, value);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const isDev = typeof process != 'undefined' && process?.env?.NODE_ENV === 'development';
|
|
24
|
+
function createLogger(name) {
|
|
25
|
+
function getPrintPrefix() {
|
|
26
|
+
const date = new Date().toLocaleString();
|
|
27
|
+
return `[${date}][${name}]`;
|
|
28
|
+
}
|
|
29
|
+
function debug(...args) {
|
|
30
|
+
if (isDev)
|
|
31
|
+
console.log(getPrintPrefix(), ...args);
|
|
32
|
+
}
|
|
33
|
+
return { debug };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const airxElementSymbol = Symbol('airx-element');
|
|
37
|
+
const airxReactiveDependenciesSymbol = Symbol('airx-dependencies');
|
|
38
|
+
|
|
39
|
+
/** FIXME: 污染全局总是不好的 */
|
|
40
|
+
const globalContext = {
|
|
41
|
+
dependencies: new Set()
|
|
42
|
+
};
|
|
43
|
+
function createCollector() {
|
|
44
|
+
const newDependencies = new Set();
|
|
45
|
+
return {
|
|
46
|
+
clear: () => newDependencies.clear(),
|
|
47
|
+
complete: () => [...newDependencies.values()],
|
|
48
|
+
collect: (process) => {
|
|
49
|
+
const beforeDeps = globalContext.dependencies;
|
|
50
|
+
globalContext.dependencies = newDependencies;
|
|
51
|
+
const result = process();
|
|
52
|
+
globalContext.dependencies = beforeDeps;
|
|
53
|
+
return result;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function triggerRef(ref) {
|
|
58
|
+
requestAnimationFrame(() => {
|
|
59
|
+
const deps = Reflect.get(ref, airxReactiveDependenciesSymbol);
|
|
60
|
+
for (const dep of deps) {
|
|
61
|
+
dep();
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
function createRefObject(value) {
|
|
66
|
+
const object = Object.create({ value });
|
|
67
|
+
Reflect.defineProperty(object, airxReactiveDependenciesSymbol, {
|
|
68
|
+
configurable: false,
|
|
69
|
+
enumerable: false,
|
|
70
|
+
writable: true,
|
|
71
|
+
value: new Set()
|
|
72
|
+
});
|
|
73
|
+
return object;
|
|
74
|
+
}
|
|
75
|
+
function watchSignal(ref, listener) {
|
|
76
|
+
const deps = Reflect.get(ref, airxReactiveDependenciesSymbol);
|
|
77
|
+
deps.add(listener);
|
|
78
|
+
return () => { deps.delete(listener); };
|
|
79
|
+
}
|
|
80
|
+
function createSignal(obj) {
|
|
81
|
+
const ref = createRefObject(obj);
|
|
82
|
+
if (!globalContext.dependencies.has(ref)) {
|
|
83
|
+
globalContext.dependencies.add(ref);
|
|
84
|
+
}
|
|
85
|
+
let value = ref.value;
|
|
86
|
+
Reflect.defineProperty(ref, 'value', {
|
|
87
|
+
get() {
|
|
88
|
+
if (!globalContext.dependencies.has(ref)) {
|
|
89
|
+
globalContext.dependencies.add(ref);
|
|
90
|
+
}
|
|
91
|
+
return value;
|
|
92
|
+
},
|
|
93
|
+
set(newValue) {
|
|
94
|
+
value = newValue;
|
|
95
|
+
triggerRef(ref);
|
|
96
|
+
return value;
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
return ref;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* createElement 是用于创建 AirxElement 的工具函数
|
|
104
|
+
*/
|
|
105
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
106
|
+
function createElement(type, props, ...children) {
|
|
107
|
+
return {
|
|
108
|
+
type,
|
|
109
|
+
props: {
|
|
110
|
+
...props,
|
|
111
|
+
children
|
|
112
|
+
},
|
|
113
|
+
[airxElementSymbol]: true
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
function isValidElement(element) {
|
|
117
|
+
return typeof element === 'object'
|
|
118
|
+
&& element !== null
|
|
119
|
+
&& Reflect.get(element, airxElementSymbol);
|
|
120
|
+
}
|
|
121
|
+
function Fragment(props) {
|
|
122
|
+
return () => props.children;
|
|
123
|
+
}
|
|
124
|
+
function component(comp) {
|
|
125
|
+
return comp;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
class InnerAirxComponentContext {
|
|
129
|
+
instance;
|
|
130
|
+
disposers = new Set();
|
|
131
|
+
providedMap = new Map();
|
|
132
|
+
injectedMap = new Map();
|
|
133
|
+
mountListeners = new Set();
|
|
134
|
+
unmountedListeners = new Set();
|
|
135
|
+
triggerMounted() {
|
|
136
|
+
this.mountListeners.forEach(listener => {
|
|
137
|
+
let disposer = undefined;
|
|
138
|
+
try {
|
|
139
|
+
disposer = listener();
|
|
140
|
+
}
|
|
141
|
+
catch (err) {
|
|
142
|
+
console.error(err, listener);
|
|
143
|
+
}
|
|
144
|
+
if (typeof disposer === 'function') {
|
|
145
|
+
this.addDisposer(disposer);
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
// 生命周期只会调用一次
|
|
149
|
+
this.mountListeners.clear();
|
|
150
|
+
}
|
|
151
|
+
triggerUnmounted() {
|
|
152
|
+
// 递归的调用子 child 的 Unmounted
|
|
153
|
+
if (this.instance?.child != null) {
|
|
154
|
+
this.instance.child.context.triggerUnmounted();
|
|
155
|
+
}
|
|
156
|
+
// 处理自己
|
|
157
|
+
this.unmountedListeners.forEach(listener => {
|
|
158
|
+
try {
|
|
159
|
+
listener();
|
|
160
|
+
}
|
|
161
|
+
catch (err) {
|
|
162
|
+
console.error(err, listener);
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
// 处理兄弟节点
|
|
166
|
+
if (this.instance?.sibling != null) {
|
|
167
|
+
this.instance.sibling.context.triggerUnmounted();
|
|
168
|
+
}
|
|
169
|
+
this.dispose();
|
|
170
|
+
}
|
|
171
|
+
onMounted(listener) {
|
|
172
|
+
this.mountListeners.add(listener);
|
|
173
|
+
}
|
|
174
|
+
onUnmounted(listener) {
|
|
175
|
+
this.unmountedListeners.add(listener);
|
|
176
|
+
}
|
|
177
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
178
|
+
provide(key, value) {
|
|
179
|
+
this.providedMap.set(key, value);
|
|
180
|
+
return v => this.providedMap.set(key, v);
|
|
181
|
+
}
|
|
182
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
183
|
+
inject(key) {
|
|
184
|
+
const getProvideValueForParent = (instance, key) => {
|
|
185
|
+
if (instance && instance.context) {
|
|
186
|
+
const value = instance.context.providedMap.get(key);
|
|
187
|
+
if (value != undefined)
|
|
188
|
+
return value;
|
|
189
|
+
if (instance.parent)
|
|
190
|
+
return getProvideValueForParent(instance.parent, key);
|
|
191
|
+
}
|
|
192
|
+
return undefined;
|
|
193
|
+
};
|
|
194
|
+
const currentParentValue = getProvideValueForParent(this.instance.parent, key);
|
|
195
|
+
this.injectedMap.set(key, currentParentValue); // 更新本地值
|
|
196
|
+
return this.injectedMap.get(key);
|
|
197
|
+
}
|
|
198
|
+
addDisposer(...disposers) {
|
|
199
|
+
disposers.forEach(disposer => {
|
|
200
|
+
this.disposers.add(disposer);
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
dispose() {
|
|
204
|
+
this.disposers.forEach((disposer) => {
|
|
205
|
+
try {
|
|
206
|
+
disposer();
|
|
207
|
+
}
|
|
208
|
+
catch (err) {
|
|
209
|
+
// eslint-disable-next-line no-console
|
|
210
|
+
console.error(err, disposer);
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
this.disposers.clear();
|
|
214
|
+
this.injectedMap.clear();
|
|
215
|
+
this.providedMap.clear();
|
|
216
|
+
this.mountListeners.clear();
|
|
217
|
+
this.unmountedListeners.clear();
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* 当 context 传递给外部消费时,隐藏内部实现,仅暴露接口定义的内容
|
|
221
|
+
* @returns 和 AirxComponentContext 接口完全一致的对象
|
|
222
|
+
*/
|
|
223
|
+
getSafeContext() {
|
|
224
|
+
return {
|
|
225
|
+
inject: k => this.inject(k),
|
|
226
|
+
provide: (k, v) => this.provide(k, v),
|
|
227
|
+
onMounted: listener => this.onMounted(listener),
|
|
228
|
+
onUnmounted: listener => this.onUnmounted(listener)
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* 更新 children
|
|
234
|
+
* @param parentInstance 当前正在处理的组件的实例
|
|
235
|
+
* @param children 当前组件的子节点
|
|
236
|
+
*/
|
|
237
|
+
function reconcileChildren(appContext, parentInstance, childrenElementArray) {
|
|
238
|
+
const logger = createLogger('reconcileChildren');
|
|
239
|
+
logger.debug('reconcileChildren', parentInstance, childrenElementArray);
|
|
240
|
+
// parentInstance ←--------
|
|
241
|
+
// | ↑ ↑
|
|
242
|
+
// child parent parent
|
|
243
|
+
// ↓ | |
|
|
244
|
+
// instance -sibling→ instance -→ ....
|
|
245
|
+
/**
|
|
246
|
+
* 内部通过 index 生成 element 的 key
|
|
247
|
+
* 通过前缀来避免和用户手动设置的 key 发生冲突
|
|
248
|
+
* @param index
|
|
249
|
+
* @returns 生成的 key
|
|
250
|
+
*/
|
|
251
|
+
function getInnerElementIndexKey(index) {
|
|
252
|
+
return `airx-element-inner-key:${index}`;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* 将 children 实例链转成 Map 便于此处消费
|
|
256
|
+
* @param firstChild children 实例的第一个元素
|
|
257
|
+
* @returns 从 firstChild 开始之后的所有 sibling 实例组成的 Map
|
|
258
|
+
*/
|
|
259
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
260
|
+
function getChildrenInstanceMap(firstChild) {
|
|
261
|
+
// 不使用递归是因为递归容易爆栈
|
|
262
|
+
let nextIndex = 0;
|
|
263
|
+
let nextChild = firstChild;
|
|
264
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
265
|
+
const map = new Map();
|
|
266
|
+
while (nextChild) {
|
|
267
|
+
/* 如果有 key,则将 key 作为 map key,否则将 index 作为 key */
|
|
268
|
+
if (nextChild.element?.props.key != null) {
|
|
269
|
+
map.set(nextChild.element?.props.key, nextChild);
|
|
270
|
+
}
|
|
271
|
+
else {
|
|
272
|
+
map.set(getInnerElementIndexKey(nextIndex), nextChild);
|
|
273
|
+
}
|
|
274
|
+
nextChild = nextChild.sibling;
|
|
275
|
+
nextIndex += 1;
|
|
276
|
+
}
|
|
277
|
+
return map;
|
|
278
|
+
}
|
|
279
|
+
const newChildrenInstanceArray = [];
|
|
280
|
+
const childrenInstanceMap = getChildrenInstanceMap(parentInstance.child);
|
|
281
|
+
function getChildInstance(child, index) {
|
|
282
|
+
if (child.props.key != null) {
|
|
283
|
+
const key = child.props.key;
|
|
284
|
+
const instance = childrenInstanceMap.get(key) || null;
|
|
285
|
+
return [instance, () => childrenInstanceMap.delete(key)];
|
|
286
|
+
}
|
|
287
|
+
const innerKey = getInnerElementIndexKey(index);
|
|
288
|
+
const instance = childrenInstanceMap.get(innerKey) || null;
|
|
289
|
+
return [instance, () => childrenInstanceMap.delete(innerKey)];
|
|
290
|
+
}
|
|
291
|
+
/** 是否复用实例 */
|
|
292
|
+
function isReuseInstance(instance, nextElement) {
|
|
293
|
+
for (const plugin of appContext.plugins) {
|
|
294
|
+
if (typeof plugin.isReuseInstance === 'function') {
|
|
295
|
+
const result = plugin.isReuseInstance(instance, nextElement);
|
|
296
|
+
// 任意一个插件要求不复用就不复用
|
|
297
|
+
if (result === false)
|
|
298
|
+
return false;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
return true;
|
|
302
|
+
}
|
|
303
|
+
function updateMemoProps(instance) {
|
|
304
|
+
if (instance.element == null)
|
|
305
|
+
return;
|
|
306
|
+
if (instance.memoProps == null)
|
|
307
|
+
instance.memoProps = {};
|
|
308
|
+
// 简单来说就是以下几件事情
|
|
309
|
+
// 始终保持 props 的引用不变
|
|
310
|
+
// 1. 创建一个新对象来保存 beforeElement props 的状态
|
|
311
|
+
// 2. 将新的 element 上的 props 引用设置为之前的 props
|
|
312
|
+
// 3. 将新的 element 上的 props 更新到之前的 props 上去
|
|
313
|
+
if (instance.memoProps != null) {
|
|
314
|
+
for (const key in instance.memoProps) {
|
|
315
|
+
delete instance.memoProps[key];
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
// 将新的 props 更新上去
|
|
319
|
+
for (const key in instance.element.props) {
|
|
320
|
+
const value = instance.element.props[key];
|
|
321
|
+
instance.memoProps[key] = value;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
function isNeedReRender(instance) {
|
|
325
|
+
for (const plugin of appContext.plugins) {
|
|
326
|
+
if (typeof plugin.isReRender === 'function') {
|
|
327
|
+
const result = plugin.isReRender(instance);
|
|
328
|
+
// 任意一个插件要求重新渲染就重新渲染
|
|
329
|
+
if (result === true)
|
|
330
|
+
return true;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
return false;
|
|
334
|
+
}
|
|
335
|
+
function getElementNamespace(element) {
|
|
336
|
+
const ns = Object.keys(element.props)
|
|
337
|
+
.filter(key => (key === 'xmlns' || key.startsWith('xmlns:')))
|
|
338
|
+
.sort((a, b) => a.length - b.length);
|
|
339
|
+
if (ns.length > 1) {
|
|
340
|
+
console.log('airx currently does not support setting multiple xmlns');
|
|
341
|
+
return '';
|
|
342
|
+
}
|
|
343
|
+
if (ns.some(i => i.startsWith('xmlns:'))) {
|
|
344
|
+
console.log('airx does not currently support setting named namespaces,only supports default');
|
|
345
|
+
return '';
|
|
346
|
+
}
|
|
347
|
+
// 原则上来说 html 仅支持设置默认的 namespace
|
|
348
|
+
return element.props[ns[0]];
|
|
349
|
+
}
|
|
350
|
+
// 依次遍历 child 并和 instance 对比
|
|
351
|
+
for (let index = 0; index < childrenElementArray.length; index++) {
|
|
352
|
+
const element = childrenElementArray[index];
|
|
353
|
+
const [instance, seize] = getChildInstance(element, index);
|
|
354
|
+
if (instance && isReuseInstance(instance, element)) {
|
|
355
|
+
seize(); // 从 childrenInstanceMap 中释放
|
|
356
|
+
newChildrenInstanceArray.push(instance);
|
|
357
|
+
instance.beforeElement = instance.element;
|
|
358
|
+
instance.element = element;
|
|
359
|
+
updateMemoProps(instance);
|
|
360
|
+
// 如果父组件更新了,子组件全部都要更新
|
|
361
|
+
if (instance.needReRender !== true && typeof element.type === 'function') {
|
|
362
|
+
instance.needReRender = parentInstance.needReRender || isNeedReRender(instance);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
else {
|
|
366
|
+
const context = new InnerAirxComponentContext();
|
|
367
|
+
const elementNamespace = getElementNamespace(element);
|
|
368
|
+
const instance = { element, context, elementNamespace };
|
|
369
|
+
newChildrenInstanceArray.push(instance);
|
|
370
|
+
context.instance = instance;
|
|
371
|
+
updateMemoProps(instance);
|
|
372
|
+
// 添加 ref 处理
|
|
373
|
+
if ('ref' in instance.memoProps) {
|
|
374
|
+
context.onMounted(() => {
|
|
375
|
+
if (instance.domRef) { // 如果组件有自己的 dom
|
|
376
|
+
instance.memoProps.ref.value = instance.domRef;
|
|
377
|
+
return () => instance.memoProps.ref.value = null;
|
|
378
|
+
}
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
// 新的 node
|
|
384
|
+
for (let index = 0; index < newChildrenInstanceArray.length; index++) {
|
|
385
|
+
const instance = newChildrenInstanceArray[index];
|
|
386
|
+
// 确保链的干净
|
|
387
|
+
instance.parent = parentInstance;
|
|
388
|
+
if (index === 0)
|
|
389
|
+
parentInstance.child = instance;
|
|
390
|
+
if (index > 0)
|
|
391
|
+
newChildrenInstanceArray[index - 1].sibling = instance;
|
|
392
|
+
if (index === newChildrenInstanceArray.length - 1)
|
|
393
|
+
delete instance.sibling;
|
|
394
|
+
// 处理 ElementNS: 继承父级 namespace
|
|
395
|
+
if (!instance.elementNamespace && instance.parent?.elementNamespace) {
|
|
396
|
+
// SVG 中 foreignObject 代表外来对象,起到隔离 namespace 的作用
|
|
397
|
+
if (instance.parent.element?.type !== 'foreignObject') {
|
|
398
|
+
instance.elementNamespace = instance.parent.elementNamespace;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
// 剩余的是需要移除的
|
|
403
|
+
if (childrenInstanceMap.size > 0) {
|
|
404
|
+
if (parentInstance.deletions == null) {
|
|
405
|
+
parentInstance.deletions = new Set();
|
|
406
|
+
}
|
|
407
|
+
childrenInstanceMap.forEach(instance => {
|
|
408
|
+
parentInstance.deletions?.add(instance);
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
logger.debug('parentInstance', parentInstance);
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* 处理单个 instance
|
|
415
|
+
* @param instance 当前处理的实例
|
|
416
|
+
* @returns 返回下一个需要处理的 instance
|
|
417
|
+
*/
|
|
418
|
+
function performUnitOfWork(pluginContext, instance, onUpdateRequire) {
|
|
419
|
+
const element = instance.element;
|
|
420
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
421
|
+
function childrenAsElements(children) {
|
|
422
|
+
const childrenAsArray = Array.isArray(children) ? children : [children];
|
|
423
|
+
function isComment(element) {
|
|
424
|
+
if (element === '')
|
|
425
|
+
return true;
|
|
426
|
+
if (element == null)
|
|
427
|
+
return true;
|
|
428
|
+
if (element === false)
|
|
429
|
+
return true;
|
|
430
|
+
return false;
|
|
431
|
+
}
|
|
432
|
+
return childrenAsArray.flat(3).map(element => {
|
|
433
|
+
if (isValidElement(element))
|
|
434
|
+
return element;
|
|
435
|
+
const elementType = isComment(element) ? 'comment' : 'text';
|
|
436
|
+
const textContent = element === '' ? 'empty-string' : String(element);
|
|
437
|
+
return createElement(elementType, { textContent });
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
// airx 组件
|
|
441
|
+
if (typeof element?.type === 'function') {
|
|
442
|
+
const collector = createCollector();
|
|
443
|
+
if (instance.render == null) {
|
|
444
|
+
const component = element.type;
|
|
445
|
+
const beforeContext = globalContext$1.current;
|
|
446
|
+
globalContext$1.current = instance.context.getSafeContext();
|
|
447
|
+
const componentReturnValue = collector.collect(() => component(instance.memoProps));
|
|
448
|
+
if (typeof componentReturnValue !== 'function') {
|
|
449
|
+
throw new Error('Component must return a render function');
|
|
450
|
+
}
|
|
451
|
+
globalContext$1.current = beforeContext;
|
|
452
|
+
instance.render = componentReturnValue;
|
|
453
|
+
const children = collector.collect(() => instance.render?.());
|
|
454
|
+
reconcileChildren(pluginContext, instance, childrenAsElements(children));
|
|
455
|
+
}
|
|
456
|
+
if (instance.needReRender) {
|
|
457
|
+
const children = collector.collect(() => instance.render?.());
|
|
458
|
+
reconcileChildren(pluginContext, instance, childrenAsElements(children));
|
|
459
|
+
delete instance.needReRender;
|
|
460
|
+
}
|
|
461
|
+
// 处理依赖触发的更新
|
|
462
|
+
collector.complete().forEach(ref => {
|
|
463
|
+
instance.context.addDisposer(watchSignal(ref, () => {
|
|
464
|
+
instance.needReRender = true;
|
|
465
|
+
onUpdateRequire?.(instance);
|
|
466
|
+
}));
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
// 浏览器组件/标签
|
|
470
|
+
if (typeof element?.type === 'string') {
|
|
471
|
+
if ('children' in element.props && Array.isArray(element.props.children)) {
|
|
472
|
+
reconcileChildren(pluginContext, instance, childrenAsElements(element.props.children));
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
// 优先处理 child
|
|
476
|
+
if (instance.child) {
|
|
477
|
+
return instance.child;
|
|
478
|
+
}
|
|
479
|
+
/**
|
|
480
|
+
* 递归向上查找可用的兄弟 instance
|
|
481
|
+
* @param instance
|
|
482
|
+
* @returns 返回下一个需要处理的兄弟 instance
|
|
483
|
+
*/
|
|
484
|
+
function returnSibling(instance) {
|
|
485
|
+
if (instance.sibling) {
|
|
486
|
+
return instance.sibling;
|
|
487
|
+
}
|
|
488
|
+
if (instance.parent) {
|
|
489
|
+
return returnSibling(instance.parent);
|
|
490
|
+
}
|
|
491
|
+
return null;
|
|
492
|
+
}
|
|
493
|
+
return returnSibling(instance);
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
function camelToKebab(str) {
|
|
497
|
+
return str.replace(/([A-Z])/g, (match, p1, offset) => {
|
|
498
|
+
if (offset === 0) {
|
|
499
|
+
return p1.toLowerCase();
|
|
500
|
+
}
|
|
501
|
+
else {
|
|
502
|
+
return '-' + p1.toLowerCase();
|
|
503
|
+
}
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
class ServerElement {
|
|
507
|
+
nodeName;
|
|
508
|
+
static createTextNode(content) {
|
|
509
|
+
const dom = new ServerElement('#text');
|
|
510
|
+
dom.content = content;
|
|
511
|
+
return dom;
|
|
512
|
+
}
|
|
513
|
+
static createComment(content) {
|
|
514
|
+
const dom = new ServerElement('#comment');
|
|
515
|
+
dom.content = content;
|
|
516
|
+
return dom;
|
|
517
|
+
}
|
|
518
|
+
static createElement(tag) {
|
|
519
|
+
return new ServerElement(tag);
|
|
520
|
+
}
|
|
521
|
+
constructor(nodeName) {
|
|
522
|
+
this.nodeName = nodeName;
|
|
523
|
+
this.style = {};
|
|
524
|
+
this.content = '';
|
|
525
|
+
this.children = [];
|
|
526
|
+
this.className = '';
|
|
527
|
+
this.attributes = new Map();
|
|
528
|
+
}
|
|
529
|
+
firstChild;
|
|
530
|
+
nextSibling;
|
|
531
|
+
parentNode;
|
|
532
|
+
className;
|
|
533
|
+
style;
|
|
534
|
+
content;
|
|
535
|
+
children;
|
|
536
|
+
attributes;
|
|
537
|
+
removeChild(dom) {
|
|
538
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
539
|
+
const index = this.children.findIndex(v => v === dom);
|
|
540
|
+
if (index != -1)
|
|
541
|
+
this.children.splice(index, 1);
|
|
542
|
+
// @ts-ignore
|
|
543
|
+
this.firstChild = undefined;
|
|
544
|
+
// @ts-ignore
|
|
545
|
+
this.nextSibling = undefined;
|
|
546
|
+
// @ts-ignore
|
|
547
|
+
this.parentNode = undefined;
|
|
548
|
+
/* eslint-enable @typescript-eslint/ban-ts-comment */
|
|
549
|
+
}
|
|
550
|
+
appendChild(dom) {
|
|
551
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
552
|
+
// @ts-ignore
|
|
553
|
+
dom.parentNode = this;
|
|
554
|
+
if (this.children.length > 0) {
|
|
555
|
+
// @ts-ignore
|
|
556
|
+
this.children[this.children.length - 1].nextSibling = dom;
|
|
557
|
+
}
|
|
558
|
+
// @ts-ignore
|
|
559
|
+
this.firstChild = this.children[0];
|
|
560
|
+
this.children.push(dom);
|
|
561
|
+
/* eslint-enable @typescript-eslint/ban-ts-comment */
|
|
562
|
+
}
|
|
563
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
564
|
+
setAttribute(name, value) {
|
|
565
|
+
if (value === '')
|
|
566
|
+
return this.attributes.delete(name);
|
|
567
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
568
|
+
// @ts-ignore
|
|
569
|
+
if (name === 'class')
|
|
570
|
+
return this.className = value;
|
|
571
|
+
// @ts-ignore
|
|
572
|
+
if (name === 'style')
|
|
573
|
+
return this.style = value;
|
|
574
|
+
/* eslint-enable @typescript-eslint/ban-ts-comment */
|
|
575
|
+
this.attributes.set(name, value);
|
|
576
|
+
}
|
|
577
|
+
removeAttribute(name) {
|
|
578
|
+
this.attributes.delete(name);
|
|
579
|
+
}
|
|
580
|
+
toString() {
|
|
581
|
+
if (this.nodeName === '#text')
|
|
582
|
+
return this.content;
|
|
583
|
+
if (this.nodeName === '#comment')
|
|
584
|
+
return this.content;
|
|
585
|
+
const styleString = Object.entries(this.style)
|
|
586
|
+
.map(([key, value]) => `${camelToKebab(key)}:${value}`)
|
|
587
|
+
.join(';');
|
|
588
|
+
const attributes = [...this.attributes.entries()];
|
|
589
|
+
if (styleString.length > 0)
|
|
590
|
+
attributes.push(['style', styleString]);
|
|
591
|
+
if (this.className.length > 0)
|
|
592
|
+
attributes.push(['class', this.className]);
|
|
593
|
+
const attributesString = attributes.map(([name, value]) => ` ${name}="${value}"`).join('');
|
|
594
|
+
return `<${this.nodeName}${attributesString}>${this.children.map(child => child.toString()).join('')}</${this.nodeName}>`;
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
function render$1(pluginContext, element, onComplete) {
|
|
598
|
+
const rootInstance = {
|
|
599
|
+
domRef: ServerElement.createElement('div'),
|
|
600
|
+
context: new InnerAirxComponentContext()
|
|
601
|
+
};
|
|
602
|
+
rootInstance.context.instance = rootInstance;
|
|
603
|
+
const context = {
|
|
604
|
+
rootInstance,
|
|
605
|
+
nextUnitOfWork: null,
|
|
606
|
+
needCommitDom: false
|
|
607
|
+
};
|
|
608
|
+
const appInstance = {
|
|
609
|
+
element,
|
|
610
|
+
parent: context.rootInstance,
|
|
611
|
+
memoProps: { ...element.props },
|
|
612
|
+
context: new InnerAirxComponentContext()
|
|
613
|
+
};
|
|
614
|
+
appInstance.context.instance = appInstance;
|
|
615
|
+
context.rootInstance.child = appInstance;
|
|
616
|
+
context.nextUnitOfWork = appInstance;
|
|
617
|
+
/**
|
|
618
|
+
* 提交 Dom 变化
|
|
619
|
+
*/
|
|
620
|
+
function commitDom(rootInstance, rootNode) {
|
|
621
|
+
const logger = createLogger('commitDom');
|
|
622
|
+
logger.debug('commitDom', rootInstance);
|
|
623
|
+
function updateDomProperties(dom, nextProps, prevProps = {}) {
|
|
624
|
+
const isKey = (key) => key === 'key';
|
|
625
|
+
const isRef = (key) => key === 'ref';
|
|
626
|
+
const isStyle = (key) => key === 'style';
|
|
627
|
+
const isClass = (key) => key === 'class';
|
|
628
|
+
const isEvent = (key) => key.startsWith("on");
|
|
629
|
+
const isChildren = (key) => key === 'children';
|
|
630
|
+
const isGone = (_prev, next) => (key) => !(key in next);
|
|
631
|
+
const isNew = (prev, next) => (key) => prev[key] !== next[key];
|
|
632
|
+
const isProperty = (key) => !isChildren(key) && !isEvent(key) && !isStyle(key) && !isClass(key) && !isKey(key) && !isRef(key);
|
|
633
|
+
// https://developer.mozilla.org/zh-CN/docs/Web/API/Node
|
|
634
|
+
if (dom.nodeName === '#text' || dom.nodeName === '#comment') {
|
|
635
|
+
const textNode = dom;
|
|
636
|
+
if (textNode.nodeValue !== nextProps.textContent) {
|
|
637
|
+
textNode.nodeValue = String(nextProps.textContent);
|
|
638
|
+
}
|
|
639
|
+
return;
|
|
640
|
+
}
|
|
641
|
+
// remove old style
|
|
642
|
+
const oldStyle = prevProps?.style;
|
|
643
|
+
if (typeof oldStyle === 'object' && oldStyle != null) {
|
|
644
|
+
Object.keys(oldStyle).forEach(key => {
|
|
645
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
646
|
+
delete dom.style[key];
|
|
647
|
+
/* eslint-enable @typescript-eslint/no-explicit-any */
|
|
648
|
+
});
|
|
649
|
+
}
|
|
650
|
+
// add new style
|
|
651
|
+
const newStyle = nextProps?.style;
|
|
652
|
+
if (typeof newStyle === 'object' && newStyle != null) {
|
|
653
|
+
Object.keys(newStyle).forEach((key) => {
|
|
654
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
655
|
+
const value = newStyle?.[key];
|
|
656
|
+
dom.style[key] = value == null ? '' : value;
|
|
657
|
+
/* eslint-enable @typescript-eslint/no-explicit-any */
|
|
658
|
+
});
|
|
659
|
+
}
|
|
660
|
+
if (dom.className !== nextProps?.class) {
|
|
661
|
+
if (!nextProps?.class) {
|
|
662
|
+
dom.removeAttribute('class');
|
|
663
|
+
}
|
|
664
|
+
else if (typeof nextProps?.class === 'string') {
|
|
665
|
+
dom.setAttribute('class', nextProps?.class);
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
// Remove old properties
|
|
669
|
+
Object.keys(prevProps)
|
|
670
|
+
.filter(isProperty)
|
|
671
|
+
.filter(isGone(prevProps, nextProps))
|
|
672
|
+
.forEach(name => dom.setAttribute(name, ''));
|
|
673
|
+
// Set new or changed properties
|
|
674
|
+
Object.keys(nextProps)
|
|
675
|
+
.filter(isProperty)
|
|
676
|
+
.filter(isNew(prevProps, nextProps))
|
|
677
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
678
|
+
.forEach(name => dom.setAttribute(name, nextProps[name]));
|
|
679
|
+
}
|
|
680
|
+
function getParentDom(instance) {
|
|
681
|
+
if (instance.parent?.domRef != null) {
|
|
682
|
+
return instance.parent.domRef;
|
|
683
|
+
}
|
|
684
|
+
if (instance.parent) {
|
|
685
|
+
return getParentDom(instance.parent);
|
|
686
|
+
}
|
|
687
|
+
throw new Error('Cant find dom');
|
|
688
|
+
}
|
|
689
|
+
/**
|
|
690
|
+
* 查找 instance 下所有的一级 dom
|
|
691
|
+
* @param instance
|
|
692
|
+
* @returns
|
|
693
|
+
*/
|
|
694
|
+
function getChildDoms(instance) {
|
|
695
|
+
const domList = [];
|
|
696
|
+
const todoList = [instance];
|
|
697
|
+
while (todoList.length > 0) {
|
|
698
|
+
const current = todoList.pop();
|
|
699
|
+
// 找到真实 dom 直接提交
|
|
700
|
+
if (current?.domRef != null) {
|
|
701
|
+
domList.push(current.domRef);
|
|
702
|
+
}
|
|
703
|
+
// 有子节点但是无真实 dom,向下继续查找
|
|
704
|
+
if (current?.domRef == null) {
|
|
705
|
+
if (current?.child != null) {
|
|
706
|
+
todoList.push(current.child);
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
// 可能有兄弟节点,则需要继续查找
|
|
710
|
+
if (current?.sibling != null) {
|
|
711
|
+
todoList.push(current.sibling);
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
return domList;
|
|
715
|
+
}
|
|
716
|
+
function commitInstanceDom(nextInstance, oldNode) {
|
|
717
|
+
// 移除标删元素
|
|
718
|
+
if (nextInstance.deletions) {
|
|
719
|
+
for (const deletion of nextInstance.deletions) {
|
|
720
|
+
const domList = getChildDoms(deletion);
|
|
721
|
+
for (let index = 0; index < domList.length; index++) {
|
|
722
|
+
const dom = domList[index];
|
|
723
|
+
if (dom && dom.parentNode)
|
|
724
|
+
dom.parentNode.removeChild(dom);
|
|
725
|
+
}
|
|
726
|
+
deletion.context.triggerUnmounted();
|
|
727
|
+
}
|
|
728
|
+
nextInstance.deletions.clear();
|
|
729
|
+
}
|
|
730
|
+
// 创建 dom
|
|
731
|
+
if (nextInstance.domRef == null) {
|
|
732
|
+
if (nextInstance.element == null)
|
|
733
|
+
throw new Error('???');
|
|
734
|
+
if (typeof nextInstance.element.type === 'string') {
|
|
735
|
+
if (nextInstance.element.type === 'text') {
|
|
736
|
+
const textContent = nextInstance.element.props.textContent;
|
|
737
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
738
|
+
nextInstance.domRef = ServerElement.createTextNode(textContent);
|
|
739
|
+
}
|
|
740
|
+
else if (nextInstance.element.type === 'comment') {
|
|
741
|
+
const textContent = nextInstance.element.props.textContent;
|
|
742
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
743
|
+
nextInstance.domRef = ServerElement.createComment(textContent);
|
|
744
|
+
}
|
|
745
|
+
else {
|
|
746
|
+
nextInstance.domRef = ServerElement.createElement(nextInstance.element.type);
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
// 更新属性
|
|
751
|
+
if (nextInstance.domRef != null && nextInstance.element != null) {
|
|
752
|
+
updateDomProperties(nextInstance.domRef, nextInstance.element.props, nextInstance.beforeElement?.props);
|
|
753
|
+
}
|
|
754
|
+
// 插入 parent
|
|
755
|
+
// TODO: 针对仅移动时优化
|
|
756
|
+
if (nextInstance.domRef != null) {
|
|
757
|
+
if (oldNode !== nextInstance.domRef) {
|
|
758
|
+
if (nextInstance.domRef.parentNode) {
|
|
759
|
+
nextInstance.domRef.parentNode.removeChild(nextInstance.domRef);
|
|
760
|
+
}
|
|
761
|
+
const parentDom = getParentDom(nextInstance);
|
|
762
|
+
parentDom.appendChild(nextInstance.domRef);
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
function commitWalkV2(initInstance, initNode) {
|
|
767
|
+
// 创建一个栈,将根节点压入栈中
|
|
768
|
+
const stack = [[initInstance, initNode]];
|
|
769
|
+
while (stack.length > 0) {
|
|
770
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
771
|
+
const stackLayer = stack.pop();
|
|
772
|
+
if (typeof stackLayer === 'function') {
|
|
773
|
+
stackLayer();
|
|
774
|
+
continue;
|
|
775
|
+
}
|
|
776
|
+
const [instance, node] = stackLayer;
|
|
777
|
+
commitInstanceDom(instance, node);
|
|
778
|
+
// stack 是先入后出
|
|
779
|
+
// 实际上是先渲染 child
|
|
780
|
+
// 这里然后再渲染 sibling
|
|
781
|
+
// 执行生命周期的 Mount
|
|
782
|
+
stack.push(() => instance.context.triggerMounted());
|
|
783
|
+
// 更新下一个兄弟节点
|
|
784
|
+
if (instance.sibling != null) {
|
|
785
|
+
const siblingNode = instance.domRef
|
|
786
|
+
? instance.domRef.nextSibling
|
|
787
|
+
: node?.nextSibling;
|
|
788
|
+
stack.push([instance.sibling, siblingNode || undefined]);
|
|
789
|
+
}
|
|
790
|
+
// 更新下一个子节点
|
|
791
|
+
if (instance.child != null) {
|
|
792
|
+
const childNode = instance.domRef
|
|
793
|
+
? instance.domRef.firstChild
|
|
794
|
+
: node;
|
|
795
|
+
stack.push([instance.child, childNode || undefined]);
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
commitWalkV2(rootInstance, rootNode);
|
|
800
|
+
}
|
|
801
|
+
while (context.nextUnitOfWork) {
|
|
802
|
+
context.nextUnitOfWork = performUnitOfWork(pluginContext, context.nextUnitOfWork);
|
|
803
|
+
}
|
|
804
|
+
commitDom(context.rootInstance.child, context.rootInstance.domRef?.firstChild || undefined);
|
|
805
|
+
onComplete(context.rootInstance.domRef?.toString() || '');
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
function render(pluginContext, element, domRef) {
|
|
809
|
+
const rootInstance = {
|
|
810
|
+
domRef,
|
|
811
|
+
context: new InnerAirxComponentContext()
|
|
812
|
+
};
|
|
813
|
+
rootInstance.context.instance = rootInstance;
|
|
814
|
+
const context = {
|
|
815
|
+
rootInstance,
|
|
816
|
+
nextUnitOfWork: null,
|
|
817
|
+
needCommitDom: false
|
|
818
|
+
};
|
|
819
|
+
const appInstance = {
|
|
820
|
+
element,
|
|
821
|
+
parent: context.rootInstance,
|
|
822
|
+
memoProps: { ...element.props },
|
|
823
|
+
context: new InnerAirxComponentContext()
|
|
824
|
+
};
|
|
825
|
+
appInstance.context.instance = appInstance;
|
|
826
|
+
context.rootInstance.child = appInstance;
|
|
827
|
+
context.nextUnitOfWork = appInstance;
|
|
828
|
+
/**
|
|
829
|
+
* 提交 Dom 变化
|
|
830
|
+
*/
|
|
831
|
+
function commitDom(rootInstance, rootNode) {
|
|
832
|
+
const logger = createLogger('commitDom');
|
|
833
|
+
logger.debug('commitDom', rootInstance);
|
|
834
|
+
function updateDomProperties(dom, nextProps, prevProps = {}) {
|
|
835
|
+
for (const plugin of pluginContext.plugins) {
|
|
836
|
+
if (plugin.updateDom)
|
|
837
|
+
plugin.updateDom(dom, nextProps, prevProps);
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
function getParentDom(instance) {
|
|
841
|
+
if (instance.parent?.domRef != null) {
|
|
842
|
+
return instance.parent.domRef;
|
|
843
|
+
}
|
|
844
|
+
if (instance.parent) {
|
|
845
|
+
return getParentDom(instance.parent);
|
|
846
|
+
}
|
|
847
|
+
throw new Error('Cant find dom');
|
|
848
|
+
}
|
|
849
|
+
/**
|
|
850
|
+
* 查找 instance 下所有的一级 dom
|
|
851
|
+
* @param instance
|
|
852
|
+
* @returns
|
|
853
|
+
*/
|
|
854
|
+
function getChildDoms(instance) {
|
|
855
|
+
const domList = [];
|
|
856
|
+
const todoList = [instance];
|
|
857
|
+
while (todoList.length > 0) {
|
|
858
|
+
const current = todoList.pop();
|
|
859
|
+
// 找到真实 dom 直接提交
|
|
860
|
+
if (current?.domRef != null) {
|
|
861
|
+
domList.push(current.domRef);
|
|
862
|
+
}
|
|
863
|
+
// 有子节点但是无真实 dom,向下继续查找
|
|
864
|
+
if (current?.domRef == null) {
|
|
865
|
+
if (current?.child != null) {
|
|
866
|
+
todoList.push(current.child);
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
// 可能有兄弟节点,则需要继续查找
|
|
870
|
+
if (current?.sibling != null) {
|
|
871
|
+
todoList.push(current.sibling);
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
return domList;
|
|
875
|
+
}
|
|
876
|
+
function commitInstanceDom(nextInstance, oldNode) {
|
|
877
|
+
// 移除标删元素
|
|
878
|
+
if (nextInstance.deletions) {
|
|
879
|
+
for (const deletion of nextInstance.deletions) {
|
|
880
|
+
const domList = getChildDoms(deletion);
|
|
881
|
+
for (let index = 0; index < domList.length; index++) {
|
|
882
|
+
const dom = domList[index];
|
|
883
|
+
if (dom && dom.parentNode)
|
|
884
|
+
dom.parentNode.removeChild(dom);
|
|
885
|
+
}
|
|
886
|
+
deletion.context.triggerUnmounted();
|
|
887
|
+
}
|
|
888
|
+
nextInstance.deletions.clear();
|
|
889
|
+
}
|
|
890
|
+
// 创建 dom
|
|
891
|
+
if (nextInstance.domRef == null) {
|
|
892
|
+
if (nextInstance.element == null)
|
|
893
|
+
throw new Error('???');
|
|
894
|
+
if (typeof nextInstance.element.type === 'string') {
|
|
895
|
+
if (nextInstance.element.type === 'text') {
|
|
896
|
+
const textContent = nextInstance.element.props.textContent;
|
|
897
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
898
|
+
nextInstance.domRef = document.createTextNode(textContent);
|
|
899
|
+
}
|
|
900
|
+
else if (nextInstance.element.type === 'comment') {
|
|
901
|
+
const textContent = nextInstance.element.props.textContent;
|
|
902
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
903
|
+
nextInstance.domRef = document.createComment(textContent);
|
|
904
|
+
}
|
|
905
|
+
else {
|
|
906
|
+
nextInstance.domRef = nextInstance.elementNamespace
|
|
907
|
+
? document.createElementNS(nextInstance.elementNamespace, nextInstance.element.type)
|
|
908
|
+
: document.createElement(nextInstance.element.type);
|
|
909
|
+
}
|
|
910
|
+
if (nextInstance.domRef) {
|
|
911
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
912
|
+
nextInstance.domRef.airxInstance = nextInstance;
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
// 更新属性
|
|
917
|
+
if (nextInstance.domRef != null && nextInstance.element != null) {
|
|
918
|
+
updateDomProperties(nextInstance.domRef, nextInstance.element.props, nextInstance.beforeElement?.props);
|
|
919
|
+
}
|
|
920
|
+
// 插入 parent
|
|
921
|
+
// TODO: 针对仅移动时优化
|
|
922
|
+
if (nextInstance.domRef != null) {
|
|
923
|
+
if (oldNode !== nextInstance.domRef) {
|
|
924
|
+
if (nextInstance.domRef.parentNode) {
|
|
925
|
+
nextInstance.domRef.parentNode.removeChild(nextInstance.domRef);
|
|
926
|
+
}
|
|
927
|
+
const parentDom = getParentDom(nextInstance);
|
|
928
|
+
parentDom.appendChild(nextInstance.domRef);
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
function commitWalkV2(initInstance, initNode) {
|
|
933
|
+
// 创建一个栈,将根节点压入栈中
|
|
934
|
+
const stack = [[initInstance, initNode]];
|
|
935
|
+
while (stack.length > 0) {
|
|
936
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
937
|
+
const stackLayer = stack.pop();
|
|
938
|
+
if (typeof stackLayer === 'function') {
|
|
939
|
+
stackLayer();
|
|
940
|
+
continue;
|
|
941
|
+
}
|
|
942
|
+
const [instance, node] = stackLayer;
|
|
943
|
+
commitInstanceDom(instance, node);
|
|
944
|
+
// stack 是先入后出
|
|
945
|
+
// 实际上是先渲染 child
|
|
946
|
+
// 这里然后再渲染 sibling
|
|
947
|
+
// 执行生命周期的 Mount
|
|
948
|
+
stack.push(() => instance.context.triggerMounted());
|
|
949
|
+
// 更新下一个兄弟节点
|
|
950
|
+
if (instance.sibling != null) {
|
|
951
|
+
const siblingNode = instance.domRef
|
|
952
|
+
? instance.domRef.nextSibling
|
|
953
|
+
: node?.nextSibling;
|
|
954
|
+
stack.push([instance.sibling, siblingNode || undefined]);
|
|
955
|
+
}
|
|
956
|
+
// 更新下一个子节点
|
|
957
|
+
if (instance.child != null) {
|
|
958
|
+
const childNode = instance.domRef
|
|
959
|
+
? instance.domRef.firstChild
|
|
960
|
+
: node;
|
|
961
|
+
stack.push([instance.child, childNode || undefined]);
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
commitWalkV2(rootInstance, rootNode);
|
|
966
|
+
}
|
|
967
|
+
function onUpdateRequire() {
|
|
968
|
+
if (context.nextUnitOfWork == null && context.rootInstance.child) {
|
|
969
|
+
context.nextUnitOfWork = context.rootInstance.child;
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
/**
|
|
973
|
+
* 调度
|
|
974
|
+
*/
|
|
975
|
+
function workLoop(deadline) {
|
|
976
|
+
let shouldYield = false;
|
|
977
|
+
const logger = createLogger('workLoop');
|
|
978
|
+
while (context.nextUnitOfWork && !shouldYield) {
|
|
979
|
+
logger.debug('nextUnitOfWork', context.nextUnitOfWork);
|
|
980
|
+
context.nextUnitOfWork = performUnitOfWork(pluginContext, context.nextUnitOfWork, onUpdateRequire);
|
|
981
|
+
if (context.nextUnitOfWork == null)
|
|
982
|
+
context.needCommitDom = true;
|
|
983
|
+
if (deadline)
|
|
984
|
+
shouldYield = deadline.timeRemaining() < 1;
|
|
985
|
+
}
|
|
986
|
+
if (context.needCommitDom && context.rootInstance.child) {
|
|
987
|
+
commitDom(context.rootInstance.child, context.rootInstance.domRef?.firstChild || undefined);
|
|
988
|
+
context.needCommitDom = false;
|
|
989
|
+
}
|
|
990
|
+
// TODO: 兼容性
|
|
991
|
+
requestIdleCallback(workLoop);
|
|
992
|
+
}
|
|
993
|
+
// 开始调度
|
|
994
|
+
workLoop();
|
|
995
|
+
return context.rootInstance;
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
class BasicLogic {
|
|
999
|
+
logger = createLogger('basicLogicPlugin');
|
|
1000
|
+
isSameProps(preProps, nextProps) {
|
|
1001
|
+
if (Object.is(nextProps, preProps)) {
|
|
1002
|
+
return true;
|
|
1003
|
+
}
|
|
1004
|
+
if (typeof preProps !== 'object'
|
|
1005
|
+
|| typeof nextProps !== 'object'
|
|
1006
|
+
|| preProps === null
|
|
1007
|
+
|| nextProps === null) {
|
|
1008
|
+
this.logger.debug('props must be an object');
|
|
1009
|
+
return false;
|
|
1010
|
+
}
|
|
1011
|
+
// 对应 key 的值不相同返回 false
|
|
1012
|
+
const prevKeys = Object.keys(preProps);
|
|
1013
|
+
for (let index = 0; index < prevKeys.length; index++) {
|
|
1014
|
+
const key = prevKeys[index];
|
|
1015
|
+
if (key !== 'children' && key !== 'key') {
|
|
1016
|
+
if (!Object.hasOwn(nextProps, key))
|
|
1017
|
+
return false;
|
|
1018
|
+
if (!Object.is(preProps[key], nextProps[key]))
|
|
1019
|
+
return false;
|
|
1020
|
+
}
|
|
1021
|
+
if (key === 'children') {
|
|
1022
|
+
const prevChildren = preProps['children'];
|
|
1023
|
+
const nextChildren = nextProps['children'];
|
|
1024
|
+
// children 都是空的,则无需更新
|
|
1025
|
+
if (prevChildren.length === 0 && nextChildren.length === 0)
|
|
1026
|
+
return true;
|
|
1027
|
+
// 简单比较一下 child 的引用
|
|
1028
|
+
for (let index = 0; index < prevChildren.length; index++) {
|
|
1029
|
+
const prevChild = prevChildren[index];
|
|
1030
|
+
const nextChild = nextChildren[index];
|
|
1031
|
+
if (prevChild !== nextChild)
|
|
1032
|
+
return false;
|
|
1033
|
+
if (typeof prevChild !== typeof nextChild)
|
|
1034
|
+
return false;
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
return true;
|
|
1038
|
+
}
|
|
1039
|
+
return false;
|
|
1040
|
+
}
|
|
1041
|
+
isReRender(instance) {
|
|
1042
|
+
const nextProps = instance.element?.props;
|
|
1043
|
+
const preProps = instance.beforeElement?.props;
|
|
1044
|
+
if (!this.isSameProps(preProps, nextProps))
|
|
1045
|
+
return true;
|
|
1046
|
+
}
|
|
1047
|
+
updateDom(dom, nextProps, prevProps = {}) {
|
|
1048
|
+
this.logger.debug('updateDom', dom, nextProps, prevProps);
|
|
1049
|
+
const isKey = (key) => key === 'key';
|
|
1050
|
+
const isRef = (key) => key === 'ref';
|
|
1051
|
+
const isStyle = (key) => key === 'style';
|
|
1052
|
+
const isClass = (key) => key === 'class';
|
|
1053
|
+
const isEvent = (key) => key.startsWith("on");
|
|
1054
|
+
const isChildren = (key) => key === 'children';
|
|
1055
|
+
const isGone = (_prev, next) => (key) => !(key in next);
|
|
1056
|
+
const isNew = (prev, next) => (key) => prev[key] !== next[key];
|
|
1057
|
+
const isProperty = (key) => !isChildren(key) && !isEvent(key) && !isStyle(key) && !isClass(key) && !isKey(key) && !isRef(key);
|
|
1058
|
+
// https://developer.mozilla.org/zh-CN/docs/Web/API/Node
|
|
1059
|
+
if (dom.nodeName === '#text' || dom.nodeName === '#comment') {
|
|
1060
|
+
const textNode = dom;
|
|
1061
|
+
if (textNode.nodeValue !== nextProps.textContent) {
|
|
1062
|
+
textNode.nodeValue = String(nextProps.textContent);
|
|
1063
|
+
}
|
|
1064
|
+
return;
|
|
1065
|
+
}
|
|
1066
|
+
// remove old style
|
|
1067
|
+
const oldStyle = prevProps?.style;
|
|
1068
|
+
if (typeof oldStyle === 'object' && oldStyle != null) {
|
|
1069
|
+
if (!('style' in dom) || !dom.style)
|
|
1070
|
+
return;
|
|
1071
|
+
Object.keys(oldStyle).forEach(key => {
|
|
1072
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
1073
|
+
if ('style' in dom && dom.style) {
|
|
1074
|
+
delete dom.style[key];
|
|
1075
|
+
/* eslint-enable @typescript-eslint/no-explicit-any */
|
|
1076
|
+
}
|
|
1077
|
+
});
|
|
1078
|
+
}
|
|
1079
|
+
// add new style
|
|
1080
|
+
const newStyle = nextProps?.style;
|
|
1081
|
+
if (typeof newStyle === 'object' && newStyle != null) {
|
|
1082
|
+
if (!('style' in dom) || !dom.style)
|
|
1083
|
+
return;
|
|
1084
|
+
Object.keys(newStyle).forEach((key) => {
|
|
1085
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
1086
|
+
const value = newStyle?.[key];
|
|
1087
|
+
dom.style[key] = value == null ? '' : value;
|
|
1088
|
+
/* eslint-enable @typescript-eslint/no-explicit-any */
|
|
1089
|
+
});
|
|
1090
|
+
}
|
|
1091
|
+
if (dom.className !== nextProps?.class) {
|
|
1092
|
+
if (!nextProps?.class) {
|
|
1093
|
+
dom.removeAttribute('class');
|
|
1094
|
+
}
|
|
1095
|
+
else if (typeof nextProps?.class === 'string') {
|
|
1096
|
+
dom.setAttribute('class', nextProps?.class);
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
//Remove old or changed event listeners
|
|
1100
|
+
Object.keys(prevProps)
|
|
1101
|
+
.filter(isEvent)
|
|
1102
|
+
.filter(key => !(key in nextProps) ||
|
|
1103
|
+
isNew(prevProps, nextProps)(key))
|
|
1104
|
+
.forEach(name => {
|
|
1105
|
+
const eventType = name
|
|
1106
|
+
.toLowerCase()
|
|
1107
|
+
.substring(2);
|
|
1108
|
+
if (prevProps[name] == null)
|
|
1109
|
+
return;
|
|
1110
|
+
if (typeof prevProps[name] !== 'function') {
|
|
1111
|
+
console.error('EventListener is not a function');
|
|
1112
|
+
}
|
|
1113
|
+
else {
|
|
1114
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1115
|
+
dom.removeEventListener(eventType, prevProps[name]);
|
|
1116
|
+
}
|
|
1117
|
+
});
|
|
1118
|
+
// Add event listeners
|
|
1119
|
+
Object.keys(nextProps)
|
|
1120
|
+
.filter(isEvent)
|
|
1121
|
+
.filter(isNew(prevProps, nextProps))
|
|
1122
|
+
.forEach(name => {
|
|
1123
|
+
const eventType = name
|
|
1124
|
+
.toLowerCase()
|
|
1125
|
+
.substring(2);
|
|
1126
|
+
if (nextProps[name] == null)
|
|
1127
|
+
return;
|
|
1128
|
+
if (typeof nextProps[name] !== 'function') {
|
|
1129
|
+
console.error('EventListener is not a function');
|
|
1130
|
+
}
|
|
1131
|
+
else {
|
|
1132
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1133
|
+
dom.addEventListener(eventType, nextProps[name]);
|
|
1134
|
+
}
|
|
1135
|
+
});
|
|
1136
|
+
// Remove old properties
|
|
1137
|
+
Object.keys(prevProps)
|
|
1138
|
+
.filter(isProperty)
|
|
1139
|
+
.filter(isGone(prevProps, nextProps))
|
|
1140
|
+
.forEach(name => dom.setAttribute(name, ''));
|
|
1141
|
+
// Set new or changed properties
|
|
1142
|
+
Object.keys(nextProps)
|
|
1143
|
+
.filter(isProperty)
|
|
1144
|
+
.filter(isNew(prevProps, nextProps))
|
|
1145
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1146
|
+
.forEach(name => dom.setAttribute(name, nextProps[name]));
|
|
1147
|
+
}
|
|
1148
|
+
isReuseInstance(instance, nextElement) {
|
|
1149
|
+
if (instance && instance.element && instance.element.type !== nextElement.type) {
|
|
1150
|
+
return false;
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
1154
|
+
|
|
1155
|
+
class InjectSystem {
|
|
1156
|
+
getProvideValueForParent(instance, key) {
|
|
1157
|
+
if (instance && instance.context) {
|
|
1158
|
+
const value = instance.context.providedMap.get(key);
|
|
1159
|
+
if (value != undefined)
|
|
1160
|
+
return value;
|
|
1161
|
+
if (instance.parent) {
|
|
1162
|
+
return this.getProvideValueForParent(instance.parent, key);
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
return undefined;
|
|
1166
|
+
}
|
|
1167
|
+
isReuseInstance(instance) {
|
|
1168
|
+
const injectedKeys = [...instance.context.injectedMap.keys()];
|
|
1169
|
+
for (let index = 0; index < injectedKeys.length; index++) {
|
|
1170
|
+
const key = injectedKeys[index];
|
|
1171
|
+
const currentValue = instance.context.injectedMap.get(key);
|
|
1172
|
+
const parentValue = this.getProvideValueForParent(instance.parent, key);
|
|
1173
|
+
// 如果发现任何值发生变化,则重建实例
|
|
1174
|
+
if (parentValue !== currentValue)
|
|
1175
|
+
return false;
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1180
|
+
class PluginContext {
|
|
1181
|
+
plugins = [
|
|
1182
|
+
new BasicLogic(),
|
|
1183
|
+
new InjectSystem()
|
|
1184
|
+
];
|
|
1185
|
+
registerPlugin(...plugins) {
|
|
1186
|
+
this.plugins.push(...plugins);
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1191
|
+
function createApp(element) {
|
|
1192
|
+
const appContext = new PluginContext();
|
|
1193
|
+
const app = {
|
|
1194
|
+
plugin: (...plugins) => {
|
|
1195
|
+
appContext.registerPlugin(...plugins);
|
|
1196
|
+
return app;
|
|
1197
|
+
},
|
|
1198
|
+
mount: (container) => {
|
|
1199
|
+
render(appContext, element, container);
|
|
1200
|
+
return app;
|
|
1201
|
+
},
|
|
1202
|
+
renderToHTML: () => {
|
|
1203
|
+
return new Promise(resolve => {
|
|
1204
|
+
render$1(appContext, element, resolve);
|
|
1205
|
+
});
|
|
1206
|
+
}
|
|
1207
|
+
};
|
|
1208
|
+
return app;
|
|
1209
|
+
}
|
|
1210
|
+
|
|
1211
|
+
export { Fragment, component, createApp, createElement, createSignal, inject, onMounted, onUnmounted, provide, watchSignal };
|
|
1212
|
+
//# sourceMappingURL=index.js.map
|