@esportsplus/reactivity 0.41.0 → 0.43.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.
Files changed (49) hide show
  1. package/README.md +7 -5
  2. package/bench/compiler.bench.ts +24 -16
  3. package/bench/lib/reactive-adapter.ts +1 -1
  4. package/bench/ownership.bench.ts +9 -5
  5. package/bench/reactive/array.bench.ts +6 -2
  6. package/bench/system.bench.ts +9 -5
  7. package/build/compiler/array.d.ts +1 -1
  8. package/build/compiler/array.js +2 -2
  9. package/build/compiler/bindings.d.ts +9 -9
  10. package/build/compiler/bindings.js +59 -62
  11. package/build/compiler/index.d.ts +2 -2
  12. package/build/compiler/index.js +19 -13
  13. package/build/compiler/object.d.ts +1 -1
  14. package/build/compiler/object.js +19 -10
  15. package/build/compiler/plugins/vite.d.ts +18 -4
  16. package/build/compiler/primitives.d.ts +1 -1
  17. package/build/compiler/primitives.js +38 -20
  18. package/build/constants.d.ts +3 -1
  19. package/build/constants.js +3 -1
  20. package/build/reactive/array.js +89 -17
  21. package/build/system.d.ts +4 -3
  22. package/build/system.js +48 -16
  23. package/package.json +2 -2
  24. package/src/compiler/array.ts +2 -2
  25. package/src/compiler/bindings.ts +82 -86
  26. package/src/compiler/index.ts +25 -14
  27. package/src/compiler/object.ts +31 -17
  28. package/src/compiler/primitives.ts +61 -25
  29. package/src/constants.ts +6 -1
  30. package/src/reactive/array.ts +130 -23
  31. package/src/system.ts +66 -23
  32. package/test/async-hardening.test.ts +2 -2
  33. package/test/async-iterable.test.ts +3 -3
  34. package/test/compiler/compiler.test.ts +169 -156
  35. package/test/compiler/composition.test.ts +31 -27
  36. package/test/compiler/coverage.test.ts +12 -12
  37. package/test/compiler/origins.test.ts +18 -18
  38. package/test/compiler/output.test.ts +4 -4
  39. package/test/deep-graphs.test.ts +6 -6
  40. package/test/effects.test.ts +5 -5
  41. package/test/errors.test.ts +4 -4
  42. package/test/invalidate.test.ts +2 -2
  43. package/test/ownership.test.ts +30 -30
  44. package/test/pause.test.ts +267 -0
  45. package/test/reactive/array.test.ts +5 -5
  46. package/test/reactive/reactive.test.ts +3 -3
  47. package/test/signal-selector.test.ts +4 -4
  48. package/test/system.test.ts +10 -9
  49. package/test/untrack.test.ts +9 -9
