@esportsplus/reactivity 0.6.2 → 0.7.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.
package/build/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export { default as reactive } from './reactive/index.js';
2
2
  export { computed, dispose, oncleanup, root, signal, stabilize } from './signal.js';
3
+ export { default as scheduler } from './scheduler.js';
3
4
  export * from './types.js';
package/build/index.js CHANGED
@@ -1,3 +1,4 @@
1
1
  export { default as reactive } from './reactive/index.js';
2
2
  export { computed, dispose, oncleanup, root, signal, stabilize } from './signal.js';
3
+ export { default as scheduler } from './scheduler.js';
3
4
  export * from './types.js';
@@ -0,0 +1,4 @@
1
+ declare const scheduler: (schedule: (task: VoidFunction) => void) => void;
2
+ declare const state: import("./types.js").Signal<number>;
3
+ export default scheduler;
4
+ export { state };
@@ -0,0 +1,17 @@
1
+ import { STATE_DIRTY, STATE_NONE } from './constants.js';
2
+ import { computed, dispose, read, signal, stabilize } from './signal.js';
3
+ let c = null;
4
+ const scheduler = (schedule) => {
5
+ if (c) {
6
+ dispose(c);
7
+ }
8
+ c = computed(() => {
9
+ if (read(state) !== STATE_DIRTY) {
10
+ return;
11
+ }
12
+ schedule(stabilize);
13
+ });
14
+ };
15
+ const state = signal(STATE_NONE);
16
+ export default scheduler;
17
+ export { state };
package/build/signal.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { isArray, isObject } from '@esportsplus/utilities';
2
2
  import { REACTIVE, STATE_CHECK, STATE_DIRTY, STATE_IN_HEAP, STATE_NONE, STATE_RECOMPUTING } from './constants.js';
3
+ import { state } from './scheduler.js';
3
4
  let dirtyHeap = new Array(2000), maxDirty = 0, markedHeap = false, minDirty = 0, observer = null;
4
5
  function cleanup(node) {
5
6
  if (!node.cleanup) {
@@ -159,6 +160,9 @@ function recompute(el, del) {
159
160
  insertIntoHeap(o);
160
161
  }
161
162
  }
163
+ if (state.value === STATE_NONE) {
164
+ root(() => signal.set(state, STATE_DIRTY));
165
+ }
162
166
  }
163
167
  function unlink(link) {
164
168
  let dep = link.dep, nextDep = link.nextDep, nextSub = link.nextSub, prevSub = link.prevSub;
@@ -305,14 +309,17 @@ signal.set = (el, v) => {
305
309
  }
306
310
  };
