airx 0.2.0 → 0.2.2

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