@esportsplus/reactivity 0.12.3 → 0.13.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.
package/build/system.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { isArray, isObject } from '@esportsplus/utilities';
2
- import { COMPUTED, SIGNAL, STABILIZER_IDLE, STABILIZER_RESCHEDULE, STABILIZER_RUNNING, STABILIZER_SCHEDULED, STATE_CHECK, STATE_DIRTY, STATE_IN_HEAP, STATE_NONE, STATE_RECOMPUTING } from './constants.js';
3
- let depth = 0, heap = new Array(2048), index = 0, length = 0, microtask = queueMicrotask, notified = false, observer = null, stabilizer = STABILIZER_IDLE, version = 0;
2
+ import { COMPUTED, SIGNAL, STABILIZER_IDLE, STABILIZER_RESCHEDULE, STABILIZER_RUNNING, STABILIZER_SCHEDULED, STATE_CHECK, STATE_DIRTY, STATE_IN_HEAP, STATE_NONE, STATE_NOTIFY_MASK, STATE_RECOMPUTING } from './constants.js';
3
+ let depth = 0, heap = new Array(64), heap_i = 0, heap_n = 0, microtask = queueMicrotask, notified = false, observer = null, stabilizer = STABILIZER_IDLE, version = 0;
4
4
  function cleanup(computed) {
5
5
  if (!computed.cleanup) {
6
6
  return;
@@ -55,10 +55,10 @@ function insertIntoHeap(computed) {
55
55
  computed.prevHeap = tail;
56
56
  heapAtHeight.prevHeap = computed;
57
57
  }
58
- if (height > length) {
59
- length = height;
58
+ if (height > heap_n) {
59
+ heap_n = height;
60
60
  if (height >= heap.length) {
61
- heap.length = Math.round(heap.length * 1.5);
61
+ heap.length = Math.max(height + 1, Math.ceil(heap.length * 2));
62
62
  }
63
63
  }
64
64
  }
@@ -106,7 +106,7 @@ function link(dep, sub) {
106
106
  }
107
107
  function notify(computed, newState = STATE_DIRTY) {
108
108
  let state = computed.state;
109
- if ((state & (STATE_CHECK | STATE_DIRTY)) >= newState) {
109
+ if ((state & STATE_NOTIFY_MASK) >= newState) {
110
110
  return;
111
111
  }
112
112
  computed.state = state | newState;
@@ -122,7 +122,9 @@ function recompute(computed, del) {
122
122
  computed.nextHeap = undefined;
123
123
  computed.prevHeap = computed;
124
124
  }
125
- cleanup(computed);
125
+ if (computed.cleanup) {
126
+ cleanup(computed);
127
+ }
126
128
  let o = observer, ok = true, value;
127
129
  observer = computed;
128
130
  computed.depsTail = null;
@@ -175,15 +177,18 @@ function stabilize() {
175
177
  let o = observer;
176
178
  observer = null;
177
179
  stabilizer = STABILIZER_RUNNING;
178
- for (index = 0; index <= length; index++) {
179
- let computed = heap[index];
180
- heap[index] = undefined;
180
+ for (heap_i = 0; heap_i <= heap_n; heap_i++) {
181
+ let computed = heap[heap_i];
182
+ heap[heap_i] = undefined;
181
183
  while (computed !== undefined) {
182
184
  let next = computed.nextHeap;
183
185
  recompute(computed, false);
184
186
  computed = next;
185
187
  }
186
188
  }
189
+ while (heap_n > 0 && heap[heap_n] === undefined) {
190
+ heap_n--;
191
+ }
187
192
  observer = o;
188
193
  if (stabilizer === STABILIZER_RESCHEDULE) {
189
194
  microtask(stabilize);
@@ -193,7 +198,7 @@ function stabilize() {
193
198
  }
194
199
  }
195
200
  function unlink(link) {
196
- let { dep, nextDep, nextSub, prevSub } = link;
201
+ let dep = link.dep, nextDep = link.nextDep, nextSub = link.nextSub, prevSub = link.prevSub;
197
202
  if (nextSub !== null) {
198
203
  nextSub.prevSub = prevSub;
199
204
  }
@@ -267,7 +272,9 @@ const dispose = (computed) => {
267
272
  dep = unlink(dep);
268
273
  }
269
274
  computed.deps = null;
270
- cleanup(computed);
275
+ if (computed.cleanup) {
276
+ cleanup(computed);
277
+ }
271
278
  };
272
279
  const effect = (fn) => {
273
280
  let c = computed(fn);
@@ -285,14 +292,15 @@ const onCleanup = (fn) => {
285
292
  if (!observer) {
286
293
  return fn;
287
294
  }
288
- if (!observer.cleanup) {
295
+ let cleanup = observer.cleanup;
296
+ if (!cleanup) {
289
297
  observer.cleanup = fn;
290
298
  }
291
- else if (isArray(observer.cleanup)) {
292
- observer.cleanup.push(fn);
299
+ else if (isArray(cleanup)) {
300
+ cleanup.push(fn);
293
301
  }
294
302
  else {
295
- observer.cleanup = [observer.cleanup, fn];
303
+ observer.cleanup = [cleanup, fn];
296
304
  }
297
305
  return fn;
298
306
  };
@@ -304,11 +312,10 @@ const read = (node) => {
304
312
  if (height >= observer.height) {
305
313
  observer.height = height + 1;
306
314
  }
307
- if (height >= index ||
308
- node.state & (STATE_DIRTY | STATE_CHECK)) {
315
+ if (height >= heap_i || node.state & STATE_NOTIFY_MASK) {
309
316
  if (!notified) {
310
317
  notified = true;
311
- for (let i = 0; i <= length; i++) {
318
+ for (let i = 0; i <= heap_n; i++) {
312
319
  for (let computed = heap[i]; computed !== undefined; computed = computed.nextHeap) {
313
320
  notify(computed);
314
321
  }
package/build/types.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import { COMPUTED, SIGNAL, STATE_CHECK, STATE_DIRTY, STATE_IN_HEAP, STATE_NONE, STATE_RECOMPUTING } from './constants.js';
2
- import { ReactiveArray } from './reactive/array.js';
3
2
  import { ReactiveObject } from './reactive/object.js';
4
3
  interface Computed<T> {
5
4
  [COMPUTED]: true;
@@ -15,7 +14,7 @@ interface Computed<T> {
15
14
  subsTail: Link | null;
16
15
  value: T;
17
16
  }
18
- type Infer<T> = T extends (...args: unknown[]) => Promise<infer R> ? R | undefined : T extends (...args: any[]) => infer R ? R : T extends (infer U)[] ? ReactiveArray<U> : T extends ReactiveObject<any> ? T : T extends Record<PropertyKey, unknown> ? {
17
+ type Infer<T> = T extends (...args: unknown[]) => Promise<infer R> ? R | undefined : T extends (...args: any[]) => infer R ? R : T extends (infer U)[] ? Infer<U>[] : T extends ReactiveObject<any> ? T : T extends Record<PropertyKey, unknown> ? {
19
18
  [K in keyof T]: T[K];
20
19
  } : T;
21
20
  interface Link {
@@ -26,11 +25,10 @@ interface Link {
26
25
  prevSub: Link | null;
27
26
  version: number;
28
27
  }
29
- type Reactive<T> = T extends Record<PropertyKey, unknown> ? ReactiveObject<T> : ReactiveArray<T>;
30
28
  type Signal<T> = {
31
29
  [SIGNAL]: true;
32
30
  subs: Link | null;
33
31
  subsTail: Link | null;
34
32
  value: T;
35
33
  };
36
- export type { Computed, Infer, Link, Signal, Reactive, ReactiveArray, ReactiveObject };
34
+ export type { Computed, Infer, Link, Signal, ReactiveObject };
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.21.1"
5
+ "@esportsplus/utilities": "^0.22.1"
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.12.3",
15
+ "version": "0.13.0",
16
16
  "scripts": {
17
17
  "build": "tsc && tsc-alias",
18
18
  "-": "-"
package/src/constants.ts CHANGED
@@ -26,6 +26,8 @@ const STATE_RECOMPUTING = 1 << 2;
26
26
 
27
27
  const STATE_IN_HEAP = 1 << 3;
28
28
 
29
+ const STATE_NOTIFY_MASK = (STATE_CHECK | STATE_DIRTY);
30
+
29
31
 
30
32
  export {
31
33
  COMPUTED,
@@ -33,6 +35,7 @@ export {
33
35
  REACTIVE_OBJECT,
34
36
  SIGNAL,
35
37
  STABILIZER_IDLE,
38
+ STATE_NOTIFY_MASK,
36
39
  STABILIZER_RESCHEDULE,
37
40
  STABILIZER_RUNNING,
38
41
  STABILIZER_SCHEDULED,