@unsetsoft/ryunixjs 0.2.15-nightly.0 → 0.2.15-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.
Files changed (2) hide show
  1. package/lib/dom.js +47 -27
  2. package/package.json +1 -1
package/lib/dom.js CHANGED
@@ -136,7 +136,40 @@ function commitRoot() {
136
136
  commitWork(wipRoot.child);
137
137
  currentRoot = wipRoot;
138
138
  wipRoot = null;
139
- pendingEffects.forEach((it) => it()); // call pending effects after render
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
+ }
140
173
  }
141
174
 
142
175
  /**
@@ -161,11 +194,15 @@ function commitWork(fiber) {
161
194
  if (fiber.dom != null) {
162
195
  domParent.appendChild(fiber.dom);
163
196
  }
197
+ runEffects(wipFiber);
164
198
  } else if (fiber.effectTag === "UPDATE") {
199
+ cancelEffects(wipFiber);
165
200
  if (fiber.dom != null) {
166
201
  updateDom(fiber.dom, fiber.alternate.props, fiber.props);
167
202
  }
203
+ runEffects(wipFiber);
168
204
  } else if (fiber.effectTag === "DELETION") {
205
+ cancelEffects(wipFiber);
169
206
  commitDeletion(fiber, domParent);
170
207
  return;
171
208
  }
@@ -276,35 +313,10 @@ requestIdleCallback(workLoop);
276
313
  * fiber. If there are no more siblings, it goes up the tree to the parent and looks for the next
277
314
  * sibling of the parent. The function returns `null` if there are no more fibers to process.
278
315
  */
279
-
280
- let pendingEffects = [];
281
316
  function performUnitOfWork(fiber) {
282
317
  const isFunctionComponent = fiber.type instanceof Function;
283
318
  if (isFunctionComponent) {
284
319
  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
- });
308
320
  } else {
309
321
  updateHostComponent(fiber);
310
322
  }
@@ -481,9 +493,17 @@ const hasDepsChanged = (prevDeps, nextDeps) =>
481
493
  * mount and never again.
482
494
  */
483
495
  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
+
484
503
  const hook = {
485
504
  tag: "effect",
486
- effect,
505
+ effect: hasChanged ? effect : null,
506
+ cancel: hasChanged && oldHook && oldHook.cancel,
487
507
  deps,
488
508
  };
489
509
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unsetsoft/ryunixjs",
3
- "version": "0.2.15-nightly.0",
3
+ "version": "0.2.15-nightly.1",
4
4
  "license": "MIT",
5
5
  "main": "./dist/Ryunix.js",
6
6
  "private": false,