@unsetsoft/ryunixjs 1.2.3-canary.6 → 1.2.3-canary.8
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/dist/Ryunix.esm.js +1485 -0
- package/dist/Ryunix.esm.js.map +1 -0
- package/dist/Ryunix.js +475 -464
- package/dist/Ryunix.min.js +928 -1
- package/dist/Ryunix.umd.js +1510 -0
- package/dist/Ryunix.umd.js.map +1 -0
- package/dist/Ryunix.umd.min.js +2 -0
- package/dist/Ryunix.umd.min.js.map +1 -0
- package/package.json +8 -7
|
@@ -0,0 +1,1485 @@
|
|
|
1
|
+
// Improved state management - avoid global mutable object
|
|
2
|
+
// Instead, create a state manager that can be instantiated per render tree
|
|
3
|
+
|
|
4
|
+
const createRenderState = () => ({
|
|
5
|
+
containerRoot: null,
|
|
6
|
+
nextUnitOfWork: null,
|
|
7
|
+
currentRoot: null,
|
|
8
|
+
wipRoot: null,
|
|
9
|
+
deletions: [],
|
|
10
|
+
wipFiber: null,
|
|
11
|
+
hookIndex: 0,
|
|
12
|
+
effects: [],
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
// Singleton for backward compatibility, but allows testing with isolated instances
|
|
16
|
+
let globalState = createRenderState();
|
|
17
|
+
|
|
18
|
+
const getState = () => globalState;
|
|
19
|
+
|
|
20
|
+
// Use const for regex to prevent accidental modification
|
|
21
|
+
const CAMEL_TO_KEBAB_REGEX = /[A-Z]/g;
|
|
22
|
+
|
|
23
|
+
const RYUNIX_TYPES = Object.freeze({
|
|
24
|
+
TEXT_ELEMENT: Symbol.for('ryunix.text.element'),
|
|
25
|
+
RYUNIX_ELEMENT: Symbol.for('ryunix.element'),
|
|
26
|
+
RYUNIX_EFFECT: Symbol.for('ryunix.effect'),
|
|
27
|
+
RYUNIX_MEMO: Symbol.for('ryunix.memo'),
|
|
28
|
+
RYUNIX_URL_QUERY: Symbol.for('ryunix.urlQuery'),
|
|
29
|
+
RYUNIX_REF: Symbol.for('ryunix.ref'),
|
|
30
|
+
RYUNIX_STORE: Symbol.for('ryunix.store'),
|
|
31
|
+
RYUNIX_REDUCE: Symbol.for('ryunix.reduce'),
|
|
32
|
+
RYUNIX_FRAGMENT: Symbol.for('ryunix.fragment'),
|
|
33
|
+
RYUNIX_CONTEXT: Symbol.for('ryunix.context'),
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const STRINGS = Object.freeze({
|
|
37
|
+
OBJECT: 'object',
|
|
38
|
+
FUNCTION: 'function',
|
|
39
|
+
STYLE: 'ryunix-style',
|
|
40
|
+
CLASS_NAME: 'ryunix-class',
|
|
41
|
+
CHILDREN: 'children',
|
|
42
|
+
BOOLEAN: 'boolean',
|
|
43
|
+
STRING: 'string',
|
|
44
|
+
UNDEFINED: 'undefined',
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const OLD_STRINGS = Object.freeze({
|
|
48
|
+
STYLE: 'style',
|
|
49
|
+
CLASS_NAME: 'className',
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const EFFECT_TAGS = Object.freeze({
|
|
53
|
+
PLACEMENT: Symbol.for('ryunix.reconciler.status.placement'),
|
|
54
|
+
UPDATE: Symbol.for('ryunix.reconciler.status.update'),
|
|
55
|
+
DELETION: Symbol.for('ryunix.reconciler.status.deletion'),
|
|
56
|
+
NO_EFFECT: Symbol.for('ryunix.reconciler.status.no_effect'),
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Type checking utilities
|
|
61
|
+
*/
|
|
62
|
+
const is = {
|
|
63
|
+
object: (val) => val !== null && typeof val === STRINGS.OBJECT,
|
|
64
|
+
function: (val) => typeof val === STRINGS.FUNCTION,
|
|
65
|
+
string: (val) => typeof val === STRINGS.STRING,
|
|
66
|
+
undefined: (val) => typeof val === STRINGS.UNDEFINED,
|
|
67
|
+
null: (val) => val === null,
|
|
68
|
+
array: (val) => Array.isArray(val),
|
|
69
|
+
promise: (val) => val instanceof Promise,
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Create text element
|
|
74
|
+
*/
|
|
75
|
+
const createTextElement = (text) => {
|
|
76
|
+
return {
|
|
77
|
+
type: RYUNIX_TYPES.TEXT_ELEMENT,
|
|
78
|
+
props: {
|
|
79
|
+
nodeValue: text,
|
|
80
|
+
children: [],
|
|
81
|
+
},
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Create element
|
|
87
|
+
*/
|
|
88
|
+
const createElement = (type, props, ...children) => {
|
|
89
|
+
const safeProps = props || {};
|
|
90
|
+
|
|
91
|
+
return {
|
|
92
|
+
type,
|
|
93
|
+
props: {
|
|
94
|
+
...safeProps,
|
|
95
|
+
children: children
|
|
96
|
+
.flat()
|
|
97
|
+
.map((child) =>
|
|
98
|
+
typeof child === STRINGS.OBJECT ? child : createTextElement(child),
|
|
99
|
+
),
|
|
100
|
+
},
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Fragment component
|
|
106
|
+
*/
|
|
107
|
+
const Fragment = (props) => {
|
|
108
|
+
const children = Array.isArray(props.children)
|
|
109
|
+
? props.children
|
|
110
|
+
: [props.children];
|
|
111
|
+
return createElement(RYUNIX_TYPES.RYUNIX_FRAGMENT, {}, ...children)
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Check if a key is an event handler
|
|
116
|
+
* @param {string} key - Prop key
|
|
117
|
+
* @returns {boolean}
|
|
118
|
+
*/
|
|
119
|
+
const isEvent = (key) => key.startsWith('on');
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Check if a key is a property (not children or event)
|
|
123
|
+
* @param {string} key - Prop key
|
|
124
|
+
* @returns {boolean}
|
|
125
|
+
*/
|
|
126
|
+
const isProperty = (key) => key !== STRINGS.CHILDREN && !isEvent(key);
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Check if a property is new or changed
|
|
130
|
+
* @param {Object} prev - Previous props
|
|
131
|
+
* @param {Object} next - Next props
|
|
132
|
+
* @returns {Function}
|
|
133
|
+
*/
|
|
134
|
+
const isNew = (prev, next) => (key) => {
|
|
135
|
+
// Use Object.is for better comparison (handles NaN, -0, +0)
|
|
136
|
+
return !Object.is(prev[key], next[key])
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Check if a property was removed
|
|
141
|
+
* @param {Object} next - Next props
|
|
142
|
+
* @returns {Function}
|
|
143
|
+
*/
|
|
144
|
+
const isGone = (next) => (key) => !(key in next);
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Cancel effects for a single fiber
|
|
148
|
+
* @param {Object} fiber - Fiber node
|
|
149
|
+
*/
|
|
150
|
+
const cancelEffects = (fiber) => {
|
|
151
|
+
if (!fiber?.hooks?.length) return
|
|
152
|
+
|
|
153
|
+
fiber.hooks
|
|
154
|
+
.filter(
|
|
155
|
+
(hook) =>
|
|
156
|
+
hook.type === RYUNIX_TYPES.RYUNIX_EFFECT && is.function(hook.cancel),
|
|
157
|
+
)
|
|
158
|
+
.forEach((hook) => {
|
|
159
|
+
try {
|
|
160
|
+
hook.cancel();
|
|
161
|
+
hook.cancel = null; // Clear reference to prevent memory leaks
|
|
162
|
+
} catch (error) {
|
|
163
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
164
|
+
console.error('Error in effect cleanup:', error);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Recursively cancel effects in fiber tree
|
|
172
|
+
* @param {Object} fiber - Root fiber node
|
|
173
|
+
*/
|
|
174
|
+
const cancelEffectsDeep = (fiber) => {
|
|
175
|
+
if (!fiber) return
|
|
176
|
+
|
|
177
|
+
// Cancel effects for current fiber
|
|
178
|
+
if (fiber.hooks?.length > 0) {
|
|
179
|
+
fiber.hooks
|
|
180
|
+
.filter(
|
|
181
|
+
(hook) =>
|
|
182
|
+
hook.type === RYUNIX_TYPES.RYUNIX_EFFECT && is.function(hook.cancel),
|
|
183
|
+
)
|
|
184
|
+
.forEach((hook) => {
|
|
185
|
+
try {
|
|
186
|
+
hook.cancel();
|
|
187
|
+
hook.cancel = null; // Clear reference
|
|
188
|
+
} catch (error) {
|
|
189
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
190
|
+
console.error('Error in deep effect cleanup:', error);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Recursively process children
|
|
197
|
+
if (fiber.child) cancelEffectsDeep(fiber.child);
|
|
198
|
+
if (fiber.sibling) cancelEffectsDeep(fiber.sibling);
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Run effects for a fiber
|
|
203
|
+
* @param {Object} fiber - Fiber node
|
|
204
|
+
*/
|
|
205
|
+
const runEffects = (fiber) => {
|
|
206
|
+
if (!fiber?.hooks?.length) return
|
|
207
|
+
|
|
208
|
+
for (let i = 0; i < fiber.hooks.length; i++) {
|
|
209
|
+
const hook = fiber.hooks[i];
|
|
210
|
+
|
|
211
|
+
if (hook.type === RYUNIX_TYPES.RYUNIX_EFFECT && is.function(hook.effect)) {
|
|
212
|
+
// Cancel previous cleanup if exists
|
|
213
|
+
if (is.function(hook.cancel)) {
|
|
214
|
+
try {
|
|
215
|
+
hook.cancel();
|
|
216
|
+
} catch (error) {
|
|
217
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
218
|
+
console.error('Error in effect cleanup:', error);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Run new effect
|
|
224
|
+
try {
|
|
225
|
+
const cleanup = hook.effect();
|
|
226
|
+
|
|
227
|
+
// Store cleanup function if returned
|
|
228
|
+
if (is.function(cleanup)) {
|
|
229
|
+
hook.cancel = cleanup;
|
|
230
|
+
} else {
|
|
231
|
+
hook.cancel = null;
|
|
232
|
+
}
|
|
233
|
+
} catch (error) {
|
|
234
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
235
|
+
console.error('Error in effect:', error);
|
|
236
|
+
}
|
|
237
|
+
hook.cancel = null;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// Clear effect reference after running
|
|
241
|
+
hook.effect = null;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Convert camelCase to kebab-case for CSS properties
|
|
248
|
+
* @param {string} camelCase - CamelCase string
|
|
249
|
+
* @returns {string} Kebab-case string
|
|
250
|
+
*/
|
|
251
|
+
const camelToKebab = (camelCase) => {
|
|
252
|
+
return camelCase.replace(
|
|
253
|
+
CAMEL_TO_KEBAB_REGEX,
|
|
254
|
+
(match) => `-${match.toLowerCase()}`,
|
|
255
|
+
)
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Apply styles to DOM element
|
|
260
|
+
* @param {HTMLElement} dom - DOM element
|
|
261
|
+
* @param {Object} styleObj - Style object
|
|
262
|
+
*/
|
|
263
|
+
const applyStyles = (dom, styleObj) => {
|
|
264
|
+
if (!is.object(styleObj) || is.null(styleObj)) {
|
|
265
|
+
dom.style.cssText = '';
|
|
266
|
+
return
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
try {
|
|
270
|
+
const cssText = Object.entries(styleObj)
|
|
271
|
+
.filter(([_, value]) => value != null) // Filter out null/undefined
|
|
272
|
+
.map(([key, value]) => {
|
|
273
|
+
const kebabKey = camelToKebab(key);
|
|
274
|
+
return `${kebabKey}: ${value}`
|
|
275
|
+
})
|
|
276
|
+
.join('; ');
|
|
277
|
+
|
|
278
|
+
dom.style.cssText = cssText;
|
|
279
|
+
} catch (error) {
|
|
280
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
281
|
+
console.error('Error applying styles:', error);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Apply CSS classes to DOM element
|
|
288
|
+
* @param {HTMLElement} dom - DOM element
|
|
289
|
+
* @param {string} prevClasses - Previous class string
|
|
290
|
+
* @param {string} nextClasses - Next class string
|
|
291
|
+
*/
|
|
292
|
+
const applyClasses = (dom, prevClasses, nextClasses) => {
|
|
293
|
+
// Allow empty/undefined - just remove classes
|
|
294
|
+
if (!nextClasses || nextClasses.trim() === '') {
|
|
295
|
+
if (prevClasses) {
|
|
296
|
+
const oldClasses = prevClasses.split(/\s+/).filter(Boolean);
|
|
297
|
+
dom.classList.remove(...oldClasses);
|
|
298
|
+
}
|
|
299
|
+
return
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// Remove old classes
|
|
303
|
+
if (prevClasses) {
|
|
304
|
+
const oldClasses = prevClasses.split(/\s+/).filter(Boolean);
|
|
305
|
+
dom.classList.remove(...oldClasses);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// Add new classes
|
|
309
|
+
const newClasses = nextClasses.split(/\s+/).filter(Boolean);
|
|
310
|
+
if (newClasses.length > 0) {
|
|
311
|
+
dom.classList.add(...newClasses);
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Create a DOM element from fiber
|
|
317
|
+
* @param {Object} fiber - Fiber node
|
|
318
|
+
* @returns {HTMLElement|Text|null}
|
|
319
|
+
*/
|
|
320
|
+
const createDom = (fiber) => {
|
|
321
|
+
// Fragments don't create real DOM nodes
|
|
322
|
+
if (fiber.type === RYUNIX_TYPES.RYUNIX_FRAGMENT) {
|
|
323
|
+
return null
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
let dom;
|
|
327
|
+
|
|
328
|
+
try {
|
|
329
|
+
if (fiber.type === RYUNIX_TYPES.TEXT_ELEMENT) {
|
|
330
|
+
dom = document.createTextNode('');
|
|
331
|
+
} else if (is.string(fiber.type)) {
|
|
332
|
+
dom = document.createElement(fiber.type);
|
|
333
|
+
} else {
|
|
334
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
335
|
+
console.warn(
|
|
336
|
+
'Attempted to create DOM for non-host component:',
|
|
337
|
+
fiber.type,
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
return null
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
updateDom(dom, {}, fiber.props);
|
|
344
|
+
return dom
|
|
345
|
+
} catch (error) {
|
|
346
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
347
|
+
console.error('Error creating DOM element:', error, fiber);
|
|
348
|
+
}
|
|
349
|
+
return null
|
|
350
|
+
}
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Update DOM element with new props
|
|
355
|
+
* @param {HTMLElement|Text} dom - DOM element
|
|
356
|
+
* @param {Object} prevProps - Previous props
|
|
357
|
+
* @param {Object} nextProps - Next props
|
|
358
|
+
*/
|
|
359
|
+
const updateDom = (dom, prevProps = {}, nextProps = {}) => {
|
|
360
|
+
// Remove old event listeners
|
|
361
|
+
Object.keys(prevProps)
|
|
362
|
+
.filter(isEvent)
|
|
363
|
+
.filter((key) => isGone(nextProps)(key) || isNew(prevProps, nextProps)(key))
|
|
364
|
+
.forEach((name) => {
|
|
365
|
+
const eventType = name.toLowerCase().substring(2);
|
|
366
|
+
try {
|
|
367
|
+
dom.removeEventListener(eventType, prevProps[name]);
|
|
368
|
+
} catch (error) {
|
|
369
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
370
|
+
console.warn('Error removing event listener:', error);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
// Remove old properties
|
|
376
|
+
Object.keys(prevProps)
|
|
377
|
+
.filter(isProperty)
|
|
378
|
+
.filter(isGone(nextProps))
|
|
379
|
+
.forEach((name) => {
|
|
380
|
+
// Skip special properties
|
|
381
|
+
if (
|
|
382
|
+
[
|
|
383
|
+
STRINGS.STYLE,
|
|
384
|
+
OLD_STRINGS.STYLE,
|
|
385
|
+
STRINGS.CLASS_NAME,
|
|
386
|
+
OLD_STRINGS.CLASS_NAME,
|
|
387
|
+
].includes(name)
|
|
388
|
+
) {
|
|
389
|
+
return
|
|
390
|
+
}
|
|
391
|
+
dom[name] = '';
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
// Set new properties
|
|
395
|
+
Object.keys(nextProps)
|
|
396
|
+
.filter(isProperty)
|
|
397
|
+
.filter(isNew(prevProps, nextProps))
|
|
398
|
+
.forEach((name) => {
|
|
399
|
+
try {
|
|
400
|
+
// Handle style properties
|
|
401
|
+
if (name === STRINGS.STYLE || name === OLD_STRINGS.STYLE) {
|
|
402
|
+
const styleValue = nextProps[name];
|
|
403
|
+
applyStyles(dom, styleValue);
|
|
404
|
+
}
|
|
405
|
+
// Handle className properties
|
|
406
|
+
else if (name === STRINGS.CLASS_NAME) {
|
|
407
|
+
applyClasses(
|
|
408
|
+
dom,
|
|
409
|
+
prevProps[STRINGS.CLASS_NAME],
|
|
410
|
+
nextProps[STRINGS.CLASS_NAME],
|
|
411
|
+
);
|
|
412
|
+
} else if (name === OLD_STRINGS.CLASS_NAME) {
|
|
413
|
+
applyClasses(
|
|
414
|
+
dom,
|
|
415
|
+
prevProps[OLD_STRINGS.CLASS_NAME],
|
|
416
|
+
nextProps[OLD_STRINGS.CLASS_NAME],
|
|
417
|
+
);
|
|
418
|
+
}
|
|
419
|
+
// Handle other properties
|
|
420
|
+
else {
|
|
421
|
+
// Special handling for value and checked (controlled components)
|
|
422
|
+
if (name === 'value' || name === 'checked') {
|
|
423
|
+
if (dom[name] !== nextProps[name]) {
|
|
424
|
+
dom[name] = nextProps[name];
|
|
425
|
+
}
|
|
426
|
+
} else {
|
|
427
|
+
dom[name] = nextProps[name];
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
} catch (error) {
|
|
431
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
432
|
+
console.warn(`Error setting property ${name}:`, error);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
// Add new event listeners
|
|
438
|
+
Object.keys(nextProps)
|
|
439
|
+
.filter(isEvent)
|
|
440
|
+
.filter(isNew(prevProps, nextProps))
|
|
441
|
+
.forEach((name) => {
|
|
442
|
+
const eventType = name.toLowerCase().substring(2);
|
|
443
|
+
try {
|
|
444
|
+
dom.addEventListener(eventType, nextProps[name]);
|
|
445
|
+
} catch (error) {
|
|
446
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
447
|
+
console.warn('Error adding event listener:', error);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
});
|
|
451
|
+
};
|
|
452
|
+
|
|
453
|
+
const commitRoot = () => {
|
|
454
|
+
const state = getState();
|
|
455
|
+
state.deletions.forEach(commitWork);
|
|
456
|
+
commitWork(state.wipRoot.child);
|
|
457
|
+
state.currentRoot = state.wipRoot;
|
|
458
|
+
state.wipRoot = null;
|
|
459
|
+
};
|
|
460
|
+
|
|
461
|
+
const commitWork = (fiber) => {
|
|
462
|
+
if (!fiber) {
|
|
463
|
+
return
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
let domParentFiber = fiber.parent;
|
|
467
|
+
while (!domParentFiber.dom) {
|
|
468
|
+
domParentFiber = domParentFiber.parent;
|
|
469
|
+
}
|
|
470
|
+
const domParent = domParentFiber.dom;
|
|
471
|
+
|
|
472
|
+
if (fiber.effectTag === EFFECT_TAGS.PLACEMENT) {
|
|
473
|
+
if (fiber.dom != null) {
|
|
474
|
+
domParent.appendChild(fiber.dom);
|
|
475
|
+
}
|
|
476
|
+
runEffects(fiber);
|
|
477
|
+
} else if (fiber.effectTag === EFFECT_TAGS.UPDATE) {
|
|
478
|
+
cancelEffects(fiber);
|
|
479
|
+
if (fiber.dom != null) {
|
|
480
|
+
updateDom(fiber.dom, fiber.alternate.props, fiber.props);
|
|
481
|
+
}
|
|
482
|
+
runEffects(fiber);
|
|
483
|
+
} else if (fiber.effectTag === EFFECT_TAGS.DELETION) {
|
|
484
|
+
cancelEffectsDeep(fiber);
|
|
485
|
+
commitDeletion(fiber, domParent);
|
|
486
|
+
return
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
commitWork(fiber.child);
|
|
490
|
+
commitWork(fiber.sibling);
|
|
491
|
+
};
|
|
492
|
+
|
|
493
|
+
const commitDeletion = (fiber, domParent) => {
|
|
494
|
+
if (fiber.dom) {
|
|
495
|
+
domParent.removeChild(fiber.dom);
|
|
496
|
+
} else {
|
|
497
|
+
let child = fiber.child;
|
|
498
|
+
while (child) {
|
|
499
|
+
commitDeletion(child, domParent);
|
|
500
|
+
child = child.sibling;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
};
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* Reconcile children with key optimization
|
|
507
|
+
*/
|
|
508
|
+
const reconcileChildren = (wipFiber, elements) => {
|
|
509
|
+
const state = getState();
|
|
510
|
+
let index = 0;
|
|
511
|
+
let prevSibling;
|
|
512
|
+
|
|
513
|
+
// Build map of old fibers by key/index
|
|
514
|
+
const oldFiberMap = new Map();
|
|
515
|
+
let oldFiber = wipFiber.alternate?.child;
|
|
516
|
+
let position = 0;
|
|
517
|
+
|
|
518
|
+
while (oldFiber) {
|
|
519
|
+
const key = oldFiber.key ?? `__index_${position}__`;
|
|
520
|
+
oldFiberMap.set(key, oldFiber);
|
|
521
|
+
oldFiber = oldFiber.sibling;
|
|
522
|
+
position++;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
// Process new elements
|
|
526
|
+
while (index < elements.length) {
|
|
527
|
+
const element = elements[index];
|
|
528
|
+
if (!element) {
|
|
529
|
+
index++;
|
|
530
|
+
continue
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
const key = element.key ?? `__index_${index}__`;
|
|
534
|
+
const matchedFiber = oldFiberMap.get(key);
|
|
535
|
+
|
|
536
|
+
let newFiber;
|
|
537
|
+
const sameType = matchedFiber && element.type === matchedFiber.type;
|
|
538
|
+
|
|
539
|
+
if (sameType) {
|
|
540
|
+
// Update existing fiber
|
|
541
|
+
newFiber = {
|
|
542
|
+
type: matchedFiber.type,
|
|
543
|
+
props: element.props,
|
|
544
|
+
dom: matchedFiber.dom,
|
|
545
|
+
parent: wipFiber,
|
|
546
|
+
alternate: matchedFiber,
|
|
547
|
+
effectTag: EFFECT_TAGS.UPDATE,
|
|
548
|
+
hooks: matchedFiber.hooks,
|
|
549
|
+
key: element.key,
|
|
550
|
+
};
|
|
551
|
+
oldFiberMap.delete(key);
|
|
552
|
+
} else {
|
|
553
|
+
// Create new fiber
|
|
554
|
+
newFiber = {
|
|
555
|
+
type: element.type,
|
|
556
|
+
props: element.props,
|
|
557
|
+
dom: null,
|
|
558
|
+
parent: wipFiber,
|
|
559
|
+
alternate: null,
|
|
560
|
+
effectTag: EFFECT_TAGS.PLACEMENT,
|
|
561
|
+
key: element.key,
|
|
562
|
+
};
|
|
563
|
+
|
|
564
|
+
// Mark matched fiber for deletion if exists
|
|
565
|
+
if (matchedFiber) {
|
|
566
|
+
matchedFiber.effectTag = EFFECT_TAGS.DELETION;
|
|
567
|
+
state.deletions.push(matchedFiber);
|
|
568
|
+
oldFiberMap.delete(key);
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
// Link fibers
|
|
573
|
+
if (index === 0) {
|
|
574
|
+
wipFiber.child = newFiber;
|
|
575
|
+
} else if (newFiber) {
|
|
576
|
+
prevSibling.sibling = newFiber;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
prevSibling = newFiber;
|
|
580
|
+
index++;
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
// Delete remaining old fibers
|
|
584
|
+
oldFiberMap.forEach((fiber) => {
|
|
585
|
+
fiber.effectTag = EFFECT_TAGS.DELETION;
|
|
586
|
+
state.deletions.push(fiber);
|
|
587
|
+
});
|
|
588
|
+
};
|
|
589
|
+
|
|
590
|
+
const updateFunctionComponent = (fiber) => {
|
|
591
|
+
const state = getState();
|
|
592
|
+
state.wipFiber = fiber;
|
|
593
|
+
state.hookIndex = 0;
|
|
594
|
+
state.wipFiber.hooks = [];
|
|
595
|
+
|
|
596
|
+
const children = [fiber.type(fiber.props)];
|
|
597
|
+
|
|
598
|
+
if (fiber.type._contextId && fiber.props.value !== undefined) {
|
|
599
|
+
fiber._contextId = fiber.type._contextId;
|
|
600
|
+
fiber._contextValue = fiber.props.value;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
reconcileChildren(fiber, children);
|
|
604
|
+
};
|
|
605
|
+
|
|
606
|
+
const updateHostComponent = (fiber) => {
|
|
607
|
+
if (!fiber.dom) {
|
|
608
|
+
fiber.dom = createDom(fiber);
|
|
609
|
+
}
|
|
610
|
+
const children = fiber.props?.children || [];
|
|
611
|
+
reconcileChildren(fiber, children);
|
|
612
|
+
};
|
|
613
|
+
|
|
614
|
+
const Image = ({ src, ...props }) => {
|
|
615
|
+
return createElement('img', { ...props, src })
|
|
616
|
+
};
|
|
617
|
+
|
|
618
|
+
/**
|
|
619
|
+
* Priority levels for updates
|
|
620
|
+
*/
|
|
621
|
+
const Priority = {
|
|
622
|
+
IMMEDIATE: 1, // User input (clicks, typing)
|
|
623
|
+
USER_BLOCKING: 2, // Hover, scroll
|
|
624
|
+
NORMAL: 3, // Data fetching
|
|
625
|
+
LOW: 4, // Analytics
|
|
626
|
+
IDLE: 5, // Background tasks
|
|
627
|
+
};
|
|
628
|
+
|
|
629
|
+
Priority.NORMAL;
|
|
630
|
+
|
|
631
|
+
/**
|
|
632
|
+
* Performance profiler for Ryunix
|
|
633
|
+
*/
|
|
634
|
+
class Profiler {
|
|
635
|
+
constructor() {
|
|
636
|
+
this.enabled = process.env.NODE_ENV !== 'production';
|
|
637
|
+
this.measures = new Map();
|
|
638
|
+
this.renderTimes = [];
|
|
639
|
+
this.maxSamples = 100;
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
startMeasure(name) {
|
|
643
|
+
if (!this.enabled) return
|
|
644
|
+
this.measures.set(name, performance.now());
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
endMeasure(name) {
|
|
648
|
+
if (!this.enabled) return
|
|
649
|
+
const start = this.measures.get(name);
|
|
650
|
+
if (!start) return
|
|
651
|
+
|
|
652
|
+
const duration = performance.now() - start;
|
|
653
|
+
this.measures.delete(name);
|
|
654
|
+
|
|
655
|
+
return duration
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
recordRender(componentName, duration) {
|
|
659
|
+
if (!this.enabled) return
|
|
660
|
+
|
|
661
|
+
this.renderTimes.push({
|
|
662
|
+
component: componentName,
|
|
663
|
+
duration,
|
|
664
|
+
timestamp: Date.now(),
|
|
665
|
+
});
|
|
666
|
+
|
|
667
|
+
if (this.renderTimes.length > this.maxSamples) {
|
|
668
|
+
this.renderTimes.shift();
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
getStats() {
|
|
673
|
+
if (!this.enabled) return null
|
|
674
|
+
|
|
675
|
+
const total = this.renderTimes.reduce((sum, r) => sum + r.duration, 0);
|
|
676
|
+
const avg = total / this.renderTimes.length;
|
|
677
|
+
const max = Math.max(...this.renderTimes.map((r) => r.duration));
|
|
678
|
+
const min = Math.min(...this.renderTimes.map((r) => r.duration));
|
|
679
|
+
|
|
680
|
+
return { total, avg, max, min, count: this.renderTimes.length }
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
getSlowestComponents(limit = 10) {
|
|
684
|
+
if (!this.enabled) return []
|
|
685
|
+
|
|
686
|
+
const byComponent = new Map();
|
|
687
|
+
|
|
688
|
+
this.renderTimes.forEach(({ component, duration }) => {
|
|
689
|
+
if (!byComponent.has(component)) {
|
|
690
|
+
byComponent.set(component, { total: 0, count: 0, max: 0 });
|
|
691
|
+
}
|
|
692
|
+
const stats = byComponent.get(component);
|
|
693
|
+
stats.total += duration;
|
|
694
|
+
stats.count++;
|
|
695
|
+
stats.max = Math.max(stats.max, duration);
|
|
696
|
+
});
|
|
697
|
+
|
|
698
|
+
return Array.from(byComponent.entries())
|
|
699
|
+
.map(([name, stats]) => ({
|
|
700
|
+
name,
|
|
701
|
+
avg: stats.total / stats.count,
|
|
702
|
+
max: stats.max,
|
|
703
|
+
count: stats.count,
|
|
704
|
+
}))
|
|
705
|
+
.sort((a, b) => b.avg - a.avg)
|
|
706
|
+
.slice(0, limit)
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
logStats() {
|
|
710
|
+
if (!this.enabled) return
|
|
711
|
+
|
|
712
|
+
const stats = this.getStats();
|
|
713
|
+
if (!stats) return
|
|
714
|
+
|
|
715
|
+
console.group('🔍 Ryunix Performance Stats');
|
|
716
|
+
console.log(`Total renders: ${stats.count}`);
|
|
717
|
+
console.log(`Avg render time: ${stats.avg.toFixed(2)}ms`);
|
|
718
|
+
console.log(
|
|
719
|
+
`Min: ${stats.min.toFixed(2)}ms | Max: ${stats.max.toFixed(2)}ms`,
|
|
720
|
+
);
|
|
721
|
+
|
|
722
|
+
const slowest = this.getSlowestComponents(5);
|
|
723
|
+
if (slowest.length > 0) {
|
|
724
|
+
console.log('\n⚠️ Slowest components:');
|
|
725
|
+
slowest.forEach((comp, i) => {
|
|
726
|
+
console.log(
|
|
727
|
+
`${i + 1}. ${comp.name}: ${comp.avg.toFixed(2)}ms avg (${comp.count} renders)`,
|
|
728
|
+
);
|
|
729
|
+
});
|
|
730
|
+
}
|
|
731
|
+
console.groupEnd();
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
clear() {
|
|
735
|
+
this.renderTimes = [];
|
|
736
|
+
this.measures.clear();
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
enable() {
|
|
740
|
+
this.enabled = true;
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
disable() {
|
|
744
|
+
this.enabled = false;
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
// Global profiler instance
|
|
749
|
+
const profiler = new Profiler();
|
|
750
|
+
|
|
751
|
+
/**
|
|
752
|
+
* Hook to profile component render
|
|
753
|
+
*/
|
|
754
|
+
const useProfiler = (componentName) => {
|
|
755
|
+
const startTime = performance.now();
|
|
756
|
+
|
|
757
|
+
return () => {
|
|
758
|
+
const duration = performance.now() - startTime;
|
|
759
|
+
profiler.recordRender(componentName, duration);
|
|
760
|
+
}
|
|
761
|
+
};
|
|
762
|
+
|
|
763
|
+
/**
|
|
764
|
+
* HOC to profile component
|
|
765
|
+
*/
|
|
766
|
+
const withProfiler = (Component, name) => {
|
|
767
|
+
return (props) => {
|
|
768
|
+
profiler.startMeasure(name);
|
|
769
|
+
const result = Component(props);
|
|
770
|
+
const duration = profiler.endMeasure(name);
|
|
771
|
+
if (duration) profiler.recordRender(name, duration);
|
|
772
|
+
return result
|
|
773
|
+
}
|
|
774
|
+
};
|
|
775
|
+
|
|
776
|
+
const workLoop = (deadline) => {
|
|
777
|
+
const state = getState();
|
|
778
|
+
let shouldYield = false;
|
|
779
|
+
|
|
780
|
+
while (state.nextUnitOfWork && !shouldYield) {
|
|
781
|
+
state.nextUnitOfWork = performUnitOfWork(state.nextUnitOfWork);
|
|
782
|
+
shouldYield = deadline.timeRemaining() < 1;
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
if (!state.nextUnitOfWork && state.wipRoot) {
|
|
786
|
+
commitRoot();
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
requestIdleCallback(workLoop);
|
|
790
|
+
};
|
|
791
|
+
|
|
792
|
+
requestIdleCallback(workLoop);
|
|
793
|
+
|
|
794
|
+
const performUnitOfWork = (fiber) => {
|
|
795
|
+
const componentName = fiber.type?.name || fiber.type?.displayName || 'Unknown';
|
|
796
|
+
|
|
797
|
+
profiler.startMeasure(componentName);
|
|
798
|
+
|
|
799
|
+
const isFunctionComponent = fiber.type instanceof Function;
|
|
800
|
+
if (isFunctionComponent) {
|
|
801
|
+
updateFunctionComponent(fiber);
|
|
802
|
+
} else {
|
|
803
|
+
updateHostComponent(fiber);
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
const duration = profiler.endMeasure(componentName);
|
|
807
|
+
if (duration) profiler.recordRender(componentName, duration);
|
|
808
|
+
|
|
809
|
+
if (fiber.child) {
|
|
810
|
+
return fiber.child
|
|
811
|
+
}
|
|
812
|
+
let nextFiber = fiber;
|
|
813
|
+
while (nextFiber) {
|
|
814
|
+
if (nextFiber.sibling) {
|
|
815
|
+
return nextFiber.sibling
|
|
816
|
+
}
|
|
817
|
+
nextFiber = nextFiber.parent;
|
|
818
|
+
}
|
|
819
|
+
};
|
|
820
|
+
|
|
821
|
+
const scheduleWork = (root, priority = Priority.NORMAL) => {
|
|
822
|
+
const state = getState();
|
|
823
|
+
state.nextUnitOfWork = root;
|
|
824
|
+
state.wipRoot = root;
|
|
825
|
+
state.deletions = [];
|
|
826
|
+
state.hookIndex = 0;
|
|
827
|
+
state.effects = [];
|
|
828
|
+
|
|
829
|
+
// Higher priority = faster scheduling
|
|
830
|
+
if (priority <= Priority.USER_BLOCKING) {
|
|
831
|
+
requestIdleCallback(workLoop);
|
|
832
|
+
} else {
|
|
833
|
+
setTimeout(() => requestIdleCallback(workLoop), 0);
|
|
834
|
+
}
|
|
835
|
+
};
|
|
836
|
+
|
|
837
|
+
const render = (element, container) => {
|
|
838
|
+
const state = getState();
|
|
839
|
+
state.wipRoot = {
|
|
840
|
+
dom: container,
|
|
841
|
+
props: {
|
|
842
|
+
children: [element],
|
|
843
|
+
},
|
|
844
|
+
alternate: state.currentRoot,
|
|
845
|
+
};
|
|
846
|
+
|
|
847
|
+
state.nextUnitOfWork = state.wipRoot;
|
|
848
|
+
state.deletions = [];
|
|
849
|
+
scheduleWork(state.wipRoot);
|
|
850
|
+
return state.wipRoot
|
|
851
|
+
};
|
|
852
|
+
|
|
853
|
+
const init = (MainElement, root = '__ryunix') => {
|
|
854
|
+
const state = getState();
|
|
855
|
+
state.containerRoot = document.getElementById(root);
|
|
856
|
+
const renderProcess = render(MainElement, state.containerRoot);
|
|
857
|
+
return renderProcess
|
|
858
|
+
};
|
|
859
|
+
|
|
860
|
+
const safeRender = (component, props, onError) => {
|
|
861
|
+
try {
|
|
862
|
+
return component(props)
|
|
863
|
+
} catch (error) {
|
|
864
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
865
|
+
console.error('Component error:', error);
|
|
866
|
+
}
|
|
867
|
+
if (onError) onError(error);
|
|
868
|
+
return null
|
|
869
|
+
}
|
|
870
|
+
};
|
|
871
|
+
|
|
872
|
+
const validateHookCall = () => {
|
|
873
|
+
const state = getState();
|
|
874
|
+
if (!state.wipFiber) {
|
|
875
|
+
throw new Error(
|
|
876
|
+
'Hooks can only be called inside the body of a function component.',
|
|
877
|
+
)
|
|
878
|
+
}
|
|
879
|
+
if (!Array.isArray(state.wipFiber.hooks)) {
|
|
880
|
+
state.wipFiber.hooks = [];
|
|
881
|
+
}
|
|
882
|
+
};
|
|
883
|
+
|
|
884
|
+
const haveDepsChanged = (oldDeps, newDeps) => {
|
|
885
|
+
if (!oldDeps || !newDeps) return true
|
|
886
|
+
if (oldDeps.length !== newDeps.length) return true
|
|
887
|
+
return oldDeps.some((dep, i) => !Object.is(dep, newDeps[i]))
|
|
888
|
+
};
|
|
889
|
+
|
|
890
|
+
const useStore = (initialState) => {
|
|
891
|
+
const reducer = (state, action) =>
|
|
892
|
+
is.function(action) ? action(state) : action;
|
|
893
|
+
return useReducer(reducer, initialState)
|
|
894
|
+
};
|
|
895
|
+
|
|
896
|
+
const useReducer = (reducer, initialState, init) => {
|
|
897
|
+
validateHookCall();
|
|
898
|
+
|
|
899
|
+
const state = getState();
|
|
900
|
+
const { wipFiber, hookIndex } = state;
|
|
901
|
+
const oldHook = wipFiber.alternate?.hooks?.[hookIndex];
|
|
902
|
+
|
|
903
|
+
const hook = {
|
|
904
|
+
hookID: hookIndex,
|
|
905
|
+
type: RYUNIX_TYPES.RYUNIX_STORE,
|
|
906
|
+
state: oldHook ? oldHook.state : init ? init(initialState) : initialState,
|
|
907
|
+
queue: [], // Siempre nueva cola vacía
|
|
908
|
+
};
|
|
909
|
+
|
|
910
|
+
// Procesar acciones del render anterior
|
|
911
|
+
if (oldHook?.queue) {
|
|
912
|
+
oldHook.queue.forEach((action) => {
|
|
913
|
+
try {
|
|
914
|
+
hook.state = reducer(hook.state, action);
|
|
915
|
+
} catch (error) {
|
|
916
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
917
|
+
console.error('Error in reducer:', error);
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
});
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
const dispatch = (action) => {
|
|
924
|
+
if (action === undefined) {
|
|
925
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
926
|
+
console.warn('dispatch called with undefined action');
|
|
927
|
+
}
|
|
928
|
+
return
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
hook.queue.push(action);
|
|
932
|
+
|
|
933
|
+
const currentState = getState();
|
|
934
|
+
currentState.wipRoot = {
|
|
935
|
+
dom: currentState.currentRoot.dom,
|
|
936
|
+
props: currentState.currentRoot.props,
|
|
937
|
+
alternate: currentState.currentRoot,
|
|
938
|
+
};
|
|
939
|
+
currentState.deletions = [];
|
|
940
|
+
currentState.hookIndex = 0;
|
|
941
|
+
scheduleWork(currentState.wipRoot);
|
|
942
|
+
};
|
|
943
|
+
|
|
944
|
+
wipFiber.hooks[hookIndex] = hook;
|
|
945
|
+
state.hookIndex++;
|
|
946
|
+
return [hook.state, dispatch]
|
|
947
|
+
};
|
|
948
|
+
|
|
949
|
+
const useEffect = (callback, deps) => {
|
|
950
|
+
validateHookCall();
|
|
951
|
+
|
|
952
|
+
if (!is.function(callback)) {
|
|
953
|
+
throw new Error('useEffect callback must be a function')
|
|
954
|
+
}
|
|
955
|
+
if (deps !== undefined && !Array.isArray(deps)) {
|
|
956
|
+
throw new Error('useEffect dependencies must be an array or undefined')
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
const state = getState();
|
|
960
|
+
const { wipFiber, hookIndex } = state;
|
|
961
|
+
const oldHook = wipFiber.alternate?.hooks?.[hookIndex];
|
|
962
|
+
const hasChanged = haveDepsChanged(oldHook?.deps, deps);
|
|
963
|
+
|
|
964
|
+
const hook = {
|
|
965
|
+
hookID: hookIndex,
|
|
966
|
+
type: RYUNIX_TYPES.RYUNIX_EFFECT,
|
|
967
|
+
deps,
|
|
968
|
+
effect: hasChanged ? callback : null,
|
|
969
|
+
cancel: oldHook?.cancel,
|
|
970
|
+
};
|
|
971
|
+
|
|
972
|
+
wipFiber.hooks[hookIndex] = hook;
|
|
973
|
+
state.hookIndex++;
|
|
974
|
+
};
|
|
975
|
+
|
|
976
|
+
const useRef = (initialValue) => {
|
|
977
|
+
validateHookCall();
|
|
978
|
+
|
|
979
|
+
const state = getState();
|
|
980
|
+
const { wipFiber, hookIndex } = state;
|
|
981
|
+
const oldHook = wipFiber.alternate?.hooks?.[hookIndex];
|
|
982
|
+
|
|
983
|
+
const hook = {
|
|
984
|
+
hookID: hookIndex,
|
|
985
|
+
type: RYUNIX_TYPES.RYUNIX_REF,
|
|
986
|
+
value: oldHook ? oldHook.value : { current: initialValue },
|
|
987
|
+
};
|
|
988
|
+
|
|
989
|
+
wipFiber.hooks[hookIndex] = hook;
|
|
990
|
+
state.hookIndex++;
|
|
991
|
+
return hook.value
|
|
992
|
+
};
|
|
993
|
+
|
|
994
|
+
const useMemo = (compute, deps) => {
|
|
995
|
+
validateHookCall();
|
|
996
|
+
|
|
997
|
+
if (!is.function(compute)) {
|
|
998
|
+
throw new Error('useMemo callback must be a function')
|
|
999
|
+
}
|
|
1000
|
+
if (!Array.isArray(deps)) {
|
|
1001
|
+
throw new Error('useMemo requires a dependencies array')
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
const state = getState();
|
|
1005
|
+
const { wipFiber, hookIndex } = state;
|
|
1006
|
+
const oldHook = wipFiber.alternate?.hooks?.[hookIndex];
|
|
1007
|
+
|
|
1008
|
+
let value;
|
|
1009
|
+
if (oldHook && !haveDepsChanged(oldHook.deps, deps)) {
|
|
1010
|
+
value = oldHook.value;
|
|
1011
|
+
} else {
|
|
1012
|
+
try {
|
|
1013
|
+
value = compute();
|
|
1014
|
+
} catch (error) {
|
|
1015
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
1016
|
+
console.error('Error in useMemo computation:', error);
|
|
1017
|
+
}
|
|
1018
|
+
value = undefined;
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
|
|
1022
|
+
const hook = {
|
|
1023
|
+
hookID: hookIndex,
|
|
1024
|
+
type: RYUNIX_TYPES.RYUNIX_MEMO,
|
|
1025
|
+
value,
|
|
1026
|
+
deps,
|
|
1027
|
+
};
|
|
1028
|
+
|
|
1029
|
+
wipFiber.hooks[hookIndex] = hook;
|
|
1030
|
+
state.hookIndex++;
|
|
1031
|
+
return value
|
|
1032
|
+
};
|
|
1033
|
+
|
|
1034
|
+
const useCallback = (callback, deps) => {
|
|
1035
|
+
if (!is.function(callback)) {
|
|
1036
|
+
throw new Error('useCallback requires a function as first argument')
|
|
1037
|
+
}
|
|
1038
|
+
return useMemo(() => callback, deps)
|
|
1039
|
+
};
|
|
1040
|
+
|
|
1041
|
+
const createContext = (
|
|
1042
|
+
contextId = RYUNIX_TYPES.RYUNIX_CONTEXT,
|
|
1043
|
+
defaultValue = {},
|
|
1044
|
+
) => {
|
|
1045
|
+
const Provider = ({ children, value }) => {
|
|
1046
|
+
const element = Fragment({ children });
|
|
1047
|
+
element._contextId = contextId;
|
|
1048
|
+
element._contextValue = value;
|
|
1049
|
+
return element
|
|
1050
|
+
};
|
|
1051
|
+
|
|
1052
|
+
Provider._contextId = contextId;
|
|
1053
|
+
|
|
1054
|
+
const useContext = (ctxID = contextId) => {
|
|
1055
|
+
validateHookCall();
|
|
1056
|
+
|
|
1057
|
+
const state = getState();
|
|
1058
|
+
let fiber = state.wipFiber;
|
|
1059
|
+
|
|
1060
|
+
while (fiber) {
|
|
1061
|
+
if (fiber._contextId === ctxID && fiber._contextValue !== undefined) {
|
|
1062
|
+
return fiber._contextValue
|
|
1063
|
+
}
|
|
1064
|
+
if (
|
|
1065
|
+
fiber.type?._contextId === ctxID &&
|
|
1066
|
+
fiber.props?.value !== undefined
|
|
1067
|
+
) {
|
|
1068
|
+
return fiber.props.value
|
|
1069
|
+
}
|
|
1070
|
+
fiber = fiber.parent;
|
|
1071
|
+
}
|
|
1072
|
+
return defaultValue
|
|
1073
|
+
};
|
|
1074
|
+
|
|
1075
|
+
return { Provider, useContext }
|
|
1076
|
+
};
|
|
1077
|
+
|
|
1078
|
+
const useQuery = () => {
|
|
1079
|
+
if (typeof window === 'undefined') return {}
|
|
1080
|
+
|
|
1081
|
+
const searchParams = new URLSearchParams(window.location.search);
|
|
1082
|
+
const query = {};
|
|
1083
|
+
for (const [key, value] of searchParams.entries()) {
|
|
1084
|
+
query[key] = value;
|
|
1085
|
+
}
|
|
1086
|
+
return query
|
|
1087
|
+
};
|
|
1088
|
+
|
|
1089
|
+
const useHash = () => {
|
|
1090
|
+
if (typeof window === 'undefined') return ''
|
|
1091
|
+
|
|
1092
|
+
const [hash, setHash] = useStore(window.location.hash);
|
|
1093
|
+
useEffect(() => {
|
|
1094
|
+
const onHashChange = () => setHash(window.location.hash);
|
|
1095
|
+
window.addEventListener('hashchange', onHashChange);
|
|
1096
|
+
return () => window.removeEventListener('hashchange', onHashChange)
|
|
1097
|
+
}, []);
|
|
1098
|
+
return hash
|
|
1099
|
+
};
|
|
1100
|
+
|
|
1101
|
+
const useMetadata = (tags = {}, options = {}) => {
|
|
1102
|
+
useEffect(() => {
|
|
1103
|
+
if (typeof document === 'undefined') return
|
|
1104
|
+
|
|
1105
|
+
let finalTitle = 'Ryunix App';
|
|
1106
|
+
const template = options.title?.template;
|
|
1107
|
+
const defaultTitle = options.title?.prefix || 'Ryunix App';
|
|
1108
|
+
const pageTitle = tags.pageTitle || tags.title;
|
|
1109
|
+
|
|
1110
|
+
if (is.string(pageTitle) && pageTitle.trim()) {
|
|
1111
|
+
finalTitle = template?.includes('%s')
|
|
1112
|
+
? template.replace('%s', pageTitle)
|
|
1113
|
+
: pageTitle;
|
|
1114
|
+
} else {
|
|
1115
|
+
finalTitle = defaultTitle;
|
|
1116
|
+
}
|
|
1117
|
+
|
|
1118
|
+
document.title = finalTitle;
|
|
1119
|
+
|
|
1120
|
+
if (tags.canonical) {
|
|
1121
|
+
let link = document.querySelector('link[rel="canonical"]');
|
|
1122
|
+
if (!link) {
|
|
1123
|
+
link = document.createElement('link');
|
|
1124
|
+
link.setAttribute('rel', 'canonical');
|
|
1125
|
+
document.head.appendChild(link);
|
|
1126
|
+
}
|
|
1127
|
+
link.setAttribute('href', tags.canonical);
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
Object.entries(tags).forEach(([key, value]) => {
|
|
1131
|
+
if (['title', 'pageTitle', 'canonical'].includes(key)) return
|
|
1132
|
+
|
|
1133
|
+
const isProperty = key.startsWith('og:') || key.startsWith('twitter:');
|
|
1134
|
+
const selector = `meta[${isProperty ? 'property' : 'name'}='${key}']`;
|
|
1135
|
+
let meta = document.head.querySelector(selector);
|
|
1136
|
+
|
|
1137
|
+
if (!meta) {
|
|
1138
|
+
meta = document.createElement('meta');
|
|
1139
|
+
meta.setAttribute(isProperty ? 'property' : 'name', key);
|
|
1140
|
+
document.head.appendChild(meta);
|
|
1141
|
+
}
|
|
1142
|
+
meta.setAttribute('content', value);
|
|
1143
|
+
});
|
|
1144
|
+
}, [JSON.stringify(tags), JSON.stringify(options)]);
|
|
1145
|
+
};
|
|
1146
|
+
|
|
1147
|
+
// Router Context
|
|
1148
|
+
const RouterContext = createContext('ryunix.navigation', {
|
|
1149
|
+
location: '/',
|
|
1150
|
+
params: {},
|
|
1151
|
+
query: {},
|
|
1152
|
+
navigate: (path) => {},
|
|
1153
|
+
route: null,
|
|
1154
|
+
});
|
|
1155
|
+
|
|
1156
|
+
const findRoute = (routes, path) => {
|
|
1157
|
+
const pathname = path.split('?')[0].split('#')[0];
|
|
1158
|
+
const notFoundRoute = routes.find((route) => route.NotFound);
|
|
1159
|
+
const notFound = notFoundRoute
|
|
1160
|
+
? { route: { component: notFoundRoute.NotFound }, params: {} }
|
|
1161
|
+
: { route: { component: null }, params: {} };
|
|
1162
|
+
|
|
1163
|
+
for (const route of routes) {
|
|
1164
|
+
if (route.subRoutes) {
|
|
1165
|
+
const childRoute = findRoute(route.subRoutes, path);
|
|
1166
|
+
if (childRoute) return childRoute
|
|
1167
|
+
}
|
|
1168
|
+
if (route.path === '*') return notFound
|
|
1169
|
+
if (!route.path || typeof route.path !== 'string') continue
|
|
1170
|
+
|
|
1171
|
+
const keys = [];
|
|
1172
|
+
const pattern = new RegExp(
|
|
1173
|
+
`^${route.path.replace(/:\w+/g, (match) => {
|
|
1174
|
+
keys.push(match.substring(1));
|
|
1175
|
+
return '([^/]+)'
|
|
1176
|
+
})}$`,
|
|
1177
|
+
);
|
|
1178
|
+
|
|
1179
|
+
const match = pathname.match(pattern);
|
|
1180
|
+
if (match) {
|
|
1181
|
+
const params = keys.reduce((acc, key, index) => {
|
|
1182
|
+
acc[key] = match[index + 1];
|
|
1183
|
+
return acc
|
|
1184
|
+
}, {});
|
|
1185
|
+
return { route, params }
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
1188
|
+
return notFound
|
|
1189
|
+
};
|
|
1190
|
+
|
|
1191
|
+
const RouterProvider = ({ routes, children }) => {
|
|
1192
|
+
const [location, setLocation] = useStore(window.location.pathname);
|
|
1193
|
+
|
|
1194
|
+
useEffect(() => {
|
|
1195
|
+
const update = () => setLocation(window.location.pathname);
|
|
1196
|
+
window.addEventListener('popstate', update);
|
|
1197
|
+
window.addEventListener('hashchange', update);
|
|
1198
|
+
return () => {
|
|
1199
|
+
window.removeEventListener('popstate', update);
|
|
1200
|
+
window.removeEventListener('hashchange', update);
|
|
1201
|
+
}
|
|
1202
|
+
}, [location]);
|
|
1203
|
+
|
|
1204
|
+
const navigate = (path) => {
|
|
1205
|
+
window.history.pushState({}, '', path);
|
|
1206
|
+
setLocation(path);
|
|
1207
|
+
};
|
|
1208
|
+
|
|
1209
|
+
const currentRouteData = findRoute(routes, location) || {};
|
|
1210
|
+
const query = useQuery();
|
|
1211
|
+
|
|
1212
|
+
const contextValue = {
|
|
1213
|
+
location,
|
|
1214
|
+
params: currentRouteData.params || {},
|
|
1215
|
+
query,
|
|
1216
|
+
navigate,
|
|
1217
|
+
route: currentRouteData.route,
|
|
1218
|
+
};
|
|
1219
|
+
|
|
1220
|
+
return createElement(
|
|
1221
|
+
RouterContext.Provider,
|
|
1222
|
+
{ value: contextValue },
|
|
1223
|
+
Fragment({ children }),
|
|
1224
|
+
)
|
|
1225
|
+
};
|
|
1226
|
+
|
|
1227
|
+
const useRouter = () => {
|
|
1228
|
+
return RouterContext.useContext('ryunix.navigation')
|
|
1229
|
+
};
|
|
1230
|
+
|
|
1231
|
+
const Children = () => {
|
|
1232
|
+
const { route, params, query, location } = useRouter();
|
|
1233
|
+
if (!route || !route.component) return null
|
|
1234
|
+
const hash = useHash();
|
|
1235
|
+
|
|
1236
|
+
useEffect(() => {
|
|
1237
|
+
if (hash) {
|
|
1238
|
+
const id = hash.slice(1);
|
|
1239
|
+
const el = document.getElementById(id);
|
|
1240
|
+
if (el) el.scrollIntoView({ block: 'start', behavior: 'smooth' });
|
|
1241
|
+
}
|
|
1242
|
+
}, [hash]);
|
|
1243
|
+
|
|
1244
|
+
return createElement(route.component, {
|
|
1245
|
+
key: location,
|
|
1246
|
+
params,
|
|
1247
|
+
query,
|
|
1248
|
+
hash,
|
|
1249
|
+
})
|
|
1250
|
+
};
|
|
1251
|
+
|
|
1252
|
+
const NavLink = ({ to, exact = false, ...props }) => {
|
|
1253
|
+
const { location, navigate } = useRouter();
|
|
1254
|
+
const isActive = exact ? location === to : location.startsWith(to);
|
|
1255
|
+
|
|
1256
|
+
const resolveClass = (cls) =>
|
|
1257
|
+
typeof cls === 'function' ? cls({ isActive }) : cls || '';
|
|
1258
|
+
|
|
1259
|
+
const handleClick = (e) => {
|
|
1260
|
+
e.preventDefault();
|
|
1261
|
+
navigate(to);
|
|
1262
|
+
};
|
|
1263
|
+
|
|
1264
|
+
const classAttrName = props['ryunix-class'] ? 'ryunix-class' : 'className';
|
|
1265
|
+
const classAttrValue = resolveClass(
|
|
1266
|
+
props['ryunix-class'] || props['className'],
|
|
1267
|
+
);
|
|
1268
|
+
|
|
1269
|
+
const {
|
|
1270
|
+
['ryunix-class']: _omitRyunix,
|
|
1271
|
+
className: _omitClassName,
|
|
1272
|
+
...cleanedProps
|
|
1273
|
+
} = props;
|
|
1274
|
+
|
|
1275
|
+
return createElement(
|
|
1276
|
+
'a',
|
|
1277
|
+
{
|
|
1278
|
+
href: to,
|
|
1279
|
+
onClick: handleClick,
|
|
1280
|
+
[classAttrName]: classAttrValue,
|
|
1281
|
+
...cleanedProps,
|
|
1282
|
+
},
|
|
1283
|
+
props.children,
|
|
1284
|
+
)
|
|
1285
|
+
};
|
|
1286
|
+
|
|
1287
|
+
/**
|
|
1288
|
+
* useStore with priority support
|
|
1289
|
+
*/
|
|
1290
|
+
const useStorePriority = (initialState) => {
|
|
1291
|
+
const reducer = (state, action) =>
|
|
1292
|
+
typeof action === 'function' ? action.value(state) : action.value;
|
|
1293
|
+
|
|
1294
|
+
const [state, baseDispatch] = useReducer(reducer, initialState);
|
|
1295
|
+
|
|
1296
|
+
const dispatch = (action, priority = Priority.NORMAL) => {
|
|
1297
|
+
const wrappedAction = {
|
|
1298
|
+
value: action,
|
|
1299
|
+
priority,
|
|
1300
|
+
};
|
|
1301
|
+
|
|
1302
|
+
baseDispatch(wrappedAction);
|
|
1303
|
+
};
|
|
1304
|
+
|
|
1305
|
+
return [state, dispatch]
|
|
1306
|
+
};
|
|
1307
|
+
|
|
1308
|
+
/**
|
|
1309
|
+
* useTransition - Mark updates as non-urgent
|
|
1310
|
+
*/
|
|
1311
|
+
const useTransition = () => {
|
|
1312
|
+
const [isPending, setIsPending] = useStorePriority(false);
|
|
1313
|
+
|
|
1314
|
+
const startTransition = (callback) => {
|
|
1315
|
+
setIsPending(true, Priority.IMMEDIATE);
|
|
1316
|
+
|
|
1317
|
+
setTimeout(() => {
|
|
1318
|
+
callback();
|
|
1319
|
+
setIsPending(false, Priority.IMMEDIATE);
|
|
1320
|
+
}, 0);
|
|
1321
|
+
};
|
|
1322
|
+
|
|
1323
|
+
return [isPending, startTransition]
|
|
1324
|
+
};
|
|
1325
|
+
|
|
1326
|
+
/**
|
|
1327
|
+
* useDeferredValue - Defer value updates
|
|
1328
|
+
*/
|
|
1329
|
+
const useDeferredValue = (value) => {
|
|
1330
|
+
const [deferredValue, setDeferredValue] = useStorePriority(value);
|
|
1331
|
+
|
|
1332
|
+
useEffect(() => {
|
|
1333
|
+
const timeout = setTimeout(() => {
|
|
1334
|
+
setDeferredValue(value, Priority.LOW);
|
|
1335
|
+
}, 100);
|
|
1336
|
+
|
|
1337
|
+
return () => clearTimeout(timeout)
|
|
1338
|
+
}, [value]);
|
|
1339
|
+
|
|
1340
|
+
return deferredValue
|
|
1341
|
+
};
|
|
1342
|
+
|
|
1343
|
+
var hooks = /*#__PURE__*/Object.freeze({
|
|
1344
|
+
__proto__: null,
|
|
1345
|
+
Children: Children,
|
|
1346
|
+
NavLink: NavLink,
|
|
1347
|
+
RouterProvider: RouterProvider,
|
|
1348
|
+
createContext: createContext,
|
|
1349
|
+
useCallback: useCallback,
|
|
1350
|
+
useDeferredValue: useDeferredValue,
|
|
1351
|
+
useEffect: useEffect,
|
|
1352
|
+
useHash: useHash,
|
|
1353
|
+
useMemo: useMemo,
|
|
1354
|
+
useMetadata: useMetadata,
|
|
1355
|
+
useQuery: useQuery,
|
|
1356
|
+
useReducer: useReducer,
|
|
1357
|
+
useRef: useRef,
|
|
1358
|
+
useRouter: useRouter,
|
|
1359
|
+
useStore: useStore,
|
|
1360
|
+
useStorePriority: useStorePriority,
|
|
1361
|
+
useTransition: useTransition
|
|
1362
|
+
});
|
|
1363
|
+
|
|
1364
|
+
/**
|
|
1365
|
+
* memo - Memoize component to prevent unnecessary re-renders
|
|
1366
|
+
*/
|
|
1367
|
+
const memo = (Component, arePropsEqual) => {
|
|
1368
|
+
return (props) => {
|
|
1369
|
+
const memoizedElement = useMemo(() => {
|
|
1370
|
+
return Component(props)
|
|
1371
|
+
}, [
|
|
1372
|
+
// Default comparison: shallow props comparison
|
|
1373
|
+
...Object.values(props),
|
|
1374
|
+
]);
|
|
1375
|
+
|
|
1376
|
+
return memoizedElement
|
|
1377
|
+
}
|
|
1378
|
+
};
|
|
1379
|
+
|
|
1380
|
+
/**
|
|
1381
|
+
* Lazy load component
|
|
1382
|
+
*/
|
|
1383
|
+
const lazy = (importFn) => {
|
|
1384
|
+
let Component = null;
|
|
1385
|
+
let promise = null;
|
|
1386
|
+
let error = null;
|
|
1387
|
+
|
|
1388
|
+
return (props) => {
|
|
1389
|
+
const [, forceUpdate] = useStore(0);
|
|
1390
|
+
|
|
1391
|
+
useEffect(() => {
|
|
1392
|
+
if (Component || error) return
|
|
1393
|
+
|
|
1394
|
+
if (!promise) {
|
|
1395
|
+
promise = importFn()
|
|
1396
|
+
.then((module) => {
|
|
1397
|
+
Component = module.default || module;
|
|
1398
|
+
forceUpdate((x) => x + 1);
|
|
1399
|
+
})
|
|
1400
|
+
.catch((err) => {
|
|
1401
|
+
error = err;
|
|
1402
|
+
forceUpdate((x) => x + 1);
|
|
1403
|
+
});
|
|
1404
|
+
}
|
|
1405
|
+
}, []);
|
|
1406
|
+
|
|
1407
|
+
if (error) throw error
|
|
1408
|
+
if (!Component) return null
|
|
1409
|
+
return createElement(Component, props)
|
|
1410
|
+
}
|
|
1411
|
+
};
|
|
1412
|
+
|
|
1413
|
+
/**
|
|
1414
|
+
* Suspense component (basic implementation)
|
|
1415
|
+
*/
|
|
1416
|
+
const Suspense = ({ fallback, children }) => {
|
|
1417
|
+
const [isLoading, setIsLoading] = useStore(true);
|
|
1418
|
+
|
|
1419
|
+
useEffect(() => {
|
|
1420
|
+
setIsLoading(false);
|
|
1421
|
+
}, []);
|
|
1422
|
+
|
|
1423
|
+
if (isLoading && fallback) {
|
|
1424
|
+
return fallback
|
|
1425
|
+
}
|
|
1426
|
+
|
|
1427
|
+
return children
|
|
1428
|
+
};
|
|
1429
|
+
|
|
1430
|
+
let isBatching = false;
|
|
1431
|
+
let pendingUpdates = [];
|
|
1432
|
+
|
|
1433
|
+
/**
|
|
1434
|
+
* Batch multiple state updates into single render
|
|
1435
|
+
*/
|
|
1436
|
+
const batchUpdates = (callback) => {
|
|
1437
|
+
const wasBatching = isBatching;
|
|
1438
|
+
isBatching = true;
|
|
1439
|
+
|
|
1440
|
+
try {
|
|
1441
|
+
callback();
|
|
1442
|
+
} finally {
|
|
1443
|
+
isBatching = wasBatching;
|
|
1444
|
+
|
|
1445
|
+
if (!isBatching && pendingUpdates.length > 0) {
|
|
1446
|
+
flushUpdates();
|
|
1447
|
+
}
|
|
1448
|
+
}
|
|
1449
|
+
};
|
|
1450
|
+
|
|
1451
|
+
/**
|
|
1452
|
+
* Flush all pending updates
|
|
1453
|
+
*/
|
|
1454
|
+
const flushUpdates = () => {
|
|
1455
|
+
if (pendingUpdates.length === 0) return
|
|
1456
|
+
|
|
1457
|
+
const updates = pendingUpdates;
|
|
1458
|
+
pendingUpdates = [];
|
|
1459
|
+
|
|
1460
|
+
// Execute all updates
|
|
1461
|
+
updates.forEach((update) => update());
|
|
1462
|
+
};
|
|
1463
|
+
|
|
1464
|
+
var Ryunix = /*#__PURE__*/Object.freeze({
|
|
1465
|
+
__proto__: null,
|
|
1466
|
+
Fragment: Fragment,
|
|
1467
|
+
Hooks: hooks,
|
|
1468
|
+
Priority: Priority,
|
|
1469
|
+
Suspense: Suspense,
|
|
1470
|
+
batchUpdates: batchUpdates,
|
|
1471
|
+
createElement: createElement,
|
|
1472
|
+
init: init,
|
|
1473
|
+
lazy: lazy,
|
|
1474
|
+
memo: memo,
|
|
1475
|
+
profiler: profiler,
|
|
1476
|
+
render: render,
|
|
1477
|
+
safeRender: safeRender,
|
|
1478
|
+
useProfiler: useProfiler,
|
|
1479
|
+
withProfiler: withProfiler
|
|
1480
|
+
});
|
|
1481
|
+
|
|
1482
|
+
window.Ryunix = Ryunix;
|
|
1483
|
+
|
|
1484
|
+
export { Fragment, hooks as Hooks, Image, Priority, Suspense, batchUpdates, createElement, Ryunix as default, init, lazy, memo, profiler, render, safeRender, useProfiler, withProfiler };
|
|
1485
|
+
//# sourceMappingURL=Ryunix.esm.js.map
|