@communecter/cocolight-api-client 1.0.42 → 1.0.43

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.
@@ -3,6 +3,8 @@
3
3
  const effectStack = [];
4
4
  const computedCache = new WeakMap();
5
5
  const signalRegistry = new WeakMap();
6
+ let batchDepth = 0;
7
+ const pendingEffects = new Set();
6
8
 
7
9
  /**
8
10
  * Crée une fonction réactive qui se relance automatiquement
@@ -23,6 +25,26 @@ export function effect(fn) {
23
25
  return wrapper;
24
26
  }
25
27
 
28
+ /**
29
+ * Exécute une fonction dans un lot, en retardant l'exécution
30
+ * des effets jusqu'à ce que le lot soit terminé.
31
+ * @param {Function} fn
32
+ * @returns {void}
33
+ */
34
+ export function batch(fn) {
35
+ batchDepth++;
36
+ try {
37
+ fn();
38
+ } finally {
39
+ batchDepth--;
40
+ if (batchDepth === 0) {
41
+ const effects = Array.from(pendingEffects);
42
+ pendingEffects.clear();
43
+ for (const effect of effects) effect();
44
+ }
45
+ }
46
+ }
47
+
26
48
  /**
27
49
  * Crée un signal observable pour une valeur primitive ou complexe.
28
50
  * @param {*} initialValue
@@ -41,7 +63,11 @@ function createSignal(initialValue) {
41
63
  set value(newValue) {
42
64
  if (value !== newValue) {
43
65
  value = newValue;
44
- subscribers.forEach(fn => fn());
66
+ if (batchDepth > 0) {
67
+ subscribers.forEach((fn) => pendingEffects.add(fn));
68
+ } else {
69
+ subscribers.forEach((fn) => fn());
70
+ }
45
71
  }
46
72
  },
47
73
  subscribe(fn) {