@unsetsoft/ryunixjs 0.2.28 → 0.2.29-nightly.1

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.js CHANGED
@@ -4,44 +4,37 @@
4
4
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Ryunix = {}));
5
5
  })(this, (function (exports) { 'use strict';
6
6
 
7
- let containerRoot = null;
8
- let nextUnitOfWork = null;
9
- let currentRoot = null;
10
- let wipRoot = null;
11
- let deletions = null;
12
- let wipFiber = null;
13
- let hookIndex = null;
14
-
15
- const RYUNIX_TYPES = {
7
+ const vars = {
8
+ containerRoot: null,
9
+ nextUnitOfWork: null,
10
+ currentRoot: null,
11
+ wipRoot: null,
12
+ deletions: null,
13
+ wipFiber: null,
14
+ hookIndex: null,
15
+ };
16
+
17
+ const reg = /[A-Z]/g;
18
+
19
+ const RYUNIX_TYPES = Object.freeze({
16
20
  TEXT_ELEMENT: Symbol("text.element"),
17
21
  RYUNIX_EFFECT: Symbol("ryunix.effect"),
18
22
  RYUNIX_CONTEXT: Symbol("ryunix.context"),
19
- };
23
+ });
20
24
 
21
- const STRINGS = {
25
+ const STRINGS = Object.freeze({
22
26
  object: "object",
23
27
  function: "function",
24
28
  style: "style",
25
29
  className: "className",
26
30
  children: "children",
27
- };
31
+ });
28
32
 
29
- const EFFECT_TAGS = {
33
+ const EFFECT_TAGS = Object.freeze({
30
34
  PLACEMENT: Symbol(),
31
35
  UPDATE: Symbol(),
32
36
  DELETION: Symbol(),
33
- };
34
-
35
- const isEvent = (key) => key.startsWith("on");
36
- const isProperty = (key) => key !== STRINGS.children && !isEvent(key);
37
- const isNew = (prev, next) => (key) => prev[key] !== next[key];
38
- const isGone = (next) => (key) => !(key in next);
39
- const reg = /[A-Z]/g;
40
- const hasDepsChanged = (prevDeps, nextDeps) =>
41
- !prevDeps ||
42
- !nextDeps ||
43
- prevDeps.length !== nextDeps.length ||
44
- prevDeps.some((dep, index) => dep !== nextDeps[index]);
37
+ });
45
38
 
46
39
  /**
47
40
  * The function creates a new element with the given type, props, and children.
@@ -59,7 +52,8 @@
59
52
  * includes any additional properties passed in the `props` argument, as well as a `children` property
60
53
  * that is an array of any child elements passed in the `...children` argument
61
54
  */
62
- function createElement(type, props, ...children) {
55
+
56
+ const createElement = (type, props, ...children) => {
63
57
  return {
64
58
  type,
65
59
  props: {
@@ -71,7 +65,7 @@
71
65
  ),
72
66
  },
73
67
  };
74
- }
68
+ };
75
69
 
76
70
  /**
77
71
  * The function creates a text element with a given text value.
@@ -79,7 +73,8 @@
79
73
  * @returns A JavaScript object with a `type` property set to `"TEXT_ELEMENT"` and a `props` property
80
74
  * that contains a `nodeValue` property set to the `text` parameter and an empty `children` array.
81
75
  */
82
- function createTextElement(text) {
76
+
77
+ const createTextElement = (text) => {
83
78
  return {
84
79
  type: RYUNIX_TYPES.TEXT_ELEMENT,
85
80
  props: {
@@ -87,7 +82,208 @@
87
82
  children: [],
88
83
  },
89
84
  };
90
- }
85
+ };
86
+
87
+ const Fragments = (props) => {
88
+ if (props.style) {
89
+ throw new Error("The style attribute is not supported");
90
+ }
91
+ if (props.className === "") {
92
+ throw new Error("className cannot be empty.");
93
+ }
94
+ return createElement("div", props, props.children);
95
+ };
96
+
97
+ /**
98
+ * The function renders an element into a container using a work-in-progress root.
99
+ * @param element - The element parameter is the component or element that needs to be rendered in the
100
+ * container. It could be a Ryunix component or a DOM element.
101
+ * @param container - The container parameter is the DOM element where the rendered element will be
102
+ * appended to. this parameter is optional if you use createRoot().
103
+ */
104
+ const render = (element, container) => {
105
+ vars.wipRoot = {
106
+ dom: vars.containerRoot || container,
107
+ props: {
108
+ children: [element],
109
+ },
110
+ alternate: vars.currentRoot,
111
+ };
112
+ vars.deletions = [];
113
+ vars.nextUnitOfWork = vars.wipRoot;
114
+ };
115
+
116
+ /**
117
+ * @description The function creates a reference to a DOM element with the specified ID. This will be used to initialize the app.
118
+ * @example Ryunix.init("root") -> <div id="root" />
119
+ * @param root - The parameter "root" is the id of the HTML element that will serve as the container
120
+ * for the root element.
121
+ */
122
+ const init = (root) => {
123
+ vars.containerRoot = document.getElementById(root);
124
+ };
125
+
126
+ const isEvent = (key) => key.startsWith("on");
127
+ const isProperty = (key) => key !== STRINGS.children && !isEvent(key);
128
+ const isNew = (prev, next) => (key) => prev[key] !== next[key];
129
+ const isGone = (next) => (key) => !(key in next);
130
+ const hasDepsChanged = (prevDeps, nextDeps) =>
131
+ !prevDeps ||
132
+ !nextDeps ||
133
+ prevDeps.length !== nextDeps.length ||
134
+ prevDeps.some((dep, index) => dep !== nextDeps[index]);
135
+
136
+ /**
137
+ * The function cancels all effect hooks in a given fiber.
138
+ * @param fiber - The "fiber" parameter is likely referring to a data structure used in React.js to
139
+ * represent a component and its state. It contains information about the component's props, state, and
140
+ * children, as well as metadata used by React to manage updates and rendering. The function
141
+ * "cancelEffects" is likely intended
142
+ */
143
+ const cancelEffects = (fiber) => {
144
+ if (fiber.hooks) {
145
+ fiber.hooks
146
+ .filter((hook) => hook.tag === RYUNIX_TYPES.RYUNIX_EFFECT && hook.cancel)
147
+ .forEach((effectHook) => {
148
+ effectHook.cancel();
149
+ });
150
+ }
151
+ };
152
+
153
+ /**
154
+ * The function runs all effect hooks in a given fiber.
155
+ * @param fiber - The "fiber" parameter is likely referring to a data structure used in the
156
+ * implementation of a fiber-based reconciliation algorithm, such as the one used in React. A fiber
157
+ * represents a unit of work that needs to be performed by the reconciliation algorithm, and it
158
+ * contains information about a component and its children, as
159
+ */
160
+ const runEffects = (fiber) => {
161
+ if (fiber.hooks) {
162
+ fiber.hooks
163
+ .filter((hook) => hook.tag === RYUNIX_TYPES.RYUNIX_EFFECT && hook.effect)
164
+ .forEach((effectHook) => {
165
+ effectHook.cancel = effectHook.effect();
166
+ });
167
+ }
168
+ };
169
+
170
+ /**
171
+ * The function `useContext` is used to read and subscribe to context from your component.
172
+ * @param ref - The `ref` parameter is a reference to a context object.
173
+ * @returns The `Value` property of the `hook` object is being returned.
174
+ */
175
+ const useContext = (ref) => {
176
+ vars.hookIndex++;
177
+
178
+ const oldHook =
179
+ vars.wipFiber.alternate &&
180
+ vars.wipFiber.alternate.hooks &&
181
+ vars.wipFiber.alternate.hooks[vars.hookIndex];
182
+
183
+ const hasOld = oldHook ? oldHook : undefined;
184
+ const Context = hasOld ? hasOld : ref;
185
+ const hook = {
186
+ ...Context,
187
+ };
188
+
189
+ vars.wipFiber.hooks.push(hook);
190
+
191
+ return hook.Value;
192
+ };
193
+
194
+ /**
195
+ * @description The function creates a state.
196
+ * @param initial - The initial value of the state for the hook.
197
+ * @returns The `useStore` function returns an array with two elements: the current state value and a
198
+ * `setState` function that can be used to update the state.
199
+ */
200
+ const useStore = (initial) => {
201
+ const oldHook =
202
+ vars.wipFiber.alternate &&
203
+ vars.wipFiber.alternate.hooks &&
204
+ vars.wipFiber.alternate.hooks[vars.hookIndex];
205
+ const hook = {
206
+ state: oldHook ? oldHook.state : initial,
207
+ queue: [],
208
+ };
209
+
210
+ const actions = oldHook ? oldHook.queue : [];
211
+ actions.forEach((action) => {
212
+ hook.state =
213
+ typeof action === STRINGS.function ? action(hook.state) : action;
214
+ });
215
+
216
+ /**
217
+ * The function `setState` updates the state of a component in Ryunix by adding an action to a queue
218
+ * and setting up a new work-in-progress root.
219
+ * @param action - The `action` parameter is an object that represents a state update to be performed
220
+ * on a component. It contains information about the type of update to be performed and any new data
221
+ * that needs to be applied to the component's state.
222
+ */
223
+ const setState = (action) => {
224
+ hook.queue.push(action);
225
+ vars.wipRoot = {
226
+ dom: vars.currentRoot.dom,
227
+ props: vars.currentRoot.props,
228
+ alternate: vars.currentRoot,
229
+ };
230
+ vars.nextUnitOfWork = vars.wipRoot;
231
+ vars.deletions = [];
232
+ };
233
+
234
+ vars.wipFiber.hooks.push(hook);
235
+ vars.hookIndex++;
236
+ return [hook.state, setState];
237
+ };
238
+
239
+ /**
240
+ * This is a function that creates a hook for managing side effects in Ryunix components.
241
+ * @param effect - The effect function that will be executed after the component has rendered or when
242
+ * the dependencies have changed. It can perform side effects such as fetching data, updating the DOM,
243
+ * or subscribing to events.
244
+ * @param deps - An array of dependencies that the effect depends on. If any of the dependencies change
245
+ * between renders, the effect will be re-run. If the array is empty, the effect will only run once on
246
+ * mount and never again.
247
+ */
248
+ const useEffect = (effect, deps) => {
249
+ const oldHook =
250
+ vars.wipFiber.alternate &&
251
+ vars.wipFiber.alternate.hooks &&
252
+ vars.wipFiber.alternate.hooks[vars.hookIndex];
253
+
254
+ const hasChanged = hasDepsChanged(oldHook ? oldHook.deps : undefined, deps);
255
+
256
+ const hook = {
257
+ tag: RYUNIX_TYPES.RYUNIX_EFFECT,
258
+ effect: hasChanged ? effect : null,
259
+ cancel: hasChanged && oldHook && oldHook.cancel,
260
+ deps,
261
+ };
262
+
263
+ vars.wipFiber.hooks.push(hook);
264
+ vars.hookIndex++;
265
+ };
266
+
267
+ /**
268
+ * The function createContext creates a context object with a default value and methods to set and get
269
+ * the context value.
270
+ * @param defaultValue - The `defaultValue` parameter is the initial value that will be assigned to the
271
+ * `contextValue` variable if no value is provided when creating the context.
272
+ * @returns a context object.
273
+ */
274
+ const createContext = (defaultValue) => {
275
+ let contextValue = defaultValue || null;
276
+
277
+ const context = {
278
+ tag: RYUNIX_TYPES.RYUNIX_CONTEXT,
279
+ Value: contextValue,
280
+ Provider: null,
281
+ };
282
+
283
+ context.Provider = (value) => (context.Value = value);
284
+
285
+ return context;
286
+ };
91
287
 
92
288
  /**
93
289
  * The function creates a new DOM element based on the given fiber object and updates its properties.
@@ -99,7 +295,7 @@
99
295
  * `document.createElement(fiber.type)`. The function then calls the `updateDom` function to update the
100
296
  * properties of the newly created
101
297
  */
102
- function createDom(fiber) {
298
+ const createDom = (fiber) => {
103
299
  const dom =
104
300
  fiber.type == RYUNIX_TYPES.TEXT_ELEMENT
105
301
  ? document.createTextNode("")
@@ -108,7 +304,7 @@
108
304
  updateDom(dom, {}, fiber.props);
109
305
 
110
306
  return dom;
111
- }
307
+ };
112
308
 
113
309
  /**
114
310
  * The function updates the DOM by removing old event listeners and properties, and adding new ones
@@ -117,7 +313,7 @@
117
313
  * @param prevProps - An object representing the previous props (properties) of a DOM element.
118
314
  * @param nextProps - An object containing the new props that need to be updated in the DOM.
119
315
  */
120
- function updateDom(dom, prevProps, nextProps) {
316
+ const updateDom = (dom, prevProps, nextProps) => {
121
317
  Object.keys(prevProps)
122
318
  .filter(isEvent)
123
319
  .filter((key) => isGone(nextProps)(key) || isNew(prevProps, nextProps)(key))
@@ -158,9 +354,9 @@
158
354
  const eventType = name.toLowerCase().substring(2);
159
355
  dom.addEventListener(eventType, nextProps[name]);
160
356
  });
161
- }
357
+ };
162
358
 
