@esportsplus/reactivity 0.40.0 → 0.42.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 (61) hide show
  1. package/README.md +16 -10
  2. package/bench/compiler.bench.ts +122 -0
  3. package/bench/kairo.bench.ts +1 -1
  4. package/bench/lib/reactive-adapter.ts +1 -1
  5. package/bench/ownership.bench.ts +9 -5
  6. package/bench/reactive/array.bench.ts +7 -2
  7. package/bench/reactive/reactive-object.bench.ts +3 -2
  8. package/bench/system.bench.ts +9 -5
  9. package/build/compiler/array.js +28 -25
  10. package/build/compiler/bindings.d.ts +3 -1
  11. package/build/compiler/bindings.js +36 -18
  12. package/build/compiler/index.d.ts +3 -2
  13. package/build/compiler/index.js +21 -13
  14. package/build/compiler/object.js +30 -21
  15. package/build/compiler/plugins/vite.d.ts +29 -6
  16. package/build/compiler/primitives.js +9 -9
  17. package/build/constants.d.ts +3 -1
  18. package/build/constants.js +3 -1
  19. package/build/reactive/array.js +89 -17
  20. package/build/reactive/index.d.ts +1 -1
  21. package/build/reactive/object.d.ts +3 -3
  22. package/build/reactive/object.js +1 -1
  23. package/build/system.d.ts +4 -3
  24. package/build/system.js +48 -16
  25. package/oxlintrc.json +3 -0
  26. package/package.json +15 -7
  27. package/pnpm-workspace.yaml +4 -7
  28. package/src/compiler/array.ts +30 -27
  29. package/src/compiler/bindings.ts +52 -23
  30. package/src/compiler/index.ts +37 -20
  31. package/src/compiler/object.ts +43 -30
  32. package/src/compiler/primitives.ts +13 -10
  33. package/src/constants.ts +6 -1
  34. package/src/reactive/array.ts +133 -24
  35. package/src/reactive/index.ts +3 -3
  36. package/src/reactive/object.ts +4 -4
  37. package/src/system.ts +68 -23
  38. package/test/async-hardening.test.ts +2 -2
  39. package/test/async-iterable.test.ts +3 -3
  40. package/test/compiler/__snapshots__/output.test.ts.snap +157 -0
  41. package/test/compiler/compiler.test.ts +51 -9
  42. package/test/compiler/composition.test.ts +110 -0
  43. package/test/compiler/coverage.test.ts +22 -22
  44. package/test/compiler/origins.test.ts +156 -0
  45. package/test/compiler/output.test.ts +116 -0
  46. package/test/deep-graphs.test.ts +6 -6
  47. package/test/effects.test.ts +5 -5
  48. package/test/errors.test.ts +4 -4
  49. package/test/invalidate.test.ts +2 -2
  50. package/test/ownership.test.ts +30 -30
  51. package/test/pause.test.ts +267 -0
  52. package/test/reactive/array.test.ts +13 -12
  53. package/test/reactive/reactive.test.ts +6 -6
  54. package/test/signal-selector.test.ts +4 -4
  55. package/test/system.test.ts +13 -13
  56. package/test/untrack.test.ts +10 -10
  57. package/tsconfig.build.json +3 -0
  58. package/tsconfig.json +2 -2
  59. package/vitest.config.ts +2 -2
  60. package/bench/tsconfig.json +0 -16
  61. package/test/tsconfig.json +0 -16
@@ -1,8 +1,7 @@
1
1
  import { ts } from '@esportsplus/typescript';
2
2
  import { code, uid } from '@esportsplus/typescript/compiler';
3
3
  import { NAMESPACE, TYPES } from './constants.js';