307
311
  const stabilize = () => {
308
- for (minDirty = 0; minDirty <= maxDirty; minDirty++) {
309
- let el = dirtyHeap[minDirty];
310
- dirtyHeap[minDirty] = undefined;
311
- while (el !== undefined) {
312
- let next = el.nextHeap;
313
- recompute(el, false);
314
- el = next;
312
+ root(() => {
313
+ for (minDirty = 0; minDirty <= maxDirty; minDirty++) {
314
+ let el = dirtyHeap[minDirty];
315
+ dirtyHeap[minDirty] = undefined;
316
+ while (el !== undefined) {
317
+ let next = el.nextHeap;
318
+ recompute(el, false);
319
+ el = next;
320
+ }
315
321
  }
316
- }
322
+ signal.set(state, STATE_NONE);
323
+ });
317
324
  };
318
325
  export { computed, dispose, isComputed, isReactive, isSignal, oncleanup, read, root, signal, stabilize };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "author": "ICJR",
3
3
  "dependencies": {
4
4
  "@esportsplus/custom-function": "^0.0.13",
5
- "@esportsplus/utilities": "^0.19.1"
5
+ "@esportsplus/utilities": "^0.21.0"
6
6
  },
7
7
  "devDependencies": {
8
8
  "@esportsplus/typescript": "^0.9.2"
@@ -12,7 +12,7 @@
12
12
  "private": false,
13
13
  "type": "module",
14
14
  "types": "build/index.d.ts",
15
- "version": "0.6.2",
15
+ "version": "0.7.1",
16
16
  "scripts": {
17
17
  "build": "tsc && tsc-alias",
18
18
  "-": "-"
package/src/constants.ts CHANGED
@@ -11,4 +11,11 @@ const STATE_RECOMPUTING = 1 << 2;
11
11
  const STATE_IN_HEAP = 1 << 3;
12
12
 
13
13
 
14
- export { REACTIVE, STATE_CHECK, STATE_DIRTY, STATE_IN_HEAP, STATE_NONE, STATE_RECOMPUTING };
14
+ export {
15
+ REACTIVE,
16
+ STATE_CHECK,
17
+ STATE_DIRTY,
18
+ STATE_IN_HEAP,
19
+ STATE_NONE,
20
+ STATE_RECOMPUTING
21
+ };
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export { default as reactive } from './reactive';
2
2
  export { computed, dispose, oncleanup, root, signal, stabilize } from './signal';
3
+ export { default as scheduler } from './scheduler';
3
4
  export * from './types';
@@ -0,0 +1,27 @@
1
+ import { STATE_DIRTY, STATE_NONE } from './constants';
2
+ import { computed, dispose, read, signal, stabilize } from './signal';
3
+ import { Computed } from './types';
4
+
5
+
6
+ let c: Computed<void> | null = null;
7
+
8
+
9
+ const scheduler = (schedule: (task: VoidFunction) => void) => {
10
+ if (c) {
11
+ dispose(c);
12
+ }
13
+
14
+ c = computed(() => {
15
+ if (read(state) !== STATE_DIRTY) {
16
+ return;
17
+ }
18
+
19
+ schedule(stabilize);
20
+ });
21
+ };
22
+
23
+ const state = signal(STATE_NONE);
24
+
25
+
26
+ export default scheduler;
27
+ export { state };
package/src/signal.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { isArray, isObject } from '@esportsplus/utilities';
2
2
  import { REACTIVE, STATE_CHECK, STATE_DIRTY, STATE_IN_HEAP, STATE_NONE, STATE_RECOMPUTING } from './constants';
3
3
  import { Computed, Link, Signal, } from './types';
4
+ import { state } from './scheduler';
4
5
 
5
6
 
6
7
  let dirtyHeap: (Computed<unknown> | undefined)[] = new Array(2000),
@@ -226,6 +227,10 @@ function recompute<T>(el: Computed<T>, del: boolean) {
226
227
  insertIntoHeap(o);
227
228
  }
228
229
  }
230
+
231
+ if (state.value === STATE_NONE) {
232
+ root(() => signal.set(state, STATE_DIRTY));
233
+ }
229
234
  }
230
235
 
231
236
  // https://github.com/stackblitz/alien-signals/blob/v2.0.3/src/system.ts#L100
@@ -421,19 +426,23 @@ signal.set = <T>(el: Signal<T>, v: T) => {
421
426
  };
422
427
 
423
428
  const stabilize = () => {
424
- for (minDirty = 0; minDirty <= maxDirty; minDirty++) {
425
- let el = dirtyHeap[minDirty];
429
+ root(() => {
430
+ for (minDirty = 0; minDirty <= maxDirty; minDirty++) {
431
+ let el = dirtyHeap[minDirty];
426
432
 
427
- dirtyHeap[minDirty] = undefined;
433
+ dirtyHeap[minDirty] = undefined;
428
434
 
429
- while (el !== undefined) {
430
- let next = el.nextHeap;
435
+ while (el !== undefined) {
436
+ let next = el.nextHeap;
431
437
 
432
- recompute(el, false);
438
+ recompute(el, false);
433
439
 
434
- el = next;
440
+ el = next;
441
+ }
435
442
  }
436
- }
443
+
444
+ signal.set(state, STATE_NONE);
445
+ });
437
446
  };
438
447
 
439
448