@@ -1,9 +1,10 @@
1
1
  declare const _default: ({ root }?: {
2
2
  root?: string;
3
3
  }) => {
4
+ buildEnd: () => void;
4
5
  buildStart: () => void;
5
- closeBundle: () => void;
6
- closeWatcher: () => void;
6
+ closeBundle: () => Promise<void> | undefined;
7
+ closeWatcher: () => Promise<void>;
7
8
  configResolved: (config: unknown) => void;
8
9
  enforce: 'pre';
9
10
  handleHotUpdate: <M>(ctx: {
@@ -11,16 +12,29 @@ declare const _default: ({ root }?: {
11
12
  modules: M[];
12
13
  server: {
13
14
  moduleGraph: {
15
+ getModuleById?(id: string): M | undefined;
14
16
  getModulesByFile(file: string): Set<M> | undefined;
15
17
  invalidateModule(module: M, seen: Set<M>, timestamp: number, isHmr: boolean): void;
16
18
  };
17
19
  };
18
20
  timestamp: number;
19
21
  }) => M[] | undefined;
22
+ load: {
23
+ filter: {
24
+ id: RegExp;
25
+ };
26
+ handler: (id: string) => string | null;
27
+ };
20
28
  name: string;
21
29
  renderChunk: (code: string, chunk: {
22
30
  fileName: string;
23
31
  }) => null;
32
+ resolveId: {
33
+ filter: {
34
+ id: RegExp;
35
+ };
36
+ handler: (id: string) => string | null;
37
+ };
24
38
  transform: {
25
39
  filter: {
26
40
  id: {
@@ -28,10 +42,10 @@ declare const _default: ({ root }?: {
28
42
  include: RegExp;
29
43
  };
30
44
  };
31
- handler: (code: string, id: string) => {
45
+ handler: (this: unknown, code: string, id: string) => Promise<{
32
46
  code: string;
33
47
  map: import("@esportsplus/typescript/compiler").SourceMapV3;
34
- } | null;
48
+ } | null>;
35
49
  };
36
50
  watchChange: (id: string) => void;
37
51
  };
@@ -11,5 +11,5 @@ type PrimitivesTransformResult = {
11
11
  replacements: ReplacementIntent[];
12
12
  };
13
13
  export default _default;
14
- declare function _default(sourceFile: ts.SourceFile, bindings: Bindings, isReactiveCall: IsReactiveCall): PrimitivesTransformResult;
14
+ declare function _default(sourceFile: ts.SourceFile, bindings: Bindings, isReactiveCall: IsReactiveCall): Promise<PrimitivesTransformResult>;
15
15
  export type { Failure };
@@ -1,9 +1,18 @@
1
1
  import { ts } from '@esportsplus/typescript';
2
2
  import { COMPOUND_OPERATORS, NAMESPACE, TYPES } from './constants.js';
3
3
  import scope from './bindings.js';
4
- function declare(ctx, node) {
4
+ function calls(ctx, node, found) {
5
5
  if (ctx.isReactiveCall(node)) {
6
- let call = node, kind = scope.classify(ctx.bindings, call);
6
+ found.push(node);
7
+ }
8
+ node.forEachChild(child => calls(ctx, child, found));
9
+ }
10
+ async function declare(ctx, sourceFile) {
11
+ let found = [];
12
+ calls(ctx, sourceFile, found);
13
+ let kinds = await Promise.all(found.map((call) => scope.classify(ctx.bindings, call)));
14
+ for (let i = 0, n = found.length; i < n; i++) {
15
+ let call = found[i], kind = kinds[i];
7
16
  ctx.calls.push(call);
8
17
  if (kind === null) {
9
18
  ctx.failures.push({
@@ -35,7 +44,6 @@ function declare(ctx, node) {
35
44
  }
36
45
  }
37
46
  }
38
- node.forEachChild(child => declare(ctx, child));
39
47
  }
40
48
  function isBound(call) {
41
49
  let node = call, parent = node.parent;
@@ -55,7 +63,7 @@ function isBound(call) {
55
63
  }
56
64
  return ts.isExportAssignment(parent);
57
65
  }
58
- function isNamespace(bindings, expression) {
66
+ async function isNamespace(bindings, expression) {
59
67
  let value = scope.unwrap(expression);
60
68
  if (ts.isAwaitExpression(value)) {
61
69
  value = scope.unwrap(value.expression);
@@ -63,7 +71,7 @@ function isNamespace(bindings, expression) {
63
71
  if (ts.isCallExpression(value)) {
64
72
  return value.expression.kind === ts.SyntaxKind.ImportKeyword;
65
73
  }
66
- return ts.isIdentifier(value) && scope.origin(bindings, value)?.declaration.kind === ts.SyntaxKind.SourceFile;
74
+ return ts.isIdentifier(value) && (await scope.origin(bindings, value))?.declaration.kind === ts.SyntaxKind.SourceFile;
67
75
  }
68
76
  function isPatternTarget(node) {
69
77
  let current = node;
@@ -175,10 +183,10 @@ function reference(ctx, node, kind) {
175
183
  }
176
184
  }
177
185
  }
178
- function rewrite(ctx) {
179
- let { bindings } = ctx;
180
- for (let [identifier, found] of bindings.origins) {
181
- let kind = scope.kind(bindings, found);
186
+ async function rewrite(ctx) {
187
+ let { bindings } = ctx, origins = [...bindings.origins], kinds = await Promise.all(origins.map(([, found]) => scope.kind(bindings, found)));
188
+ for (let i = 0, n = origins.length; i < n; i++) {
189
+ let [identifier] = origins[i], kind = kinds[i];
182
190
  if (kind !== TYPES.Signal && kind !== TYPES.Computed) {
183
191
  continue;
184
192
  }
@@ -189,21 +197,31 @@ function rewrite(ctx) {
189
197
  }
190
198
  reference(ctx, readNode(identifier), kind);
191
199
  }
192
- unpacked(ctx, bindings.sourceFile);
200
+ let elements = [];
201
+ unpacked(bindings.sourceFile, elements);
202
+ let copied = await Promise.all(elements.map(async ({ element, initializer }) => {
203
+ if (!(await isNamespace(bindings, initializer))) {
204
+ return false;
205
+ }
206
+ let kind = await scope.kind(bindings, await scope.origin(bindings, element));
207
+ return kind === TYPES.Signal || kind === TYPES.Computed;
208
+ }));
209
+ for (let i = 0, n = elements.length; i < n; i++) {
210
+ if (copied[i]) {
211
+ ctx.failures.push({ message: 'a reactive binding cannot be destructured (it would copy its signal); read it through the namespace', node: elements[i].element });
212
+ }
213
+ }
193
214
  }
194
- function unpacked(ctx, node) {
215
+ function unpacked(node, found) {
195
216
  if (ts.isBindingElement(node) && !node.propertyName && ts.isObjectBindingPattern(node.parent)) {
196
217
  let initializer = node.parent.parent.initializer;
197
- if (initializer && isNamespace(ctx.bindings, initializer)) {
198
- let kind = scope.kind(ctx.bindings, scope.origin(ctx.bindings, node));
199
- if (kind === TYPES.Signal || kind === TYPES.Computed) {
200
- ctx.failures.push({ message: 'a reactive binding cannot be destructured (it would copy its signal); read it through the namespace', node });
201
- }
218
+ if (initializer) {
219
+ found.push({ element: node, initializer });
202
220
  }
203
221
  }
204
- node.forEachChild(child => unpacked(ctx, child));
222
+ node.forEachChild(child => unpacked(child, found));
205
223
  }
206
- export default (sourceFile, bindings, isReactiveCall) => {
224
+ export default async (sourceFile, bindings, isReactiveCall) => {
207
225
  let ctx = {
208
226
  bindings,
209
227
  calls: [],
@@ -212,7 +230,7 @@ export default (sourceFile, bindings, isReactiveCall) => {
212
230
  replacements: [],
213
231
  tmpCounter: 0
214
232
  };
215
- declare(ctx, sourceFile);
216
- rewrite(ctx);
233
+ await declare(ctx, sourceFile);
234
+ await rewrite(ctx);
217
235
  return { calls: ctx.calls, failures: ctx.failures, replacements: ctx.replacements };
218
236
  };
@@ -16,5 +16,7 @@ declare const STATE_COMPUTED: number;
16
16
  declare const STATE_ERROR: number;
17
17
  declare const STATE_EFFECT: number;
18
18
  declare const STATE_DISPOSED: number;
19
+ declare const STATE_PAUSED: number;
20
+ declare const STATE_STALE: number;
19
21
  declare const STATE_NOTIFY_MASK: number;
20
- export { COMPUTED, PACKAGE_NAME, REACTIVE_ARRAY, REACTIVE_OBJECT, SIGNAL, STABILIZER_DEFERRED, STABILIZER_IDLE, STABILIZER_RESCHEDULE, STABILIZER_RUNNING, STABILIZER_SCHEDULED, STATE_CHECK, STATE_COMPUTED, STATE_DIRTY, STATE_DISPOSED, STATE_EFFECT, STATE_ERROR, STATE_IN_HEAP, STATE_NOTIFY_MASK, STATE_RECOMPUTING };
22
+ export { COMPUTED, PACKAGE_NAME, REACTIVE_ARRAY, REACTIVE_OBJECT, SIGNAL, STABILIZER_DEFERRED, STABILIZER_IDLE, STABILIZER_RESCHEDULE, STABILIZER_RUNNING, STABILIZER_SCHEDULED, STATE_CHECK, STATE_COMPUTED, STATE_DIRTY, STATE_DISPOSED, STATE_EFFECT, STATE_ERROR, STATE_IN_HEAP, STATE_NOTIFY_MASK, STATE_PAUSED, STATE_RECOMPUTING, STATE_STALE };
@@ -16,5 +16,7 @@ const STATE_COMPUTED = 1 << 4;
16
16
  const STATE_ERROR = 1 << 5;
17
17
  const STATE_EFFECT = 1 << 6;
18
18
  const STATE_DISPOSED = 1 << 7;
19
+ const STATE_PAUSED = 1 << 8;
20
+ const STATE_STALE = 1 << 9;
19
21
  const STATE_NOTIFY_MASK = (STATE_CHECK | STATE_DIRTY);
20
- export { COMPUTED, PACKAGE_NAME, REACTIVE_ARRAY, REACTIVE_OBJECT, SIGNAL, STABILIZER_DEFERRED, STABILIZER_IDLE, STABILIZER_RESCHEDULE, STABILIZER_RUNNING, STABILIZER_SCHEDULED, STATE_CHECK, STATE_COMPUTED, STATE_DIRTY, STATE_DISPOSED, STATE_EFFECT, STATE_ERROR, STATE_IN_HEAP, STATE_NOTIFY_MASK, STATE_RECOMPUTING };
22
+ export { COMPUTED, PACKAGE_NAME, REACTIVE_ARRAY, REACTIVE_OBJECT, SIGNAL, STABILIZER_DEFERRED, STABILIZER_IDLE, STABILIZER_RESCHEDULE, STABILIZER_RUNNING, STABILIZER_SCHEDULED, STATE_CHECK, STATE_COMPUTED, STATE_DIRTY, STATE_DISPOSED, STATE_EFFECT, STATE_ERROR, STATE_IN_HEAP, STATE_NOTIFY_MASK, STATE_PAUSED, STATE_RECOMPUTING, STATE_STALE };
@@ -1,12 +1,63 @@
1
1
  import { isArray } from '@esportsplus/utilities';
2
2
  import { PACKAGE_NAME, REACTIVE_ARRAY } from '../constants.js';
3
- import { effect, read, root, signal, write } from '../system.js';
3
+ import { dispose as disposeNode, effect, read, root, signal, write } from '../system.js';
4
4
  import { isReactiveObject } from './object.js';
5
5
  function dispose(value) {
6
6
  if (isReactiveObject(value)) {
7
7
  value.dispose();
8
8
  }
9
9
  }
10
+ function move(array, from, to) {
11
+ if (from in array) {
12
+ array[to] = array[from];
13
+ }
14
+ else {
15
+ delete array[to];
16
+ }
17
+ }
18
+ function place(array, start, count, items) {
19
+ let length = array.length, n = items.length, total = length - count + n;
20
+ if (n < count) {
21
+ for (let i = start + count; i < length; i++) {
22
+ move(array, i, i - count + n);
23
+ }
24
+ }
25
+ else if (n > count) {
26
+ let shift = n - count;
27
+ for (let i = length; i < total; i++) {
28
+ if (i < start + n) {
29
+ array[i] = items[i - start];
30
+ }
31
+ else {
32
+ move(array, i - shift, i);
33
+ }
34
+ }
35
+ for (let i = length - 1; i >= start + n; i--) {
36
+ move(array, i - shift, i);
37
+ }
38
+ }
39
+ for (let i = 0; i < n; i++) {
40
+ array[start + i] = items[i];
41
+ }
42
+ if (array.length !== total) {
43
+ array.length = total;
44
+ }
45
+ }
46
+ function rank(array, fn) {
47
+ let n = array.length, values = [];
48
+ for (let i = 0; i < n; i++) {
49
+ if (i in array) {
50
+ values.push(array[i]);
51
+ }
52
+ }
53
+ values.sort(fn);
54
+ for (let i = 0, m = values.length; i < m; i++) {
55
+ array[i] = values[i];
56
+ }
57
+ for (let i = values.length; i < n; i++) {
58
+ delete array[i];
59
+ }
60
+ }
10
61
  class ReactiveArray extends Array {
11
62
  _length;
12
63
  batched = null;
@@ -100,7 +151,7 @@ class ReactiveArray extends Array {
100
151
  let batched = this.batched;
101
152
  if (batched !== null) {
102
153
  this.batched = null;
103
- batched.stop();
154
+ disposeNode(batched.stop);
104
155
  }
105
156
  }
106
157
  clear() {
@@ -115,13 +166,14 @@ class ReactiveArray extends Array {
115
166
  let item = items[i];
116
167
  if (isArray(item)) {
117
168
  for (let j = 0, o = item.length; j < o; j++) {
118
- added.push(item[j]);
119
- super.push(item[j]);
169
+ let value = item[j];
170
+ added.push(value);
171
+ this[this.length] = value;
120
172
  }
121
173
  }
122
174
  else {
123
175
  added.push(item);
124
- super.push(item);
176
+ this[this.length] = item;
125
177
  }
126
178
  }
127
179
  if (added.length) {
@@ -235,14 +287,26 @@ class ReactiveArray extends Array {
235
287
  if (!items.length) {
236
288
  return this.length;
237
289
  }
238
- let length = super.push(...items);
290
+ let length = this.length;
291
+ for (let i = 0, n = items.length; i < n; i++) {
292
+ this[length++] = items[i];
293
+ }
239
294
  write(this._length, length);
240
295
  this.dispatch('push', { items });
241
296
  this.record({ items, op: 'push' });
242
297
  return length;
243
298
  }
244
299
  reverse() {
245
- super.reverse();
300
+ for (let lower = 0, upper = this.length - 1; lower < upper; lower++, upper--) {
301
+ let value = this[lower], within = lower in this;
302
+ move(this, upper, lower);
303
+ if (within) {
304
+ this[upper] = value;
305
+ }
306
+ else {
307
+ delete this[upper];
308
+ }
309
+ }
246
310
  this.dispatch('reverse');
247
311
  this.record({ op: 'reverse' });
248
312
  return this;
@@ -251,7 +315,8 @@ class ReactiveArray extends Array {
251
315
  if (this.length === 0) {
252
316
  return undefined;
253
317
  }
254
- let item = super.shift();
318
+ let item = this[0];
319
+ place(this, 0, 1, []);
255
320
  dispose(item);
256
321
  write(this._length, this.length);
257
322
  this.dispatch('shift', { item });
@@ -260,14 +325,14 @@ class ReactiveArray extends Array {
260
325
  }
261
326
  sort(fn) {
262
327
  if (this.listeners.sort === undefined && this.batched === null) {
263
- super.sort(fn);
328
+ rank(this, fn);
264
329
  }
265
330
  else {
266
331
  let n = this.length, before = new Array(n);
267
332
  for (let i = 0; i < n; i++) {
268
333
  before[i] = this[i];
269
334
  }
270
- super.sort(fn);
335
+ rank(this, fn);
271
336
  let buckets = new Map(), order = new Array(n);
272
337
  for (let i = 0; i < n; i++) {
273
338
  let value = before[i], bucket = buckets.get(value);
@@ -288,15 +353,21 @@ class ReactiveArray extends Array {
288
353
  return this;
289
354
  }
290
355
  splice(start, deleteCount = this.length, ...items) {
291
- let length = this.length, removed = super.splice(start, deleteCount, ...items);
292
- if (items.length > 0 || removed.length > 0) {
356
+ let length = this.length, index = Math.trunc(start) || 0, resolved = index < 0 ? Math.max(length + index, 0) : Math.min(index, length), count = Math.min(Math.max(Math.trunc(deleteCount) || 0, 0), length - resolved), removed = [];
357
+ for (let i = 0; i < count; i++) {
358
+ if (resolved + i in this) {
359
+ removed[i] = this[resolved + i];
360
+ }
361
+ }
362
+ removed.length = count;
363
+ if (items.length > 0 || count > 0) {
364
+ place(this, resolved, count, items);
293
365
  write(this._length, this.length);
294
- for (let i = 0, n = removed.length; i < n; i++) {
366
+ for (let i = 0; i < count; i++) {
295
367
  dispose(removed[i]);
296
368
  }
297
- let index = Math.trunc(start) || 0, resolved = index < 0 ? Math.max(length + index, 0) : Math.min(index, length);
298
- this.dispatch('splice', { deleteCount: removed.length, items, start: resolved });
299
- this.record({ deleteCount: removed.length, items, op: 'splice', start: resolved });
369
+ this.dispatch('splice', { deleteCount: count, items, start: resolved });
370
+ this.record({ deleteCount: count, items, op: 'splice', start: resolved });
300
371
  }
301
372
  return removed;
302
373
  }
@@ -304,7 +375,8 @@ class ReactiveArray extends Array {
304
375
  if (!items.length) {
305
376
  return this.length;
306
377
  }
307
- let length = super.unshift(...items);
378
+ place(this, 0, 0, items);
379
+ let length = this.length;
308
380
  write(this._length, length);
309
381
  this.dispatch('unshift', { items });
310
382
  this.record({ items, op: 'unshift' });
package/build/system.d.ts CHANGED
@@ -5,15 +5,16 @@ declare namespace computed {
5
5
  var invalidate: <T>(c: Computed<T>) => void;
6
6
  }
7
7
  declare const dispose: <T>(computed: Computed<T>) => void;
8
- declare const effect: <T>(fn: Computed<T>['fn'], apply?: (value: T, prev: T | undefined) => void) => () => void;
8
+ declare const effect: <T>(fn: Computed<T>['fn'], apply?: (value: T, prev: T | undefined) => void) => Computed<T | undefined>;
9
9
  declare const flush: () => void;
10
- declare const hasOwner: () => boolean;
11
10
  declare const isComputed: (value: unknown) => value is Computed<unknown>;
12
11
  declare const isSignal: (value: unknown) => value is Signal<unknown>;
13
12
  declare const onCleanup: (fn: VoidFunction) => typeof fn;
14
13
  declare const peek: <T>(node: Signal<T> | Computed<T>) => T;
15
14
  declare const read: <T>(node: Signal<T> | Computed<T>) => T;
16
15
  declare const owner: () => Computed<unknown> | null;
16
+ declare const pause: <T>(node: Computed<T>) => void;
17
+ declare const resume: <T>(node: Computed<T>) => void;
17
18
  declare const root: <T>(fn: ((dispose: VoidFunction) => T) | (() => T), parent?: Computed<unknown> | null) => T;
18
19
  declare function signal<T>(value: T, equals?: ((a: T, b: T) => boolean) | null): Signal<T>;
19
20
  declare namespace signal {
@@ -21,5 +22,5 @@ declare namespace signal {
21
22
  }
22
23
  declare const untrack: <T>(fn: () => T) => T;
23
24
  declare const write: <T>(signal: Signal<T>, value: T) => void;
24
- export { batch, computed, dispose, effect, flush, hasOwner, isComputed, isSignal, onCleanup, owner, peek, read, root, signal, untrack, write };
25
+ export { batch, computed, dispose, effect, flush, isComputed, isSignal, onCleanup, owner, pause, peek, read, resume, root, signal, untrack, write };
25
26
  export type { Computed, Signal };
package/build/system.js CHANGED
@@ -1,4 +1,4 @@
1
- import { PACKAGE_NAME, SIGNAL, STABILIZER_DEFERRED, STABILIZER_IDLE, STABILIZER_RESCHEDULE, STABILIZER_RUNNING, STABILIZER_SCHEDULED, STATE_CHECK, STATE_COMPUTED, STATE_DIRTY, STATE_DISPOSED, STATE_EFFECT, STATE_ERROR, STATE_IN_HEAP, STATE_NOTIFY_MASK, STATE_RECOMPUTING } from './constants.js';
1
+ import { PACKAGE_NAME, SIGNAL, STABILIZER_DEFERRED, STABILIZER_IDLE, STABILIZER_RESCHEDULE, STABILIZER_RUNNING, STABILIZER_SCHEDULED, STATE_CHECK, STATE_COMPUTED, STATE_DIRTY, STATE_DISPOSED, STATE_EFFECT, STATE_ERROR, STATE_IN_HEAP, STATE_NOTIFY_MASK, STATE_PAUSED, STATE_RECOMPUTING, STATE_STALE } from './constants.js';
2
2
  import { isObject, isPromise } from '@esportsplus/utilities';
3
3
  let asyncFactory = new WeakMap(), depth = 0, disposeHead = null, draining = false, heap = new Array(64), heap_i = 0, heap_n = 0, linkPoolHead = null, microtask = queueMicrotask, notified = false, observer = null, pendingHead = null, pulled = null, puller = null, scope = null, stabilizer = STABILIZER_IDLE, version = 0, walkPoolHead = null, writes = 0;
4
4
  function noop() {
@@ -267,8 +267,14 @@ function propagate(computed) {
267
267
  schedule();
268
268
  }
269
269
  function recompute(computed) {
270
- if (computed.state & STATE_IN_HEAP) {
271
- deleteFromHeap(computed);
270
+ if (computed.state & (STATE_IN_HEAP | STATE_PAUSED)) {
271
+ if (computed.state & STATE_IN_HEAP) {
272
+ deleteFromHeap(computed);
273
+ }
274
+ if (computed.state & STATE_PAUSED) {
275
+ computed.state |= STATE_STALE;
276
+ return;
277
+ }
272
278
  }
273
279
  if (computed.cleanup !== null || computed.owned !== null) {
274
280
  try {
@@ -296,18 +302,23 @@ function recompute(computed) {
296
302
  depth--;
297
303
  version++;
298
304
  observer = o;
299
- computed.state = STATE_COMPUTED | flags | (computed.state & STATE_IN_HEAP);
305
+ computed.state = STATE_COMPUTED | flags | (computed.state & (STATE_IN_HEAP | STATE_PAUSED));
300
306
  computed.gv = w;
301
307
  let depsTail = computed.depsTail, remove = depsTail ? depsTail.nextDep : computed.deps;
302
308
  if (remove) {
303
- do {
304
- remove = unlink(remove);
305
- } while (remove);
306
- if (depsTail) {
307
- depsTail.nextDep = null;
309
+ if (computed.state & STATE_PAUSED) {
310
+ computed.state |= STATE_STALE;
308
311
  }
309
312
  else {
310
- computed.deps = null;
313
+ do {
314
+ remove = unlink(remove);
315
+ } while (remove);
316
+ if (depsTail) {
317
+ depsTail.nextDep = null;
318
+ }
319
+ else {
320
+ computed.deps = null;
321
+ }
311
322
  }
312
323
  }
313
324
  if (ok) {
@@ -519,7 +530,7 @@ function makeAsyncComputed(factory) {
519
530
  });
520
531
  wrapper.pending = pending;
521
532
  asyncFactory.set(wrapper, factory);
522
- wrapper.disposal = stop;
533
+ wrapper.disposal = () => dispose(stop);
523
534
  return wrapper;
524
535
  }
525
536
  function makeComputed(fn, eager = false) {
@@ -681,16 +692,13 @@ const effect = (fn, apply) => {
681
692
  throw err;
682
693
  });
683
694
  }
684
- return () => {
685
- dispose(c);
686
- };
695
+ return c;
687
696
  };
688
697
  const flush = () => {
689
698
  if (stabilizer === STABILIZER_SCHEDULED) {
690
699
  stabilize();
691
700
  }
692
701
  };
693
- const hasOwner = () => observer !== null || scope !== null;
694
702
  const isComputed = (value) => {
695
703
  return isObject(value) && !!(value.state & STATE_COMPUTED);
696
704
  };
@@ -753,6 +761,30 @@ const read = (node) => {
753
761
  return node.value;
754
762
  };
755
763
  const owner = () => observer || scope;
764
+ const pause = (node) => {
765
+ let state = node.state;
766
+ if (state & STATE_DISPOSED) {
767
+ return;
768
+ }
769
+ if (!(state & STATE_EFFECT)) {
770
+ throw new Error(`${PACKAGE_NAME}: pause() expects an effect`);
771
+ }
772
+ node.state = state | STATE_PAUSED;
773
+ };
774
+ const resume = (node) => {
775
+ let state = node.state;
776
+ if (state & STATE_DISPOSED) {
777
+ return;
778
+ }
779
+ if (!(state & STATE_EFFECT)) {
780
+ throw new Error(`${PACKAGE_NAME}: resume() expects an effect`);
781
+ }
782
+ node.state = state & ~(STATE_PAUSED | STATE_STALE);
783
+ if (state & STATE_STALE) {
784
+ insertIntoHeap(node);
785
+ schedule();
786
+ }
787
+ };
756
788
  const root = (fn, parent) => {
757
789
  if (parent && parent.state & STATE_DISPOSED) {
758
790
  throw new Error(`${PACKAGE_NAME}: root() was given a disposed owner`);
@@ -859,4 +891,4 @@ const write = (signal, value) => {
859
891
  }
860
892
  schedule();
861
893
  };
862
- export { batch, computed, dispose, effect, flush, hasOwner, isComputed, isSignal, onCleanup, owner, peek, read, root, signal, untrack, write };
894
+ export { batch, computed, dispose, effect, flush, isComputed, isSignal, onCleanup, owner, pause, peek, read, resume, root, signal, untrack, write };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "author": "ICJR",
3
3
  "dependencies": {
4
- "@esportsplus/typescript": "^0.39.0",
4
+ "@esportsplus/typescript": "^0.42.0",
5
5
  "@esportsplus/utilities": "^0.28.6"
6
6
  },
7
7
  "devDependencies": {
@@ -41,7 +41,7 @@
41
41
  },
42
42
  "type": "module",
43
43
  "types": "build/index.d.ts",
44
- "version": "0.41.0",
44
+ "version": "0.43.0",
45
45
  "scripts": {
46
46
  "agent:bench": "vitest bench --run",
47
47
  "agent:test": "pnpm typecheck && vitest run",
@@ -196,7 +196,7 @@ function write(access: ts.ElementAccessExpression, ctx: ArrayTransformResult): v
196
196
 
197
197
  // Reactive arrays are recognized by type, so `.length` and element writes compile wherever the
198
198
  // array came from: a local reactive([...]), an import, an object property, a parameter
199
- export default (sourceFile: ts.SourceFile, bindings: Bindings, isReactiveCall: IsReactiveCall): ArrayTransformResult => {
199
+ export default async (sourceFile: ts.SourceFile, bindings: Bindings, isReactiveCall: IsReactiveCall): Promise<ArrayTransformResult> => {
200
200
  let candidates: Candidates = { lengths: [], reassignments: [], writes: [] },
201
201
  result: ArrayTransformResult = { failures: [], namespaced: false, reactive: [], replacements: [] };
202
202
 
@@ -205,7 +205,7 @@ export default (sourceFile: ts.SourceFile, bindings: Bindings, isReactiveCall: I
205
205
  result.namespaced = result.replacements.length > 0;
206
206
  collect(sourceFile, candidates);
207
207
 
208
- let reactive = scope.arrays(bindings, [
208
+ let reactive = await scope.arrays(bindings, [
209
209
  ...candidates.lengths.map(access => access.expression),
210
210
  ...candidates.reassignments.map(assignment => assignment.left),
211
211
  ...candidates.writes.map(access => access.expression)