airx 0.1.3 → 0.1.4

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/render.js DELETED
@@ -1,663 +0,0 @@
1
- import { createCollector, createRef, watch } from './reactive';
2
- import { createElement, isValidElement } from './element';
3
- import { createLogger } from './logger';
4
- const globalContext = {
5
- current: null
6
- };
7
- export function useContext() {
8
- if (globalContext.current == null) {
9
- throw new Error('Unable to find a valid component context');
10
- }
11
- return globalContext.current;
12
- }
13
- export const onMounted = (listener) => {
14
- return useContext().onMounted(listener);
15
- };
16
- export const onUnmounted = (listener) => {
17
- return useContext().onUnmounted(listener);
18
- };
19
- export const inject = (key) => {
20
- return useContext().inject(key);
21
- };
22
- export const provide = (key, value) => {
23
- return useContext().provide(key, value);
24
- };
25
- class InnerAirxComponentContext {
26
- instance;
27
- disposers = new Set();
28
- providedMap = new Map();
29
- injectedMap = new Map();
30
- mountListeners = new Set();
31
- unmountedListeners = new Set();
32
- triggerMounted() {
33
- this.mountListeners.forEach(listener => {
34
- let disposer = null;
35
- try {
36
- disposer = listener();
37
- }
38
- catch (err) {
39
- console.error(err, listener);
40
- }
41
- if (typeof disposer === 'function') {
42
- this.addDisposer(disposer);
43
- }
44
- });
45
- // 生命周期只会调用一次
46
- this.mountListeners.clear();
47
- }
48
- triggerUnmounted() {
49
- // 递归的调用子 child 的 Unmounted
50
- if (this.instance?.child != null) {
51
- this.instance.child.context.triggerUnmounted();
52
- }
53
- // 处理自己
54
- this.unmountedListeners.forEach(listener => {
55
- try {
56
- listener();
57
- }
58
- catch (err) {
59
- console.error(err, listener);
60
- }
61
- });
62
- // 处理兄弟节点
63
- if (this.instance?.sibling != null) {
64
- this.instance.sibling.context.triggerUnmounted();
65
- }
66
- this.dispose();
67
- }
68
- onMounted(listener) {
69
- this.mountListeners.add(listener);
70
- }
71
- onUnmounted(listener) {
72
- this.unmountedListeners.add(listener);
73
- }
74
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
75
- provide(key, value) {
76
- if (!this.providedMap.has(key)) {
77
- this.providedMap.set(key, createRef(value));
78
- }
79
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
80
- const ref = this.providedMap.get(key);
81
- ref.value = value;
82
- return newValue => ref.value = newValue;
83
- }
84
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
85
- inject(key) {
86
- if (!this.injectedMap.has(key)) {
87
- this.injectedMap.set(key, createRef(null));
88
- }
89
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
90
- const resultRef = this.injectedMap.get(key);
91
- /**
92
- * 从当前组件开始向上查找
93
- * @returns 返回找到的值的 Ref 和提供该值的实例
94
- */
95
- const getResultOfContext = () => {
96
- let provider = null;
97
- let result = null;
98
- let nextParentInstance = this.instance;
99
- while (nextParentInstance != null) {
100
- provider = nextParentInstance;
101
- result = nextParentInstance.context.providedMap.get(key) || null;
102
- if (result != null)
103
- break;
104
- nextParentInstance = nextParentInstance.parent;
105
- }
106
- return { result, provider };
107
- };
108
- const { result, provider } = getResultOfContext();
109
- if (result != null && provider != null) {
110
- // 每当值发生变化,同步到当前的 Ref 上
111
- this.addDisposer(watch(result, () => {
112
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
113
- resultRef.value = result.value;
114
- }));
115
- }
116
- resultRef.value = result?.value;
117
- return resultRef;
118
- }
119
- addDisposer(...disposers) {
120
- disposers.forEach(disposer => {
121
- this.disposers.add(disposer);
122
- });
123
- }
124
- dispose() {
125
- this.disposers.forEach((disposer) => {
126
- try {
127
- disposer();
128
- }
129
- catch (err) {
130
- // eslint-disable-next-line no-console
131
- console.error(err, disposer);
132
- }
133
- });
134
- this.disposers.clear();
135
- this.injectedMap.clear();
136
- this.providedMap.clear();
137
- this.mountListeners.clear();
138
- this.unmountedListeners.clear();
139
- }
140
- /**
141
- * 当 context 传递给外部消费时,隐藏内部实现,仅暴露接口定义的内容
142
- * @returns 和 AirxComponentContext 接口完全一致的对象
143
- */
144
- getSafeContext() {
145
- return {
146
- inject: k => this.inject(k),
147
- provide: (k, v) => this.provide(k, v),
148
- onMounted: listener => this.onMounted(listener),
149
- onUnmounted: listener => this.onUnmounted(listener)
150
- };
151
- }
152
- }
153
- export function render(element, domRef) {
154
- const rootInstance = {
155
- domRef,
156
- context: new InnerAirxComponentContext()
157
- };
158
- rootInstance.context.instance = rootInstance;
159
- const context = {
160
- rootInstance,
161
- nextUnitOfWork: null,
162
- needCommitDom: false
163
- };
164
- const appInstance = {
165
- element,
166
- parent: context.rootInstance,
167
- memoProps: { ...element.props },
168
- context: new InnerAirxComponentContext()
169
- };
170
- appInstance.context.instance = appInstance;
171
- context.rootInstance.child = appInstance;
172
- context.nextUnitOfWork = appInstance;
173
- /**
174
- * 更新 children
175
- * @param parentInstance 当前正在处理的组件的实例
176
- * @param children 当前组件的子节点
177
- */
178
- function reconcileChildren(parentInstance, childrenElementArray) {
179
- const logger = createLogger('reconcileChildren');
180
- logger.debug('reconcileChildren', parentInstance, childrenElementArray);
181
- // parentInstance ←--------
182
- // | ↑ ↑
183
- // child parent parent
184
- // ↓ | |
185
- // instance -sibling→ instance -→ ....
186
- /**
187
- * 内部通过 index 生成 element 的 key
188
- * 通过前缀来避免和用户手动设置的 key 发生冲突
189
- * @param index
190
- * @returns 生成的 key
191
- */
192
- function getInnerElementIndexKey(index) {
193
- return `airx-element-inner-key:${index}`;
194
- }
195
- /**
196
- * 将 children 实例链转成 Map 便于此处消费
197
- * @param firstChild children 实例的第一个元素
198
- * @returns 从 firstChild 开始之后的所有 sibling 实例组成的 Map
199
- */
200
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
201
- function getChildrenInstanceMap(firstChild) {
202
- // 不使用递归是因为递归容易爆栈
203
- let nextIndex = 0;
204
- let nextChild = firstChild;
205
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
206
- const map = new Map();
207
- while (nextChild) {
208
- /* 如果有 key,则将 key 作为 map key,否则将 index 作为 key */
209
- if (nextChild.element?.props.key != null) {
210
- map.set(nextChild.element?.props.key, nextChild);
211
- }
212
- else {
213
- map.set(getInnerElementIndexKey(nextIndex), nextChild);
214
- }
215
- nextChild = nextChild.sibling;
216
- nextIndex += 1;
217
- }
218
- return map;
219
- }
220
- const newChildrenInstanceArray = [];
221
- const childrenInstanceMap = getChildrenInstanceMap(parentInstance.child);
222
- function getChildInstance(child, index) {
223
- if (child.props.key != null) {
224
- const key = child.props.key;
225
- const instance = childrenInstanceMap.get(key) || null;
226
- return [instance, () => childrenInstanceMap.delete(key)];
227
- }
228
- const innerKey = getInnerElementIndexKey(index);
229
- const instance = childrenInstanceMap.get(innerKey) || null;
230
- return [instance, () => childrenInstanceMap.delete(innerKey)];
231
- }
232
- /**
233
- * 浅比较 props 是否相等
234
- * @param prev 之前的 props
235
- * @param next 下一个 props
236
- */
237
- function isSameElementType(instance, nextElement) {
238
- if (instance == null)
239
- return false;
240
- if (instance.element == null)
241
- return false;
242
- if (instance.element.type !== nextElement.type)
243
- return false;
244
- return true;
245
- }
246
- function updateMemoProps(instance) {
247
- if (instance.element == null)
248
- return;
249
- if (instance.memoProps == null)
250
- instance.memoProps = {};
251
- // 简单来说就是以下几件事情
252
- // 始终保持 props 的引用不变
253
- // 1. 创建一个新对象来保存 beforeElement props 的状态
254
- // 2. 将新的 element 上的 props 引用设置为之前的 props
255
- // 3. 将新的 element 上的 props 更新到之前的 props 上去
256
- if (instance.memoProps != null) {
257
- for (const key in instance.memoProps) {
258
- delete instance.memoProps[key];
259
- }
260
- }
261
- // 将新的 props 更新上去
262
- for (const key in instance.element.props) {
263
- const value = instance.element.props[key];
264
- instance.memoProps[key] = value;
265
- }
266
- }
267
- function shouldUpdate(instance) {
268
- const nextProps = instance.element?.props;
269
- const preProps = instance.beforeElement?.props;
270
- if (Object.is(nextProps, preProps)) {
271
- return false;
272
- }
273
- if (typeof preProps !== 'object'
274
- || typeof nextProps !== 'object'
275
- || preProps === null
276
- || nextProps === null) {
277
- logger.debug('props must be an object');
278
- return true;
279
- }
280
- // 对应 key 的值不相同返回 false
281
- const prevKeys = Object.keys(preProps);
282
- for (let index = 0; index < prevKeys.length; index++) {
283
- const key = prevKeys[index];
284
- if (key !== 'children' && key !== 'key') {
285
- if (!Object.hasOwn(nextProps, key))
286
- return true;
287
- if (!Object.is(preProps[key], nextProps[key]))
288
- return true;
289
- }
290
- if (key === 'children') {
291
- const prevChildren = preProps['children'];
292
- const nextChildren = nextProps['children'];
293
- // children 都是空的,则无需更新
294
- if (prevChildren.length === 0 && nextChildren.length === 0)
295
- return false;
296
- // 简单比较一下 child 的引用
297
- for (let index = 0; index < prevChildren.length; index++) {
298
- const prevChild = prevChildren[index];
299
- const nextChild = nextChildren[index];
300
- if (prevChild !== nextChild)
301
- return true;
302
- if (typeof prevChild !== typeof nextChild)
303
- return true;
304
- }
305
- }
306
- return false;
307
- }
308
- return true;
309
- }
310
- // 依次遍历 child 并和 instance 对比
311
- for (let index = 0; index < childrenElementArray.length; index++) {
312
- const element = childrenElementArray[index];
313
- const [instance, seize] = getChildInstance(element, index);
314
- const isSameType = instance && isSameElementType(instance, element);
315
- if (isSameType) {
316
- seize(); // 从 childrenInstanceMap 中释放
317
- newChildrenInstanceArray.push(instance);
318
- instance.beforeElement = instance.element;
319
- instance.element = element;
320
- updateMemoProps(instance);
321
- // 未标注更新的检查一下是否需要更新
322
- if (instance.requiredUpdate !== true && typeof element.type === 'function') {
323
- // FIXME: 没必要让 child 都 requiredUpdate,最好时可以只通过 shouldUpdate 来判断是否需要更新
324
- instance.requiredUpdate = parentInstance.requiredUpdate || shouldUpdate(instance);
325
- }
326
- }
327
- else {
328
- const context = new InnerAirxComponentContext();
329
- const instance = { element, context };
330
- newChildrenInstanceArray.push(instance);
331
- context.instance = instance;
332
- updateMemoProps(instance);
333
- }
334
- }
335
- // 新的 node
336
- for (let index = 0; index < newChildrenInstanceArray.length; index++) {
337
- const instance = newChildrenInstanceArray[index];
338
- // 确保链的干净
339
- instance.parent = parentInstance;
340
- if (index === 0)
341
- parentInstance.child = instance;
342
- if (index > 0)
343
- newChildrenInstanceArray[index - 1].sibling = instance;
344
- if (index === newChildrenInstanceArray.length - 1)
345
- delete instance.sibling;
346
- }
347
- // 剩余的是需要移除的
348
- if (childrenInstanceMap.size > 0) {
349
- if (parentInstance.deletions == null) {
350
- parentInstance.deletions = new Set();
351
- }
352
- childrenInstanceMap.forEach(instance => {
353
- delete instance.parent;
354
- delete instance.sibling;
355
- parentInstance.deletions?.add(instance);
356
- });
357
- }
358
- logger.debug('parentInstance', parentInstance);
359
- }
360
- /**
361
- * 处理单个 instance
362
- * @param instance 当前处理的实例
363
- * @returns 返回下一个需要处理的 instance
364
- */
365
- function performUnitOfWork(instance) {
366
- const element = instance.element;
367
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
368
- function childrenAsElements(children) {
369
- const childrenAsArray = Array.isArray(children) ? children : [children];
370
- function isComment(element) {
371
- if (element === '')
372
- return true;
373
- if (element == null)
374
- return true;
375
- if (element === false)
376
- return true;
377
- return false;
378
- }
379
- return childrenAsArray.flat(3).map(element => {
380
- if (isValidElement(element))
381
- return element;
382
- const elementType = isComment(element) ? 'comment' : 'text';
383
- const textContent = element === '' ? 'empty-string' : String(element);
384
- return createElement(elementType, { textContent });
385
- });
386
- }
387
- // airx 组件
388
- if (typeof element?.type === 'function') {
389
- const collector = createCollector();
390
- if (instance.render == null) {
391
- const component = element.type;
392
- const beforeContext = globalContext.current;
393
- globalContext.current = instance.context.getSafeContext();
394
- instance.render = collector.collect(() => component(instance.memoProps));
395
- globalContext.current = beforeContext;
396
- const children = collector.collect(() => instance.render?.());
397
- reconcileChildren(instance, childrenAsElements(children));
398
- }
399
- if (instance.requiredUpdate) {
400
- const children = collector.collect(() => instance.render?.());
401
- reconcileChildren(instance, childrenAsElements(children));
402
- delete instance.requiredUpdate;
403
- }
404
- // 处理依赖触发的更新
405
- collector.complete().forEach(ref => {
406
- instance.context.addDisposer(watch(ref, () => {
407
- instance.requiredUpdate = true;
408
- if (context.nextUnitOfWork == null && context.rootInstance.child) {
409
- context.nextUnitOfWork = context.rootInstance.child;
410
- }
411
- }));
412
- });
413
- }
414
- // 浏览器组件/标签
415
- if (typeof element?.type === 'string') {
416
- if ('children' in element.props && Array.isArray(element.props.children)) {
417
- reconcileChildren(instance, childrenAsElements(element.props.children));
418
- }
419
- }
420
- // 优先处理 child
421
- if (instance.child) {
422
- return instance.child;
423
- }
424
- /**
425
- * 递归向上查找可用的兄弟 instance
426
- * @param instance
427
- * @returns 返回下一个需要处理的兄弟 instance
428
- */
429
- function returnSibling(instance) {
430
- if (instance.sibling) {
431
- return instance.sibling;
432
- }
433
- if (instance.parent) {
434
- return returnSibling(instance.parent);
435
- }
436
- return null;
437
- }
438
- return returnSibling(instance);
439
- }
440
- /**
441
- * 提交 Dom 变化
442
- */
443
- function commitDom(rootInstance, rootNode) {
444
- const logger = createLogger('commitDom');
445
- logger.debug('commitDom', rootInstance);
446
- function updateDomProperties(dom, nextProps, prevProps = {}) {
447
- const isKey = (key) => key === 'key';
448
- const isStyle = (key) => key === 'style';
449
- const isClass = (key) => key === 'class';
450
- const isEvent = (key) => key.startsWith("on");
451
- const isChildren = (key) => key === 'children';
452
- const isGone = (_prev, next) => (key) => !(key in next);
453
- const isNew = (prev, next) => (key) => prev[key] !== next[key];
454
- const isProperty = (key) => !isChildren(key) && !isEvent(key) && !isStyle(key) && !isClass(key) && !isKey(key);
455
- // https://developer.mozilla.org/zh-CN/docs/Web/API/Node
456
- if (dom.nodeName === '#text' || dom.nodeName === '#comment') {
457
- const textNode = dom;
458
- if (textNode.nodeValue !== nextProps.textContent) {
459
- textNode.nodeValue = String(nextProps.textContent);
460
- }
461
- return;
462
- }
463
- // remove old style
464
- const oldStyle = prevProps?.style;
465
- if (typeof oldStyle === 'object' && oldStyle != null) {
466
- Object.keys(oldStyle).forEach(key => {
467
- /* eslint-disable @typescript-eslint/no-explicit-any */
468
- delete dom.style[key];
469
- /* eslint-enable @typescript-eslint/no-explicit-any */
470
- });
471
- }
472
- // add new style
473
- const newStyle = nextProps?.style;
474
- if (typeof newStyle === 'object' && newStyle != null) {
475
- Object.keys(newStyle).forEach((key) => {
476
- /* eslint-disable @typescript-eslint/no-explicit-any */
477
- const value = newStyle?.[key];
478
- dom.style[key] = value == null ? '' : value;
479
- /* eslint-enable @typescript-eslint/no-explicit-any */
480
- });
481
- }
482
- if (dom.className !== nextProps?.class) {
483
- if (!nextProps?.class) {
484
- dom.removeAttribute('class');
485
- }
486
- else if (typeof nextProps?.class === 'string') {
487
- dom.setAttribute('class', nextProps?.class);
488
- }
489
- }
490
- //Remove old or changed event listeners
491
- Object.keys(prevProps)
492
- .filter(isEvent)
493
- .filter(key => !(key in nextProps) ||
494
- isNew(prevProps, nextProps)(key))
495
- .forEach(name => {
496
- const eventType = name
497
- .toLowerCase()
498
- .substring(2);
499
- if (prevProps[name] == null)
500
- return;
501
- if (typeof prevProps[name] !== 'function') {
502
- console.error('EventListener is not a function');
503
- }
504
- else {
505
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
506
- dom.removeEventListener(eventType, prevProps[name]);
507
- }
508
- });
509
- // Add event listeners
510
- Object.keys(nextProps)
511
- .filter(isEvent)
512
- .filter(isNew(prevProps, nextProps))
513
- .forEach(name => {
514
- const eventType = name
515
- .toLowerCase()
516
- .substring(2);
517
- if (nextProps[name] == null)
518
- return;
519
- if (typeof nextProps[name] !== 'function') {
520
- console.error('EventListener is not a function');
521
- }
522
- else {
523
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
524
- dom.addEventListener(eventType, nextProps[name]);
525
- }
526
- });
527
- // Remove old properties
528
- Object.keys(prevProps)
529
- .filter(isProperty)
530
- .filter(isGone(prevProps, nextProps))
531
- .forEach(name => dom.setAttribute(name, ''));
532
- // Set new or changed properties
533
- Object.keys(nextProps)
534
- .filter(isProperty)
535
- .filter(isNew(prevProps, nextProps))
536
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
537
- .forEach(name => dom.setAttribute(name, nextProps[name]));
538
- }
539
- function getParentDom(instance) {
540
- if (instance.parent?.domRef != null) {
541
- return instance.parent.domRef;
542
- }
543
- if (instance.parent) {
544
- return getParentDom(instance.parent);
545
- }
546
- throw new Error('Cant find dom');
547
- }
548
- function getChildDom(instance) {
549
- if (instance.child?.domRef != null) {
550
- return instance.child.domRef;
551
- }
552
- if (instance.child) {
553
- return getChildDom(instance.child);
554
- }
555
- return null;
556
- }
557
- function commitInstanceDom(nextInstance, oldNode) {
558
- // 移除标删元素
559
- if (nextInstance.deletions) {
560
- for (const deletion of nextInstance.deletions) {
561
- const dom = deletion.domRef || getChildDom(deletion);
562
- if (dom && dom.parentNode)
563
- dom.parentNode.removeChild(dom);
564
- deletion.context.triggerUnmounted();
565
- }
566
- nextInstance.deletions.clear();
567
- }
568
- // 创建 dom
569
- if (nextInstance.domRef == null) {
570
- if (nextInstance.element == null)
571
- throw new Error('???');
572
- if (typeof nextInstance.element.type === 'string') {
573
- if (nextInstance.element.type === 'text') {
574
- const textContent = nextInstance.element.props.textContent;
575
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
576
- nextInstance.domRef = document.createTextNode(textContent);
577
- }
578
- else if (nextInstance.element.type === 'comment') {
579
- const textContent = nextInstance.element.props.textContent;
580
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
581
- nextInstance.domRef = document.createComment(textContent);
582
- }
583
- else {
584
- nextInstance.domRef = document.createElement(nextInstance.element.type);
585
- }
586
- }
587
- }
588
- // 更新属性
589
- if (nextInstance.domRef != null && nextInstance.element != null) {
590
- updateDomProperties(nextInstance.domRef, nextInstance.element.props, nextInstance.beforeElement?.props);
591
- }
592
- // 插入 parent
593
- // TODO: 针对仅移动时优化
594
- if (nextInstance.domRef != null) {
595
- if (oldNode !== nextInstance.domRef) {
596
- if (nextInstance.domRef.parentNode) {
597
- nextInstance.domRef.parentNode.removeChild(nextInstance.domRef);
598
- }
599
- const parentDom = getParentDom(nextInstance);
600
- parentDom.appendChild(nextInstance.domRef);
601
- }
602
- }
603
- }
604
- function commitWalkV2(initInstance, initNode) {
605
- // 创建一个栈,将根节点压入栈中
606
- const stack = [[initInstance, initNode]];
607
- while (stack.length > 0) {
608
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
609
- const stackLayer = stack.pop();
610
- if (typeof stackLayer === 'function') {
611
- stackLayer();
612
- continue;
613
- }
614
- const [instance, node] = stackLayer;
615
- commitInstanceDom(instance, node);
616
- // stack 是先入后出
617
- // 实际上是先渲染 child
618
- // 这里然后再渲染 sibling
619
- // 执行生命周期的 Mount
620
- stack.push(() => instance.context.triggerMounted());
621
- // 更新下一个兄弟节点
622
- if (instance.sibling != null) {
623
- const siblingNode = instance.domRef
624
- ? instance.domRef.nextSibling
625
- : node?.nextSibling;
626
- stack.push([instance.sibling, siblingNode || undefined]);
627
- }
628
- // 更新下一个子节点
629
- if (instance.child != null) {
630
- const childNode = instance.domRef
631
- ? instance.domRef.firstChild
632
- : node;
633
- stack.push([instance.child, childNode || undefined]);
634
- }
635
- }
636
- }
637
- commitWalkV2(rootInstance, rootNode);
638
- }
639
- /**
640
- * 调度
641
- */
642
- function workLoop(deadline) {
643
- let shouldYield = false;
644
- const logger = createLogger('workLoop');
645
- while (context.nextUnitOfWork && !shouldYield) {
646
- logger.debug('nextUnitOfWork', context.nextUnitOfWork);
647
- context.nextUnitOfWork = performUnitOfWork(context.nextUnitOfWork);
648
- if (context.nextUnitOfWork == null)
649
- context.needCommitDom = true;
650
- if (deadline)
651
- shouldYield = deadline.timeRemaining() < 1;
652
- }
653
- if (context.needCommitDom && context.rootInstance.child) {
654
- commitDom(context.rootInstance.child, context.rootInstance.domRef?.firstChild || undefined);
655
- context.needCommitDom = false;
656
- }
657
- requestIdleCallback(workLoop);
658
- }
659
- // 开始调度
660
- requestIdleCallback(workLoop);
661
- return context.rootInstance;
662
- }
663
- //# sourceMappingURL=render.js.map