@unsetsoft/ryunixjs 0.2.0-beta.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/lib/dom.js +101 -20
  2. package/lib/main.js +2 -2
  3. package/package.json +2 -1
package/lib/dom.js CHANGED
@@ -19,9 +19,11 @@ function createElement(type, props, ...children) {
19
19
  type,
20
20
  props: {
21
21
  ...props,
22
- children: children.map((child) =>
23
- typeof child === "object" ? child : createTextElement(child)
24
- ),
22
+ children: children
23
+ .flat()
24
+ .map((child) =>
25
+ typeof child === "object" ? child : createTextElement(child)
26
+ ),
25
27
  },
26
28
  };
27
29
  }
@@ -117,9 +119,43 @@ function commitRoot() {
117
119
  wipRoot = null;
118
120
  }
119
121
 
122
+ /**
123
+ * The function cancels all effect hooks in a given fiber.
124
+ * @param fiber - The "fiber" parameter is likely referring to a data structure used in React.js to
125
+ * represent a component and its state. It contains information about the component's props, state, and
126
+ * children, as well as metadata used by React to manage updates and rendering. The function
127
+ * "cancelEffects" is likely intended
128
+ */
129
+ function cancelEffects(fiber) {
130
+ if (fiber.hooks) {
131
+ fiber.hooks
132
+ .filter((hook) => hook.tag === "effect" && hook.cancel)
133
+ .forEach((effectHook) => {
134
+ effectHook.cancel();
135
+ });
136
+ }
137
+ }
138
+
139
+ /**
140
+ * The function runs all effect hooks in a given fiber.
141
+ * @param fiber - The "fiber" parameter is likely referring to a data structure used in the
142
+ * implementation of a fiber-based reconciliation algorithm, such as the one used in React. A fiber
143
+ * represents a unit of work that needs to be performed by the reconciliation algorithm, and it
144
+ * contains information about a component and its children, as
145
+ */
146
+ function runEffects(fiber) {
147
+ if (fiber.hooks) {
148
+ fiber.hooks
149
+ .filter((hook) => hook.tag === "effect" && hook.effect)
150
+ .forEach((effectHook) => {
151
+ effectHook.cancel = effectHook.effect();
152
+ });
153
+ }
154
+ }
155
+
120
156
  /**
121
157
  * The function commits changes made to the DOM based on the effect tag of the fiber.
122
- * @param fiber - A fiber is a unit of work in React's reconciliation process. It represents a
158
+ * @param fiber - A fiber is a unit of work in Ryunix's reconciliation process. It represents a
123
159
  * component and its state at a particular point in time. The `commitWork` function takes a fiber as a
124
160
  * parameter to commit the changes made during the reconciliation process to the actual DOM.
125
161
  * @returns The function does not return anything, it performs side effects by manipulating the DOM.
@@ -135,11 +171,19 @@ function commitWork(fiber) {
135
171
  }
136
172
  const domParent = domParentFiber.dom;
137
173
 
138
- if (fiber.effectTag === "PLACEMENT" && fiber.dom != null) {
139
- domParent.appendChild(fiber.dom);
140
- } else if (fiber.effectTag === "UPDATE" && fiber.dom != null) {
141
- updateDom(fiber.dom, fiber.alternate.props, fiber.props);
174
+ if (fiber.effectTag === "PLACEMENT") {
175
+ if (fiber.dom != null) {
176
+ domParent.appendChild(fiber.dom);
177
+ }
178
+ runEffects(fiber);
179
+ } else if (fiber.effectTag === "UPDATE") {
180
+ cancelEffects(fiber);
181
+ if (fiber.dom != null) {
182
+ updateDom(fiber.dom, fiber.alternate.props, fiber.props);
183
+ }
184
+ runEffects(fiber);
142
185
  } else if (fiber.effectTag === "DELETION") {
186
+ cancelEffects(fiber);
143
187
  commitDeletion(fiber, domParent);
144
188
  }
145
189
 
@@ -150,7 +194,7 @@ function commitWork(fiber) {
150
194
  /**
151
195
  * The function removes a fiber's corresponding DOM node from its parent node or recursively removes
152
196
  * its child's DOM node until it finds a node to remove.
153
- * @param fiber - a fiber node in a fiber tree, which represents a component or an element in the React
197
+ * @param fiber - a fiber node in a fiber tree, which represents a component or an element in the Ryunix
154
198
  * application.
155
199
  * @param domParent - The parent DOM element from which the fiber's DOM element needs to be removed.
156
200
  */