163
- function DomStyle(dom, style) {
359
+ const DomStyle = (dom, style) => {
164
360
  dom.style = Object.keys(style).reduce((acc, styleName) => {
165
361
  const key = styleName.replace(reg, function (v) {
166
362
  return "-" + v.toLowerCase();
@@ -168,51 +364,24 @@
168
364
  acc += `${key}: ${style[styleName]};`;
169
365
  return acc;
170
366
  }, "");
171
- }
367
+ };
172
368
 
173
- /**
174
- * The function commits changes made to the virtual DOM to the actual DOM.
175
- */
176
- function commitRoot() {
177
- deletions.forEach(commitWork);
178
- commitWork(wipRoot.child);
179
- currentRoot = wipRoot;
180
- wipRoot = null;
181
- }
369
+ var Dom = /*#__PURE__*/Object.freeze({
370
+ __proto__: null,
371
+ DomStyle: DomStyle,
372
+ createDom: createDom,
373
+ updateDom: updateDom
374
+ });
182
375
 
183
376
  /**
184
- * The function cancels all effect hooks in a given fiber.
185
- * @param fiber - The "fiber" parameter is likely referring to a data structure used in React.js to
186
- * represent a component and its state. It contains information about the component's props, state, and
187
- * children, as well as metadata used by React to manage updates and rendering. The function
188
- * "cancelEffects" is likely intended
189
- */
190
- function cancelEffects(fiber) {
191
- if (fiber.hooks) {
192
- fiber.hooks
193
- .filter((hook) => hook.tag === RYUNIX_TYPES.RYUNIX_EFFECT && hook.cancel)
194
- .forEach((effectHook) => {
195
- effectHook.cancel();
196
- });
197
- }
198
- }
199
-
200
- /**
201
- * The function runs all effect hooks in a given fiber.
202
- * @param fiber - The "fiber" parameter is likely referring to a data structure used in the
203
- * implementation of a fiber-based reconciliation algorithm, such as the one used in React. A fiber
204
- * represents a unit of work that needs to be performed by the reconciliation algorithm, and it
205
- * contains information about a component and its children, as
377
+ * The function commits changes made to the virtual DOM to the actual DOM.
206
378
  */
207
- function runEffects(fiber) {
208
- if (fiber.hooks) {
209
- fiber.hooks
210
- .filter((hook) => hook.tag === RYUNIX_TYPES.RYUNIX_EFFECT && hook.effect)
211
- .forEach((effectHook) => {
212
- effectHook.cancel = effectHook.effect();
213
- });
214
- }
215
- }
379
+ const commitRoot = () => {
380
+ vars.deletions.forEach(commitWork);
381
+ commitWork(vars.wipRoot.child);
382
+ vars.currentRoot = vars.wipRoot;
383
+ vars.wipRoot = null;
384
+ };
216
385
 
217
386
  /**
218
387
  * The function commits changes made to the DOM based on the effect tag of the fiber.
@@ -221,7 +390,7 @@
221
390
  * parameter to commit the changes made during the reconciliation process to the actual DOM.
222
391
  * @returns The function does not return anything, it performs side effects by manipulating the DOM.
223
392
  */
224
- function commitWork(fiber) {
393
+ const commitWork = (fiber) => {
225
394
  if (!fiber) {
226
395
  return;
227
396
  }
@@ -251,7 +420,7 @@
251
420
 
252
421
  commitWork(fiber.child);
253
422
  commitWork(fiber.sibling);
254
- }
423
+ };
255
424
 
256
425
  /**
257
426
  * The function removes a fiber's corresponding DOM node from its parent node or recursively removes
@@ -260,140 +429,20 @@
260
429
  * application.
261
430
  * @param domParent - The parent DOM element from which the fiber's DOM element needs to be removed.
262
431
  */
263
- function commitDeletion(fiber, domParent) {
432
+ const commitDeletion = (fiber, domParent) => {
264
433
  if (fiber.dom) {
265
434
  domParent.removeChild(fiber.dom);
266
435
  } else {
267
436
  commitDeletion(fiber.child, domParent);
268
437
  }
269
- }
270
-
271
- /**
272
- * @deprecated use Ryunix.init(root) instead.
273
- *
274
- * @description The function creates a root container for a web application.
275
- * @example Ryunix.createRoot(document.getElementById("root")) -> <div id="root" />
276
- * @param root - The parameter `root` is likely referring to an HTML element that will serve as the
277
- * root or container for a web application or component. The `createRoot` function takes this element
278
- * as an argument and assigns it to a variable called `containerRoot`. This variable can then be used
279
- * to manipulate the contents
280
- *
281
- */
282
- function createRoot(root) {
283
- containerRoot = root;
284
- }
285
-
286
- /**
287
- * @description The function creates a reference to a DOM element with the specified ID. This will be used to initialize the app.
288
- * @example Ryunix.init("root") -> <div id="root" />
289
- * @param root - The parameter "root" is the id of the HTML element that will serve as the container
290
- * for the root element.
291
- */
292
- function init(root) {
293
- containerRoot = document.getElementById(root);
294
- }
295
-
296
- /**
297
- * The function renders an element into a container using a work-in-progress root.
298
- * @param element - The element parameter is the component or element that needs to be rendered in the
299
- * container. It could be a Ryunix component or a DOM element.
300
- * @param container - The container parameter is the DOM element where the rendered element will be
301
- * appended to. this parameter is optional if you use createRoot().
302
- */
303
- function render(element, container) {
304
- wipRoot = {
305
- dom: containerRoot || container,
306
- props: {
307
- children: [element],
308
- },
309
- alternate: currentRoot,
310
- };
311
- deletions = [];
312
- nextUnitOfWork = wipRoot;
313
- }
314
-
315
- /**
316
- * This function uses requestIdleCallback to perform work on a fiber tree until it is complete or the
317
- * browser needs to yield to other tasks.
318
- * @param deadline - The `deadline` parameter is an object that represents the amount of time the
319
- * browser has to perform work before it needs to handle other tasks. It has a `timeRemaining()` method
320
- * that returns the amount of time remaining before the deadline is reached. The `shouldYield` variable
321
- * is used to determine
322
- */
323
- function workLoop(deadline) {
324
- let shouldYield = false;
325
- while (nextUnitOfWork && !shouldYield) {
326
- nextUnitOfWork = performUnitOfWork(nextUnitOfWork);
327
- shouldYield = deadline.timeRemaining() < 1;
328
- }
329
-
330
- if (!nextUnitOfWork && wipRoot) {
331
- commitRoot();
332
- }
333
-
334
- requestIdleCallback(workLoop);
335
- }
336
-
337
- requestIdleCallback(workLoop);
338
-
339
- /**
340
- * The function performs a unit of work by updating either a function component or a host component and
341
- * returns the next fiber to be processed.
342
- * @param fiber - A fiber is a unit of work in Ryunix that represents a component and its state. It
343
- * contains information about the component's type, props, and children, as well as pointers to its
344
- * parent, child, and sibling fibers. The `performUnitOfWork` function takes a fiber as a parameter and
345
- * performs work
346
- * @returns The function `performUnitOfWork` returns the next fiber to be processed. If the current
347
- * fiber has a child, it returns the child. Otherwise, it looks for the next sibling of the current
348
- * fiber. If there are no more siblings, it goes up the tree to the parent and looks for the next
349
- * sibling of the parent. The function returns `null` if there are no more fibers to process.
350
- */
351
- function performUnitOfWork(fiber) {
352
- const isFunctionComponent = fiber.type instanceof Function;
353
- if (isFunctionComponent) {
354
- updateFunctionComponent(fiber);
355
- } else {
356
- updateHostComponent(fiber);
357
- }
358
- if (fiber.child) {
359
- return fiber.child;
360
- }
361
- let nextFiber = fiber;
362
- while (nextFiber) {
363
- if (nextFiber.sibling) {
364
- return nextFiber.sibling;
365
- }
366
- nextFiber = nextFiber.parent;
367
- }
368
- }
369
-
370
- /**
371
- * This function updates a function component by setting up a work-in-progress fiber, resetting the
372
- * hook index, creating an empty hooks array, rendering the component, and reconciling its children.
373
- * @param fiber - The fiber parameter is an object that represents a node in the fiber tree. It
374
- * contains information about the component, its props, state, and children. In this function, it is
375
- * used to update the state of the component and its children.
376
- */
377
- function updateFunctionComponent(fiber) {
378
- wipFiber = fiber;
379
- hookIndex = 0;
380
- wipFiber.hooks = [];
381
- const children = [fiber.type(fiber.props)];
382
- reconcileChildren(fiber, children);
383
- }
438
+ };
384
439
 
385
- /**
386
- * This function updates a host component's DOM element and reconciles its children.
387
- * @param fiber - A fiber is a unit of work in Ryunix that represents a component and its state. It
388
- * contains information about the component's type, props, and children, as well as pointers to other
389
- * fibers in the tree.
390
- */
391
- function updateHostComponent(fiber) {
392
- if (!fiber.dom) {
393
- fiber.dom = createDom(fiber);
394
- }
395
- reconcileChildren(fiber, fiber.props.children.flat());
396
- }
440
+ var Commits = /*#__PURE__*/Object.freeze({
441
+ __proto__: null,
442
+ commitDeletion: commitDeletion,
443
+ commitRoot: commitRoot,
444
+ commitWork: commitWork
445
+ });
397
446
 
398
447
  /**
399
448
  * This function reconciles the children of a fiber node with a new set of elements, creating new
@@ -404,14 +453,14 @@
404
453
  * @param elements - an array of elements representing the new children to be rendered in the current
405
454
  * fiber's subtree
406
455
  */
407
- function reconcileChildren(wipFiber, elements) {
456
+ const reconcileChildren = (wipFiber, elements) => {
408
457
  let index = 0;
409
458
  let oldFiber = wipFiber.alternate && wipFiber.alternate.child;
410
- let prevSibling = null;
459
+ let prevSibling;
411
460
 
412
461
  while (index < elements.length || oldFiber != null) {
413
462
  const element = elements[index];
414
- let newFiber = null;
463
+ let newFiber;
415
464
 
416
465
  const sameType = oldFiber && element && element.type == oldFiber.type;
417
466
 
@@ -437,7 +486,7 @@
437
486
  }
438
487
  if (oldFiber && !sameType) {
439
488
  oldFiber.effectTag = EFFECT_TAGS.DELETION;
440
- deletions.push(oldFiber);
489
+ vars.deletions.push(oldFiber);
441
490
  }
442
491
 
443
492
  if (oldFiber) {
@@ -453,144 +502,118 @@
453
502
  prevSibling = newFiber;
454
503
  index++;
455
504
  }
456
- }
505
+ };
506
+
507
+ var Reconciler = /*#__PURE__*/Object.freeze({
508
+ __proto__: null,
509
+ reconcileChildren: reconcileChildren
510
+ });
457
511
 
458
512
  /**
459
- * The function createContext creates a context object with a default value and methods to set and get
460
- * the context value.
461
- * @param defaultValue - The `defaultValue` parameter is the initial value that will be assigned to the
462
- * `contextValue` variable if no value is provided when creating the context.
463
- * @returns a context object.
513
+ * This function updates a function component by setting up a work-in-progress fiber, resetting the
514
+ * hook index, creating an empty hooks array, rendering the component, and reconciling its children.
515
+ * @param fiber - The fiber parameter is an object that represents a node in the fiber tree. It
516
+ * contains information about the component, its props, state, and children. In this function, it is
517
+ * used to update the state of the component and its children.
464
518
  */
465
- function createContext(defaultValue) {
466
- let contextValue = defaultValue || null;
467
-
468
- const context = {
469
- tag: RYUNIX_TYPES.RYUNIX_CONTEXT,
470
- Value: contextValue,
471
- Provider: null,
472
- };
473
-
474
- context.Provider = (value) => (context.Value = value);
475
-
476
- return context;
477
- }
478
-
479
- function Fragments(props) {
480
- if (props.style) {
481
- throw new Error("The style attribute is not supported");
482
- }
483
- if (props.className === "") {
484
- throw new Error("className cannot be empty.");
485
- }
486
- return createElement("div", props, props.children);
487
- }
488
-
489
- // Hooks
519
+ const updateFunctionComponent = (fiber) => {
520
+ vars.wipFiber = fiber;
521
+ vars.hookIndex = 0;
522
+ vars.wipFiber.hooks = [];
523
+ const children = [fiber.type(fiber.props)];
524
+ reconcileChildren(fiber, children);
525
+ };
490
526
 
491
527
  /**
492
- * The function `useContext` is used to read and subscribe to context from your component.
493
- * @param ref - The `ref` parameter is a reference to a context object.
494
- * @returns The `Value` property of the `hook` object is being returned.
528
+ * This function updates a host component's DOM element and reconciles its children.
529
+ * @param fiber - A fiber is a unit of work in Ryunix that represents a component and its state. It
530
+ * contains information about the component's type, props, and children, as well as pointers to other
531
+ * fibers in the tree.
495
532
  */
496
- function useContext(ref) {
497
- hookIndex++;
498
-
499
- const oldHook =
500
- wipFiber.alternate &&
501
- wipFiber.alternate.hooks &&
502
- wipFiber.alternate.hooks[hookIndex];
503
-
504
- const hasOld = oldHook ? oldHook : undefined;
505
- const Context = hasOld ? hasOld : ref;
506
- const hook = {
507
- ...Context,
508
- };
509
-
510
- wipFiber.hooks.push(hook);
533
+ const updateHostComponent = (fiber) => {
534
+ if (!fiber.dom) {
535
+ fiber.dom = createDom(fiber);
536
+ }
537
+ reconcileChildren(fiber, fiber.props.children.flat());
538
+ };
511
539
 
512
- return hook.Value;
513
- }
540
+ var Components = /*#__PURE__*/Object.freeze({
541
+ __proto__: null,
542
+ updateFunctionComponent: updateFunctionComponent,
543
+ updateHostComponent: updateHostComponent
544
+ });
514
545
 
515
546
  /**
516
- * @description The function creates a state.
517
- * @param initial - The initial value of the state for the hook.
518
- * @returns The `useStore` function returns an array with two elements: the current state value and a
519
- * `setState` function that can be used to update the state.
547
+ * This function uses requestIdleCallback to perform work on a fiber tree until it is complete or the
548
+ * browser needs to yield to other tasks.
549
+ * @param deadline - The `deadline` parameter is an object that represents the amount of time the
550
+ * browser has to perform work before it needs to handle other tasks. It has a `timeRemaining()` method
551
+ * that returns the amount of time remaining before the deadline is reached. The `shouldYield` variable
552
+ * is used to determine
520
553
  */
521
- function useStore(initial) {
522
- const oldHook =
523
- wipFiber.alternate &&
524
- wipFiber.alternate.hooks &&
525
- wipFiber.alternate.hooks[hookIndex];
526
- const hook = {
527
- state: oldHook ? oldHook.state : initial,
528
- queue: [],
529
- };
554
+ const workLoop = (deadline) => {
555
+ let shouldYield = false;
556
+ while (vars.nextUnitOfWork && !shouldYield) {
557
+ vars.nextUnitOfWork = performUnitOfWork(vars.nextUnitOfWork);
558
+ shouldYield = deadline.timeRemaining() < 1;
559
+ }
530
560
 
531
- const actions = oldHook ? oldHook.queue : [];
532
- actions.forEach((action) => {
533
- hook.state =
534
- typeof action === STRINGS.function ? action(hook.state) : action;
535
- });
561
+ if (!vars.nextUnitOfWork && vars.wipRoot) {
562
+ commitRoot();
563
+ }
536
564
 
537
- /**
538
- * The function `setState` updates the state of a component in Ryunix by adding an action to a queue
539
- * and setting up a new work-in-progress root.
540
- * @param action - The `action` parameter is an object that represents a state update to be performed
541
- * on a component. It contains information about the type of update to be performed and any new data
542
- * that needs to be applied to the component's state.
543
- */
544
- const setState = (action) => {
545
- hook.queue.push(action);
546
- wipRoot = {
547
- dom: currentRoot.dom,
548
- props: currentRoot.props,
549
- alternate: currentRoot,
550
- };
551
- nextUnitOfWork = wipRoot;
552
- deletions = [];
553
- };
565
+ requestIdleCallback(workLoop);
566
+ };
554
567
 
555
- wipFiber.hooks.push(hook);
556
- hookIndex++;
557
- return [hook.state, setState];
558
- }
568
+ requestIdleCallback(workLoop);
559
569
 
560
570
  /**
561
- * This is a function that creates a hook for managing side effects in Ryunix components.
562
- * @param effect - The effect function that will be executed after the component has rendered or when
563
- * the dependencies have changed. It can perform side effects such as fetching data, updating the DOM,
564
- * or subscribing to events.
565
- * @param deps - An array of dependencies that the effect depends on. If any of the dependencies change
566
- * between renders, the effect will be re-run. If the array is empty, the effect will only run once on
567
- * mount and never again.
571
+ * The function performs a unit of work by updating either a function component or a host component and
572
+ * returns the next fiber to be processed.
573
+ * @param fiber - A fiber is a unit of work in Ryunix that represents a component and its state. It
574
+ * contains information about the component's type, props, and children, as well as pointers to its
575
+ * parent, child, and sibling fibers. The `performUnitOfWork` function takes a fiber as a parameter and
576
+ * performs work
577
+ * @returns The function `performUnitOfWork` returns the next fiber to be processed. If the current
578
+ * fiber has a child, it returns the child. Otherwise, it looks for the next sibling of the current
579
+ * fiber. If there are no more siblings, it goes up the tree to the parent and looks for the next
580
+ * sibling of the parent. The function returns `null` if there are no more fibers to process.
568
581
  */
569
- function useEffect(effect, deps) {
570
- const oldHook =
571
- wipFiber.alternate &&
572
- wipFiber.alternate.hooks &&
573
- wipFiber.alternate.hooks[hookIndex];
574
-
575
- const hasChanged = hasDepsChanged(oldHook ? oldHook.deps : undefined, deps);
576
-
577
- const hook = {
578
- tag: RYUNIX_TYPES.RYUNIX_EFFECT,
579
- effect: hasChanged ? effect : null,
580
- cancel: hasChanged && oldHook && oldHook.cancel,
581
- deps,
582
- };
582
+ const performUnitOfWork = (fiber) => {
583
+ const isFunctionComponent = fiber.type instanceof Function;
584
+ if (isFunctionComponent) {
585
+ updateFunctionComponent(fiber);
586
+ } else {
587
+ updateHostComponent(fiber);
588
+ }
589
+ if (fiber.child) {
590
+ return fiber.child;
591
+ }
592
+ let nextFiber = fiber;
593
+ while (nextFiber) {
594
+ if (nextFiber.sibling) {
595
+ return nextFiber.sibling;
596
+ }
597
+ nextFiber = nextFiber.parent;
598
+ }
599
+ };
583
600
 
584
- wipFiber.hooks.push(hook);
585
- hookIndex++;
586
- }
601
+ var Workers = /*#__PURE__*/Object.freeze({
602
+ __proto__: null,
603
+ performUnitOfWork: performUnitOfWork,
604
+ workLoop: workLoop
605
+ });
587
606
 
588
607
  var Ryunix = {
589
608
  createElement,
590
609
  render,
591
- createRoot,
592
610
  init,
593
611
  Fragments,
612
+ Dom,
613
+ Workers,
614
+ Reconciler,
615
+ Components,
616
+ Commits,
594
617
  };
595
618
 
596
619
  window.Ryunix = Ryunix;