4
- function analyzeProperty(prop, bindings) {
5
- let sourceFile = bindings.sourceFile;
4
+ function analyzeProperty(prop, typed) {
6
5
  if (!ts.isPropertyAssignment(prop)) {
7
6
  return null;
8
7
  }
@@ -13,7 +12,7 @@ function analyzeProperty(prop, bindings) {
13
12
  else {
14
13
  return null;
15
14
  }
16
- let unwrapped = prop.initializer, value = unwrapped, valueText = value.getText(sourceFile);
15
+ let unwrapped = prop.initializer, value = unwrapped;
17
16
  while (ts.isAsExpression(unwrapped) || ts.isTypeAssertion(unwrapped) || ts.isParenthesizedExpression(unwrapped)) {
18
17
  unwrapped = unwrapped.expression;
19
18
  }
@@ -23,11 +22,11 @@ function analyzeProperty(prop, bindings) {
23
22
  (ts.isTypeReferenceNode(type) &&
24
23
  ts.isIdentifier(type.typeName) &&
25
24
  type.typeName.text === 'Array')) {
26
- return { isStatic: false, key, type: TYPES.Array, valueText };
25
+ return { isStatic: false, key, type: TYPES.Array, value };
27
26
  }
28
27
  }
29
28
  if (ts.isArrowFunction(unwrapped) || ts.isFunctionExpression(unwrapped)) {
30
- return { isStatic: false, key, type: TYPES.Computed, valueText };
29
+ return { isStatic: false, key, type: TYPES.Computed, value };
31
30
  }
32
31
  if (ts.isArrayLiteralExpression(unwrapped)) {
33
32
  let elements = unwrapped.elements, isStatic = value === unwrapped;
@@ -36,23 +35,23 @@ function analyzeProperty(prop, bindings) {
36
35
  isStatic = false;
37
36
  }
38
37
  }
39
- return { isStatic, key, type: TYPES.Array, valueText };
38
+ return { isStatic, key, type: TYPES.Array, value };
40
39
  }
41
- let type = isStaticValue(unwrapped) ? undefined : bindings.checker.getTypeAtLocation(value);
42
- if (type && (bindings.checker.isArrayType(type) || bindings.checker.isTupleType(type))) {
43
- return { isStatic: false, key, type: TYPES.Array, valueText };
44
- }
45
- return {
40
+ let property = {
46
41
  isStatic: isStaticValue(value),
47
42
  key,
48
43
  type: TYPES.Signal,
49
- valueText
44
+ value
50
45
  };
46
+ if (!isStaticValue(unwrapped)) {
47
+ typed.push(property);
48
+ }
49
+ return property;
51
50
  }
52
- function buildClassCode(classname, properties, typehint) {
51
+ function buildClassCode(classname, properties, sourceFile, typehint) {
53
52
  let accessors = [], body = [], constraint = [], fields = [], generics = [], parameters = [], setters = 0;
54
53
  for (let i = 0, n = properties.length; i < n; i++) {
55
- let { isStatic, key, type, valueText } = properties[i], generic = typehint ? `T['${key}']` : `T${parameters.length}`, parameter = `_p${parameters.length}`;
54
+ let { isStatic, key, type, value: initializer } = properties[i], generic = typehint ? `T['${key}']` : `T${parameters.length}`, parameter = `_p${parameters.length}`;
56
55
  if (typehint && type === TYPES.Signal) {
57
56
  constraint.push(`'${key}'?: unknown`);
58
57
  isStatic = false;
@@ -68,7 +67,7 @@ function buildClassCode(classname, properties, typehint) {
68
67
  ${NAMESPACE}.write(this.#${key}, ${value});
69
68
  }
70
69
  `);
71
- fields.push(`#${key} = this[${NAMESPACE}.SIGNAL](${valueText});`);
70
+ fields.push(`#${key} = this[${NAMESPACE}.SIGNAL](${initializer.getText(sourceFile)});`);
72
71
  }
73
72
  else {
74
73
  accessors.push(`
@@ -157,7 +156,7 @@ function visit(ctx, node) {
157
156
  node.forEachChild(n => visit(ctx, n));
158
157
  return;
159
158
  }
160
- let analyzed = analyzeProperty(prop, ctx.bindings);
159
+ let analyzed = analyzeProperty(prop, ctx.typed);
161
160
  if (!analyzed) {
162
161
  node.forEachChild(n => visit(ctx, n));
163
162
  return;
@@ -177,21 +176,31 @@ function visit(ctx, node) {
177
176
  }
178
177
  export default (sourceFile, bindings, isReactiveCall) => {
179
178
  let ctx = {
180
- bindings,
181
179
  calls: [],
182
180
  isReactiveCall,
183
- sourceFile
181
+ sourceFile,
182
+ typed: []
184
183
  };
185
184
  visit(ctx, sourceFile);
185
+ if (ctx.typed.length > 0) {
186
+ let types = bindings.checker.getTypeAtLocation(ctx.typed.map(property => property.value));
187
+ for (let i = 0, n = ctx.typed.length; i < n; i++) {
188
+ let type = types[i];
189
+ if (type && (bindings.checker.isArrayType(type) || bindings.checker.isTupleType(type))) {
190
+ ctx.typed[i].isStatic = false;
191
+ ctx.typed[i].type = TYPES.Array;
192
+ }
193
+ }
194
+ }
186
195
  let prepend = [], replacements = [];
187
196
  for (let i = 0, n = ctx.calls.length; i < n; i++) {
188
197
  let call = ctx.calls[i], typehint = call.typehint;
189
- prepend.push(buildClassCode(call.classname, call.properties, typehint));
198
+ prepend.push(buildClassCode(call.classname, call.properties, sourceFile, typehint));
190
199
  replacements.push({
191
- generate: () => {
200
+ generate: (_sourceFile, text) => {
192
201
  let args = call.properties
193
202
  .filter(({ isStatic, type }) => typehint || !isStatic || type !== TYPES.Signal)
194
- .map(p => p.valueText)
203
+ .map(p => text(p.value))
195
204
  .join(', ');
196
205
  return typehint
197
206
  ? ` new ${call.classname}<${typehint}>(${args})`
@@ -1,8 +1,10 @@
1
1
  declare const _default: ({ root }?: {
2
2
  root?: string;
3
3
  }) => {
4
- closeBundle: () => void;
5
- closeWatcher: () => void;
4
+ buildEnd: () => void;
5
+ buildStart: () => void;
6
+ closeBundle: () => Promise<void> | undefined;
7
+ closeWatcher: () => Promise<void>;
6
8
  configResolved: (config: unknown) => void;
7
9
  enforce: 'pre';
8
10
  handleHotUpdate: <M>(ctx: {
@@ -10,20 +12,41 @@ declare const _default: ({ root }?: {
10
12
  modules: M[];
11
13
  server: {
12
14
  moduleGraph: {
15
+ getModuleById?(id: string): M | undefined;
13
16
  getModulesByFile(file: string): Set<M> | undefined;
14
17
  invalidateModule(module: M, seen: Set<M>, timestamp: number, isHmr: boolean): void;
15
18
  };
16
19
  };
17
20
  timestamp: number;
18
21
  }) => M[] | undefined;
22
+ load: {
23
+ filter: {
24
+ id: RegExp;
25
+ };
26
+ handler: (id: string) => string | null;
27
+ };
19
28
  name: string;
20
29
  renderChunk: (code: string, chunk: {
21
30
  fileName: string;
22
31
  }) => null;
23
- transform: (code: string, id: string) => {
24
- code: string;
25
- map: import("@esportsplus/typescript/compiler").SourceMapV3;
26
- } | null;
32
+ resolveId: {
33
+ filter: {
34
+ id: RegExp;
35
+ };
36
+ handler: (id: string) => string | null;
37
+ };
38
+ transform: {
39
+ filter: {
40
+ id: {
41
+ exclude: RegExp[];
42
+ include: RegExp;
43
+ };
44
+ };
45
+ handler: (this: unknown, code: string, id: string) => {
46
+ code: string;
47
+ map: import("@esportsplus/typescript/compiler").SourceMapV3;
48
+ } | null;
49
+ };
27
50
  watchChange: (id: string) => void;
28
51
  };
29
52
  export default _default;
@@ -86,7 +86,7 @@ function isPatternTarget(node) {
86
86
  function readNode(identifier) {
87
87
  let parent = identifier.parent;
88
88
  if ((ts.isPropertyAccessExpression(parent) && parent.name === identifier) ||
89
- (ts.isElementAccessExpression(parent) && parent.argumentExpression === identifier)) {
89
+ (ts.isElementAccessExpression(parent) && parent.argumentExpression === identifier && (ts.isStringLiteral(identifier) || ts.isNoSubstitutionTemplateLiteral(identifier)))) {
90
90
  return parent;
91
91
  }
92
92
  return identifier;
@@ -103,10 +103,10 @@ function reference(ctx, node, kind) {
103
103
  ctx.failures.push({ message: 'a reactive binding cannot be assigned through a destructuring pattern; assign it directly', node });
104
104
  return;
105
105
  }
106
- let text = node.getText(ctx.bindings.sourceFile);
106
+ let binding = node.getText(ctx.bindings.sourceFile);
107
107
  if (ts.isShorthandPropertyAssignment(parent)) {
108
108
  ctx.replacements.push({
109
- generate: () => `${text}: ${NAMESPACE}.read(${text})`,
109
+ generate: () => `${binding}: ${NAMESPACE}.read(${binding})`,
110
110
  node: parent
111
111
  });
112
112
  return;
@@ -129,7 +129,7 @@ function reference(ctx, node, kind) {
129
129
  }
130
130
  if (!writeCtx) {
131
131
  ctx.replacements.push({
132
- generate: () => `${NAMESPACE}.read(${text})`,
132
+ generate: () => `${NAMESPACE}.read(${binding})`,
133
133
  node
134
134
  });
135
135
  return;
@@ -141,14 +141,14 @@ function reference(ctx, node, kind) {
141
141
  if (writeCtx === 'simple' && ts.isBinaryExpression(parent)) {
142
142
  let right = parent.right;
143
143
  ctx.replacements.push({
144
- generate: (sf) => `${NAMESPACE}.write(${text}, ${right.getText(sf)})`,
144
+ generate: (_sourceFile, text) => `${NAMESPACE}.write(${binding}, ${text(right)})`,
145
145
  node: parent
146
146
  });
147
147
  }
148
148
  else if (writeCtx === 'compound' && ts.isBinaryExpression(parent)) {
149
149
  let op = COMPOUND_OPERATORS.get(parent.operatorToken.kind) ?? '+', right = parent.right;
150
150
  ctx.replacements.push({
151
- generate: (sf) => `${NAMESPACE}.write(${text}, ${text}.value ${op} ${right.getText(sf)})`,
151
+ generate: (_sourceFile, text) => `${NAMESPACE}.write(${binding}, ${binding}.value ${op} ${text(right)})`,
152
152
  node: parent
153
153
  });
154
154
  }
@@ -156,20 +156,20 @@ function reference(ctx, node, kind) {
156
156
  let unary = parent, delta = unary.operator === ts.SyntaxKind.PlusPlusToken ? '+ 1' : '- 1';
157
157
  if (ts.isExpressionStatement(unary.parent)) {
158
158
  ctx.replacements.push({
159
- generate: () => `${NAMESPACE}.write(${text}, ${text}.value ${delta})`,
159
+ generate: () => `${NAMESPACE}.write(${binding}, ${binding}.value ${delta})`,
160
160
  node: unary
161
161
  });
162
162
  }
163
163
  else if (ts.isPrefixUnaryExpression(unary)) {
164
164
  ctx.replacements.push({
165
- generate: () => `(${NAMESPACE}.write(${text}, ${text}.value ${delta}), ${text}.value)`,
165
+ generate: () => `(${NAMESPACE}.write(${binding}, ${binding}.value ${delta}), ${binding}.value)`,
166
166
  node: unary
167
167
  });
168
168
  }
169
169
  else {
170
170
  let tmp = `_t${ctx.tmpCounter++}`;
171
171
  ctx.replacements.push({
172
- generate: () => `((${tmp}) => (${NAMESPACE}.write(${text}, ${tmp} ${delta}), ${tmp}))(${text}.value)`,
172
+ generate: () => `((${tmp}) => (${NAMESPACE}.write(${binding}, ${tmp} ${delta}), ${tmp}))(${binding}.value)`,
173
173
  node: unary
174
174
  });
175
175
  }
@@ -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' });
@@ -1,6 +1,6 @@
1
- import type { Reactive } from '../types.js';
2
1
  import { ReactiveArray } from './array.js';
3
2
  import { ReactiveObject } from './object.js';
3
+ import type { Reactive } from '../types.js';
4
4
  type Guard<T> = T extends Record<PropertyKey, unknown> ? T extends {
5
5
  dispose: unknown;
6
6
  } ? {
@@ -1,12 +1,12 @@
1
1
  import { COMPUTED, REACTIVE_ARRAY, SIGNAL } from '../constants.js';
2
- import { Computed } from '../types.js';
3
2
  import { ReactiveArray } from './array.js';
3
+ import type { Computed } from '../types.js';
4
4
  declare class ReactiveObject<T extends Record<PropertyKey, unknown>> {
5
5
  protected disposers: VoidFunction[] | null;
6
6
  constructor(data: T | null);
7
- protected [COMPUTED]<T extends Computed<ReturnType<T>>['fn']>(value: T): import("../types.js").ComputedResult<ReturnType<T>>;
7
+ protected [COMPUTED]<T extends Computed<ReturnType<T>>['fn']>(value: T): import("../index.js").ComputedResult<ReturnType<T>>;
8
8
  protected [REACTIVE_ARRAY]<U>(value: U[]): ReactiveArray<U>;
9
- protected [SIGNAL]<T>(value: T): import("../types.js").Signal<T>;
9
+ protected [SIGNAL]<T>(value: T): import("../index.js").Signal<T>;
10
10
  dispose(): void;
11
11
  }
12
12
  declare const isReactiveObject: (value: unknown) => value is ReactiveObject<Record<PropertyKey, unknown>>;
@@ -58,7 +58,7 @@ class ReactiveObject {
58
58
  if (!disposers) {
59
59
  return;
60
60
  }
61
- while (disposer = disposers.pop()) {
61
+ while ((disposer = disposers.pop())) {
62
62
  disposer();
63
63
  }
64
64
  }
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/oxlintrc.json ADDED
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": ["./node_modules/@esportsplus/typescript/oxlintrc.json"]
3
+ }