@xaendar/signals 0.9.2 → 0.9.4

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.
@@ -290,7 +290,7 @@ var Computed = class Computed {
290
290
  return;
291
291
  }
292
292
  this.#state = newState;
293
- if (this.#state === "dirty" || this.#state === "checked") this.#sinks.forEach((sink) => sink instanceof Computed ? sink.setState("checked", PRIVATE) : sink.notify(PRIVATE));
293
+ if (this.#state === "dirty" || this.#state === "checked") for (const sink of this.#sinks) sink instanceof Computed ? sink.setState("checked", PRIVATE) : sink.notify(PRIVATE);
294
294
  }
295
295
  /**
296
296
  * Registers a new sink (a `Computed` or `Watcher` that directly depends on
@@ -306,7 +306,7 @@ var Computed = class Computed {
306
306
  */
307
307
  addSink(sink, symbol) {
308
308
  assertPrivateContext(symbol);
309
- if (this.#sinks.size === 0) this.#sources.forEach((source) => source.addSink(this, PRIVATE));
309
+ if (this.#sinks.size === 0) for (const source of this.#sources) source.addSink(this, PRIVATE);
310
310
  this.#sinks.add(sink);
311
311
  }
312
312
  /**
@@ -323,7 +323,7 @@ var Computed = class Computed {
323
323
  removeSink(sink, symbol) {
324
324
  assertPrivateContext(symbol);
325
325
  this.#sinks.delete(sink);
326
- if (this.#sinks.size === 0) this.#sources.forEach((source) => source.removeSink(this, PRIVATE));
326
+ if (this.#sinks.size === 0) for (const source of this.#sources) source.removeSink(this, PRIVATE);
327
327
  }
328
328
  /**
329
329
  * Recursively walks the source graph depth-first to find the deepest,
@@ -358,7 +358,7 @@ var Computed = class Computed {
358
358
  * @see Signal algorithms — "Algorithm: recalculate dirty computed Signal"
359
359
  */
360
360
  #computeValue() {
361
- this.#sources.forEach((source) => source.removeSink(this, PRIVATE));
361
+ for (const source of this.#sources) source.removeSink(this, PRIVATE);
362
362
  this.#sources.clear();
363
363
  pushComputed(this);
364
364
  this.setState("computing", PRIVATE);
@@ -376,7 +376,8 @@ var Computed = class Computed {
376
376
  }
377
377
  const outcome = this.#setValue(newValue);
378
378
  this.setState("clean", PRIVATE);
379
- outcome === "dirty" ? this.#sinks.forEach((sink) => sink instanceof Computed ? sink.setState("dirty", PRIVATE) : sink.notify(PRIVATE)) : this.#propagateClean();
379
+ if (outcome === "dirty") for (const sink of this.#sinks) sink instanceof Computed ? sink.setState("dirty", PRIVATE) : sink.notify(PRIVATE);
380
+ else this.#propagateClean();
380
381
  }
381
382
  /**
382
383
  * Implements the "set Signal value" algorithm.
@@ -427,12 +428,17 @@ var Computed = class Computed {
427
428
  * @see Signal algorithms — "Algorithm: recalculate dirty computed Signal"
428
429
  */
429
430
  #propagateClean() {
430
- [...this.#sinks].filter((sink) => sink instanceof Computed && sink.#state === "checked").forEach((sink) => {
431
- if ([...sink.#sources].every((source) => !(source instanceof Computed) || source.#state === "clean")) {
431
+ for (const sink of this.#sinks) if (sink instanceof Computed && sink.#state === "checked") {
432
+ let allSourcesClean = true;
433
+ for (const source of sink.#sources) if (source instanceof Computed && source.#state !== "clean") {
434
+ allSourcesClean = false;
435
+ break;
436
+ }
437
+ if (allSourcesClean) {
432
438
  sink.#state = "clean";
433
439
  sink.#propagateClean();
434
440
  }
435
- });
441
+ }
436
442
  }
437
443
  /**
438
444
  * Type guard that checks whether a value is a boxed error object.
@@ -581,7 +587,7 @@ var State = class {
581
587
  if (GLOBAL_STATE.frozen) throw new Error("Cannot set value while signals are frozen");
582
588
  if (!this.#equals.call(this, this.#value, newValue)) {
583
589
  this.#value = newValue;
584
- this.#sinks.forEach((sink) => sink instanceof Computed ? sink.setState("dirty", PRIVATE) : sink.notify(PRIVATE));
590
+ for (const sink of this.#sinks) sink instanceof Computed ? sink.setState("dirty", PRIVATE) : sink.notify(PRIVATE);
585
591
  }
586
592
  }
587
593
  /**
@@ -725,11 +731,13 @@ var Watcher = class {
725
731
  * @see Signal algorithms — 'Method: Signal.subtle.Watcher.prototype.getPending()'
726
732
  */
727
733
  getPending() {
728
- return [...this.#signals].filter((signal) => {
729
- if (!(signal instanceof Computed)) return false;
734
+ const filteredSignals = new Array();
735
+ for (const signal of this.#signals) {
736
+ if (!(signal instanceof Computed)) continue;
730
737
  const state = signal.getState(PRIVATE);
731
- return state === "dirty" || state === "checked";
732
- });
738
+ if (state === "dirty" || state === "checked") filteredSignals.push(signal);
739
+ }
740
+ return filteredSignals;
733
741
  }
734
742
  /**
735
743
  * Adds the given Signals to the watched set and transitions the Watcher to
@@ -749,11 +757,12 @@ var Watcher = class {
749
757
  */
750
758
  watch(...signals) {
751
759
  if (GLOBAL_STATE.frozen) throw new Error("Cannot watch signals while frozen");
752
- signals.forEach((signal) => {
760
+ for (let i = 0; i < signals.length; i++) {
761
+ const signal = signals[i];
753
762
  if (this.#signals.has(signal)) throw new Error("Cannot watch a signal that is already being watched");
754
763
  this.#signals.add(signal);
755
764
  signal.addSink(this, PRIVATE);
756
- });
765
+ }
757
766
  this.setState("watching", PRIVATE);
758
767
  }
759
768
  /**
@@ -778,11 +787,12 @@ var Watcher = class {
778
787
  */
779
788
  unwatch(...signals) {
780
789
  if (GLOBAL_STATE.frozen) throw new Error("Cannot unwatch signals while frozen");
781
- signals.forEach((signal) => {
790
+ for (let i = 0; i < signals.length; i++) {
791
+ const signal = signals[i];
782
792
  if (!this.#signals.has(signal)) throw new Error("Cannot unwatch a signal that is not being watched");
783
793
  this.#signals.delete(signal);
784
794
  signal.removeSink(this, PRIVATE);
785
- });
795
+ }
786
796
  if (!this.#signals.size) this.setState("waiting", PRIVATE);
787
797
  }
788
798
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xaendar/signals",
3
- "version": "0.9.2",
3
+ "version": "0.9.4",
4
4
  "description": "A library implementing a reactive system for Xaendar framework. This signal implementation is based on the TC39 Stage1 proposal for reactive primitives.",
5
5
  "sideEffects": false,
6
6
  "type": "module",
@@ -16,7 +16,7 @@
16
16
  }
17
17
  },
18
18
  "dependencies": {
19
- "@xaendar/common": "0.9.2",
20
- "@xaendar/types": "0.9.2"
19
+ "@xaendar/common": "0.9.4",
20
+ "@xaendar/types": "0.9.4"
21
21
  }
22
22
  }