@@ -178,7 +222,7 @@ function createRoot(root) {
178
222
  /**
179
223
  * The function renders an element into a container using a work-in-progress root.
180
224
  * @param element - The element parameter is the component or element that needs to be rendered in the
181
- * container. It could be a React component or a DOM element.
225
+ * container. It could be a Ryunix component or a DOM element.
182
226
  * @param container - The container parameter is the DOM element where the rendered element will be
183
227
  * appended to. this parameter is optional if you use createRoot().
184
228
  */
@@ -226,7 +270,7 @@ requestIdleCallback(workLoop);
226
270
  /**
227
271
  * The function performs a unit of work by updating either a function component or a host component and
228
272
  * returns the next fiber to be processed.
229
- * @param fiber - A fiber is a unit of work in React that represents a component and its state. It
273
+ * @param fiber - A fiber is a unit of work in Ryunix that represents a component and its state. It
230
274
  * contains information about the component's type, props, and children, as well as pointers to its
231
275
  * parent, child, and sibling fibers. The `performUnitOfWork` function takes a fiber as a parameter and
232
276
  * performs work
@@ -274,7 +318,7 @@ function updateFunctionComponent(fiber) {
274
318
 
275
319
  /**
276
320
  * This function updates a host component's DOM element and reconciles its children.
277
- * @param fiber - A fiber is a unit of work in React that represents a component and its state. It
321
+ * @param fiber - A fiber is a unit of work in Ryunix that represents a component and its state. It
278
322
  * contains information about the component's type, props, and children, as well as pointers to other
279
323
  * fibers in the tree.
280
324
  */
@@ -282,7 +326,7 @@ function updateHostComponent(fiber) {
282
326
  if (!fiber.dom) {
283
327
  fiber.dom = createDom(fiber);
284
328
  }
285
- reconcileChildren(fiber, fiber.props.children);
329
+ reconcileChildren(fiber, fiber.props.children.flat());
286
330
  }
287
331
 
288
332
  /**
@@ -348,7 +392,7 @@ function reconcileChildren(wipFiber, elements) {
348
392
  // Hooks
349
393
 
350
394
  /**
351
- * The function creates a state hook for a React-like framework.
395
+ * The function creates a state hook for a Ryunix-like framework.
352
396
  * @param initial - The initial value of the state for the hook.
353
397
  * @returns The `useStore` function returns an array with two elements: the current state value and a
354
398
  * `setState` function that can be used to update the state.
@@ -365,12 +409,11 @@ function useStore(initial) {
365
409
 
366
410
  const actions = oldHook ? oldHook.queue : [];
367
411
  actions.forEach((action) => {
368
- console.log(action);
369
- hook.state = action(hook.state);
412
+ hook.state = typeof action === "function" ? action(hook.state) : action;
370
413
  });
371
414
 
372
415
  /**
373
- * The function `setState` updates the state of a component in React by adding an action to a queue
416
+ * The function `setState` updates the state of a component in Ryunix by adding an action to a queue
374
417
  * and setting up a new work-in-progress root.
375
418
  * @param action - The `action` parameter is an object that represents a state update to be performed
376
419
  * on a component. It contains information about the type of update to be performed and any new data
@@ -392,8 +435,46 @@ function useStore(initial) {
392
435
  return [hook.state, setState];
393
436
  }
394
437
 
395
- function Fragment() {
396
- return null;
438
+ /**
439
+ * The function checks if the previous dependencies are different from the next dependencies.
440
+ * @param prevDeps - The previous dependencies, which could be an array of values or objects that a
441
+ * function or component depends on.
442
+ * @param nextDeps - `nextDeps` is an array of dependencies that are being checked for changes. These
443
+ * dependencies are typically used in React's `useEffect` and `useCallback` hooks to determine when a
444
+ * component should re-render or when a function should be re-created.
445
+ */
446
+ const hasDepsChanged = (prevDeps, nextDeps) =>
447
+ !prevDeps ||
448
+ !nextDeps ||
449
+ prevDeps.length !== nextDeps.length ||
450
+ prevDeps.some((dep, index) => dep !== nextDeps[index]);
451
+
452
+ /**
453
+ * This is a function that creates a hook for managing side effects in React components.
454
+ * @param effect - The effect function that will be executed after the component has rendered or when
455
+ * the dependencies have changed. It can perform side effects such as fetching data, updating the DOM,
456
+ * or subscribing to events.
457
+ * @param deps - An array of dependencies that the effect depends on. If any of the dependencies change
458
+ * between renders, the effect will be re-run. If the array is empty, the effect will only run once on
459
+ * mount and never again.
460
+ */
461
+ function useEffect(effect, deps) {
462
+ const oldHook =
463
+ wipFiber.alternate &&
464
+ wipFiber.alternate.hooks &&
465
+ wipFiber.alternate.hooks[hookIndex];
466
+
467
+ const hasChanged = hasDepsChanged(oldHook ? oldHook.deps : undefined, deps);
468
+
469
+ const hook = {
470
+ tag: "effect",
471
+ effect: hasChanged ? effect : null,
472
+ cancel: hasChanged && oldHook && oldHook.cancel,
473
+ deps,
474
+ };
475
+
476
+ wipFiber.hooks.push(hook);
477
+ hookIndex++;
397
478
  }
398
479
 
399
480
  // export
@@ -405,5 +486,5 @@ export {
405
486
  createRoot,
406
487
  // Hooks
407
488
  useStore,
408
- Fragment,
489
+ useEffect,
409
490
  };
package/lib/main.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import { createElement, render, createRoot } from "./dom";
2
- import { Fragment, useStore } from "./dom";
2
+ import { useStore, useEffect } from "./dom";
3
3
 
4
4
  // Hooks
5
- export { useStore, Fragment };
5
+ export { useStore, useEffect };
6
6
 
7
7
  // General
8
8
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unsetsoft/ryunixjs",
3
- "version": "0.2.0-beta.1",
3
+ "version": "0.2.0",
4
4
  "license": "MIT",
5
5
  "main": "./dist/Ryunix.js",
6
6
  "private": false,
@@ -13,6 +13,7 @@
13
13
  "postinstall": "yarn build"
14
14
  },
15
15
  "dependencies": {
16
+ "@unsetsoft/ryunix-router": "^0.2.0",
16
17
  "rollup": "3.24.0"
17
18
  },
18
19
  "engines": {