@unsetsoft/ryunixjs 0.2.14 → 0.2.15-nightly.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 (2) hide show
  1. package/lib/dom.js +27 -47
  2. package/package.json +1 -1
package/lib/dom.js CHANGED
@@ -136,40 +136,7 @@ function commitRoot() {
136
136
  commitWork(wipRoot.child);
137
137
  currentRoot = wipRoot;
138
138
  wipRoot = null;
139
- }
140
-
141
- /**
142
- * The function cancels all effect hooks in a given fiber.
143
- * @param fiber - The "fiber" parameter is likely referring to a data structure used in React.js to
144
- * represent a component and its state. It contains information about the component's props, state, and
145
- * children, as well as metadata used by React to manage updates and rendering. The function
146
- * "cancelEffects" is likely intended
147
- */
148
- function cancelEffects(fiber) {
149
- if (fiber.hooks) {
150
- fiber.hooks
151
- .filter((hook) => hook.tag === "effect" && hook.cancel)
152
- .forEach((effectHook) => {
153
- effectHook.cancel();
154
- });
155
- }
156
- }
157
-
158
- /**
159
- * The function runs all effect hooks in a given fiber.
160
- * @param fiber - The "fiber" parameter is likely referring to a data structure used in the
161
- * implementation of a fiber-based reconciliation algorithm, such as the one used in React. A fiber
162
- * represents a unit of work that needs to be performed by the reconciliation algorithm, and it
163
- * contains information about a component and its children, as
164
- */
165
- function runEffects(fiber) {
166
- if (fiber.hooks) {
167
- fiber.hooks
168
- .filter((hook) => hook.tag === "effect" && hook.effect)
169
- .forEach((effectHook) => {
170
- effectHook.cancel = effectHook.effect();
171
- });
172
- }
139
+ pendingEffects.forEach((it) => it()); // call pending effects after render
173
140
  }
174
141
 
175
142
  /**
@@ -194,15 +161,11 @@ function commitWork(fiber) {
194
161
  if (fiber.dom != null) {
195
162
  domParent.appendChild(fiber.dom);
196
163
  }
197
- runEffects(fiber);
198
164
  } else if (fiber.effectTag === "UPDATE") {
199
- cancelEffects(fiber);
200
165
  if (fiber.dom != null) {
201
166
  updateDom(fiber.dom, fiber.alternate.props, fiber.props);
202
167
  }
203
- runEffects(fiber);
204
168
  } else if (fiber.effectTag === "DELETION") {
205
- cancelEffects(fiber);
206
169
  commitDeletion(fiber, domParent);
207
170
  return;
208
171
  }
@@ -313,10 +276,35 @@ requestIdleCallback(workLoop);
313
276
  * fiber. If there are no more siblings, it goes up the tree to the parent and looks for the next
314
277
  * sibling of the parent. The function returns `null` if there are no more fibers to process.
315
278
  */
279
+
280
+ let pendingEffects = [];
316
281
  function performUnitOfWork(fiber) {
317
282
  const isFunctionComponent = fiber.type instanceof Function;
318
283
  if (isFunctionComponent) {
319
284
  updateFunctionComponent(fiber);
285
+
286
+ Object.keys(fiber.hooks)
287
+ .filter((hookIndex) => fiber.hooks[hookIndex].tag === "effect")
288
+ .forEach((hookIndex) => {
289
+ const oldHook =
290
+ fiber.alternate &&
291
+ fiber.alternate.hooks &&
292
+ fiber.alternate.hooks[hookIndex];
293
+
294
+ const hook = fiber.hooks[hookIndex];
295
+ const depsChanged = (prev, next) => (_, index) =>
296
+ prev[index] !== next[index];
297
+ if (
298
+ (hook.deps.length === 0 && !oldHook) ||
299
+ (oldHook &&
300
+ (oldHook.deps.length !== hook.deps.length ||
301
+ (oldHook &&
302
+ hook.deps.filter(depsChanged(oldHook.deps, hook.deps))
303
+ .length !== 0)))
304
+ ) {
305
+ pendingEffects.push(hook.fn);
306
+ }
307
+ });
320
308
  } else {
321
309
  updateHostComponent(fiber);
322
310
  }
@@ -493,17 +481,9 @@ const hasDepsChanged = (prevDeps, nextDeps) =>
493
481
  * mount and never again.
494
482
  */
495
483
  function useEffect(effect, deps) {
496
- const oldHook =
497
- wipFiber.alternate &&
498
- wipFiber.alternate.hooks &&
499
- wipFiber.alternate.hooks[hookIndex];
500
-
501
- const hasChanged = hasDepsChanged(oldHook ? oldHook.deps : undefined, deps);
502
-
503
484
  const hook = {
504
485
  tag: "effect",
505
- effect: hasChanged ? effect : null,
506
- cancel: hasChanged && oldHook && oldHook.cancel,
486
+ effect,
507
487
  deps,
508
488
  };
509
489
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unsetsoft/ryunixjs",
3
- "version": "0.2.14",
3
+ "version": "0.2.15-nightly.0",
4
4
  "license": "MIT",
5
5
  "main": "./dist/Ryunix.js",
6
6
  "private": false,