@unsetsoft/ryunixjs 0.2.27 → 0.2.28-nightly.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.
- package/lib/dom.js +56 -46
- package/package.json +1 -1
package/lib/dom.js
CHANGED
|
@@ -1,3 +1,42 @@
|
|
|
1
|
+
let containerRoot = null;
|
|
2
|
+
let nextUnitOfWork = null;
|
|
3
|
+
let currentRoot = null;
|
|
4
|
+
let wipRoot = null;
|
|
5
|
+
let deletions = null;
|
|
6
|
+
let wipFiber = null;
|
|
7
|
+
let hookIndex = null;
|
|
8
|
+
|
|
9
|
+
const RYUNIX_TYPES = {
|
|
10
|
+
TEXT_ELEMENT: Symbol("text.element"),
|
|
11
|
+
RYUNIX_EFFECT: Symbol("ryunix.effect"),
|
|
12
|
+
RYUNIX_CONTEXT: Symbol("ryunix.context"),
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const STRINGS = {
|
|
16
|
+
object: "object",
|
|
17
|
+
function: "function",
|
|
18
|
+
style: "style",
|
|
19
|
+
className: "className",
|
|
20
|
+
children: "children",
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const EFFECT_TAGS = {
|
|
24
|
+
PLACEMENT: Symbol(),
|
|
25
|
+
UPDATE: Symbol(),
|
|
26
|
+
DELETION: Symbol(),
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const isEvent = (key) => key.startsWith("on");
|
|
30
|
+
const isProperty = (key) => key !== STRINGS.children && !isEvent(key);
|
|
31
|
+
const isNew = (prev, next) => (key) => prev[key] !== next[key];
|
|
32
|
+
const isGone = (next) => (key) => !(key in next);
|
|
33
|
+
const reg = /[A-Z]/g;
|
|
34
|
+
const hasDepsChanged = (prevDeps, nextDeps) =>
|
|
35
|
+
!prevDeps ||
|
|
36
|
+
!nextDeps ||
|
|
37
|
+
prevDeps.length !== nextDeps.length ||
|
|
38
|
+
prevDeps.some((dep, index) => dep !== nextDeps[index]);
|
|
39
|
+
|
|
1
40
|
/**
|
|
2
41
|
* The function creates a new element with the given type, props, and children.
|
|
3
42
|
* @param type - The type of the element to be created, such as "div", "span", "h1", etc.
|
|
@@ -22,7 +61,7 @@ function createElement(type, props, ...children) {
|
|
|
22
61
|
children: children
|
|
23
62
|
.flat()
|
|
24
63
|
.map((child) =>
|
|
25
|
-
typeof child ===
|
|
64
|
+
typeof child === STRINGS.object ? child : createTextElement(child)
|
|
26
65
|
),
|
|
27
66
|
},
|
|
28
67
|
};
|
|
@@ -36,7 +75,7 @@ function createElement(type, props, ...children) {
|
|
|
36
75
|
*/
|
|
37
76
|
function createTextElement(text) {
|
|
38
77
|
return {
|
|
39
|
-
type:
|
|
78
|
+
type: RYUNIX_TYPES.TEXT_ELEMENT,
|
|
40
79
|
props: {
|
|
41
80
|
nodeValue: text,
|
|
42
81
|
children: [],
|
|
@@ -56,7 +95,7 @@ function createTextElement(text) {
|
|
|
56
95
|
*/
|
|
57
96
|
function createDom(fiber) {
|
|
58
97
|
const dom =
|
|
59
|
-
fiber.type ==
|
|
98
|
+
fiber.type == RYUNIX_TYPES.TEXT_ELEMENT
|
|
60
99
|
? document.createTextNode("")
|
|
61
100
|
: document.createElement(fiber.type);
|
|
62
101
|
|
|
@@ -65,11 +104,6 @@ function createDom(fiber) {
|
|
|
65
104
|
return dom;
|
|
66
105
|
}
|
|
67
106
|
|
|
68
|
-
const isEvent = (key) => key.startsWith("on");
|
|
69
|
-
const isProperty = (key) => key !== "children" && !isEvent(key);
|
|
70
|
-
const isNew = (prev, next) => (key) => prev[key] !== next[key];
|
|
71
|
-
const isGone = (next) => (key) => !(key in next);
|
|
72
|
-
|
|
73
107
|
/**
|
|
74
108
|
* The function updates the DOM by removing old event listeners and properties, and adding new ones
|
|
75
109
|
* based on the previous and next props.
|
|
@@ -97,9 +131,9 @@ function updateDom(dom, prevProps, nextProps) {
|
|
|
97
131
|
.filter(isProperty)
|
|
98
132
|
.filter(isNew(prevProps, nextProps))
|
|
99
133
|
.forEach((name) => {
|
|
100
|
-
if (name ===
|
|
134
|
+
if (name === STRINGS.style) {
|
|
101
135
|
DomStyle(dom, nextProps.style);
|
|
102
|
-
} else if (name ===
|
|
136
|
+
} else if (name === STRINGS.className) {
|
|
103
137
|
if (nextProps.className === "") {
|
|
104
138
|
throw new Error("className cannot be empty.");
|
|
105
139
|
}
|
|
@@ -120,7 +154,6 @@ function updateDom(dom, prevProps, nextProps) {
|
|
|
120
154
|
});
|
|
121
155
|
}
|
|
122
156
|
|
|
123
|
-
const reg = /[A-Z]/g;
|
|
124
157
|
function DomStyle(dom, style) {
|
|
125
158
|
dom.style = Object.keys(style).reduce((acc, styleName) => {
|
|
126
159
|
const key = styleName.replace(reg, function (v) {
|
|
@@ -151,7 +184,7 @@ function commitRoot() {
|
|
|
151
184
|
function cancelEffects(fiber) {
|
|
152
185
|
if (fiber.hooks) {
|
|
153
186
|
fiber.hooks
|
|
154
|
-
.filter((hook) => hook.tag ===
|
|
187
|
+
.filter((hook) => hook.tag === RYUNIX_TYPES.RYUNIX_EFFECT && hook.cancel)
|
|
155
188
|
.forEach((effectHook) => {
|
|
156
189
|
effectHook.cancel();
|
|
157
190
|
});
|
|
@@ -168,7 +201,7 @@ function cancelEffects(fiber) {
|
|
|
168
201
|
function runEffects(fiber) {
|
|
169
202
|
if (fiber.hooks) {
|
|
170
203
|
fiber.hooks
|
|
171
|
-
.filter((hook) => hook.tag ===
|
|
204
|
+
.filter((hook) => hook.tag === RYUNIX_TYPES.RYUNIX_EFFECT && hook.effect)
|
|
172
205
|
.forEach((effectHook) => {
|
|
173
206
|
effectHook.cancel = effectHook.effect();
|
|
174
207
|
});
|
|
@@ -193,18 +226,18 @@ function commitWork(fiber) {
|
|
|
193
226
|
}
|
|
194
227
|
const domParent = domParentFiber.dom;
|
|
195
228
|
|
|
196
|
-
if (fiber.effectTag ===
|
|
229
|
+
if (fiber.effectTag === EFFECT_TAGS.PLACEMENT) {
|
|
197
230
|
if (fiber.dom != null) {
|
|
198
231
|
domParent.appendChild(fiber.dom);
|
|
199
232
|
}
|
|
200
233
|
runEffects(fiber);
|
|
201
|
-
} else if (fiber.effectTag ===
|
|
234
|
+
} else if (fiber.effectTag === EFFECT_TAGS.UPDATE) {
|
|
202
235
|
cancelEffects(fiber);
|
|
203
236
|
if (fiber.dom != null) {
|
|
204
237
|
updateDom(fiber.dom, fiber.alternate.props, fiber.props);
|
|
205
238
|
}
|
|
206
239
|
runEffects(fiber);
|
|
207
|
-
} else if (fiber.effectTag ===
|
|
240
|
+
} else if (fiber.effectTag === EFFECT_TAGS.DELETION) {
|
|
208
241
|
cancelEffects(fiber);
|
|
209
242
|
commitDeletion(fiber, domParent);
|
|
210
243
|
return;
|
|
@@ -229,8 +262,6 @@ function commitDeletion(fiber, domParent) {
|
|
|
229
262
|
}
|
|
230
263
|
}
|
|
231
264
|
|
|
232
|
-
let containerRoot = null;
|
|
233
|
-
|
|
234
265
|
/**
|
|
235
266
|
* @deprecated use Ryunix.init(root) instead.
|
|
236
267
|
*
|
|
@@ -275,11 +306,6 @@ function render(element, container) {
|
|
|
275
306
|
nextUnitOfWork = wipRoot;
|
|
276
307
|
}
|
|
277
308
|
|
|
278
|
-
let nextUnitOfWork = null;
|
|
279
|
-
let currentRoot = null;
|
|
280
|
-
let wipRoot = null;
|
|
281
|
-
let deletions = null;
|
|
282
|
-
|
|
283
309
|
/**
|
|
284
310
|
* This function uses requestIdleCallback to perform work on a fiber tree until it is complete or the
|
|
285
311
|
* browser needs to yield to other tasks.
|
|
@@ -335,9 +361,6 @@ function performUnitOfWork(fiber) {
|
|
|
335
361
|
}
|
|
336
362
|
}
|
|
337
363
|
|
|
338
|
-
let wipFiber = null;
|
|
339
|
-
let hookIndex = null;
|
|
340
|
-
|
|
341
364
|
/**
|
|
342
365
|
* This function updates a function component by setting up a work-in-progress fiber, resetting the
|
|
343
366
|
* hook index, creating an empty hooks array, rendering the component, and reconciling its children.
|
|
@@ -393,7 +416,7 @@ function reconcileChildren(wipFiber, elements) {
|
|
|
393
416
|
dom: oldFiber.dom,
|
|
394
417
|
parent: wipFiber,
|
|
395
418
|
alternate: oldFiber,
|
|
396
|
-
effectTag:
|
|
419
|
+
effectTag: EFFECT_TAGS.UPDATE,
|
|
397
420
|
};
|
|
398
421
|
}
|
|
399
422
|
if (element && !sameType) {
|
|
@@ -403,11 +426,11 @@ function reconcileChildren(wipFiber, elements) {
|
|
|
403
426
|
dom: null,
|
|
404
427
|
parent: wipFiber,
|
|
405
428
|
alternate: null,
|
|
406
|
-
effectTag:
|
|
429
|
+
effectTag: EFFECT_TAGS.PLACEMENT,
|
|
407
430
|
};
|
|
408
431
|
}
|
|
409
432
|
if (oldFiber && !sameType) {
|
|
410
|
-
oldFiber.effectTag =
|
|
433
|
+
oldFiber.effectTag = EFFECT_TAGS.DELETION;
|
|
411
434
|
deletions.push(oldFiber);
|
|
412
435
|
}
|
|
413
436
|
|
|
@@ -437,7 +460,7 @@ function createContext(defaultValue) {
|
|
|
437
460
|
let contextValue = defaultValue || null;
|
|
438
461
|
|
|
439
462
|
const context = {
|
|
440
|
-
tag:
|
|
463
|
+
tag: RYUNIX_TYPES.RYUNIX_CONTEXT,
|
|
441
464
|
Value: contextValue,
|
|
442
465
|
Provider: null,
|
|
443
466
|
};
|
|
@@ -501,7 +524,8 @@ function useStore(initial) {
|
|
|
501
524
|
|
|
502
525
|
const actions = oldHook ? oldHook.queue : [];
|
|
503
526
|
actions.forEach((action) => {
|
|
504
|
-
hook.state =
|
|
527
|
+
hook.state =
|
|
528
|
+
typeof action === STRINGS.function ? action(hook.state) : action;
|
|
505
529
|
});
|
|
506
530
|
|
|
507
531
|
/**
|
|
@@ -527,20 +551,6 @@ function useStore(initial) {
|
|
|
527
551
|
return [hook.state, setState];
|
|
528
552
|
}
|
|
529
553
|
|
|
530
|
-
/**
|
|
531
|
-
* The function checks if the previous dependencies are different from the next dependencies.
|
|
532
|
-
* @param prevDeps - The previous dependencies, which could be an array of values or objects that a
|
|
533
|
-
* function or component depends on.
|
|
534
|
-
* @param nextDeps - `nextDeps` is an array of dependencies that are being checked for changes. These
|
|
535
|
-
* dependencies are typically used in React's `useEffect` and `useCallback` hooks to determine when a
|
|
536
|
-
* component should re-render or when a function should be re-created.
|
|
537
|
-
*/
|
|
538
|
-
const hasDepsChanged = (prevDeps, nextDeps) =>
|
|
539
|
-
!prevDeps ||
|
|
540
|
-
!nextDeps ||
|
|
541
|
-
prevDeps.length !== nextDeps.length ||
|
|
542
|
-
prevDeps.some((dep, index) => dep !== nextDeps[index]);
|
|
543
|
-
|
|
544
554
|
/**
|
|
545
555
|
* This is a function that creates a hook for managing side effects in Ryunix components.
|
|
546
556
|
* @param effect - The effect function that will be executed after the component has rendered or when
|
|
@@ -559,7 +569,7 @@ function useEffect(effect, deps) {
|
|
|
559
569
|
const hasChanged = hasDepsChanged(oldHook ? oldHook.deps : undefined, deps);
|
|
560
570
|
|
|
561
571
|
const hook = {
|
|
562
|
-
tag:
|
|
572
|
+
tag: RYUNIX_TYPES.RYUNIX_EFFECT,
|
|
563
573
|
effect: hasChanged ? effect : null,
|
|
564
574
|
cancel: hasChanged && oldHook && oldHook.cancel,
|
|
565
575
|
deps,
|