@ibodr/state 0.2.5 → 0.2.7

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/dist/index.d.ts CHANGED
@@ -1,122 +1,52 @@
1
1
  /**
2
2
  * An ArraySet operates as an array until it reaches a certain size, after which a Set is used
3
3
  * instead. In either case, the same methods are used to get, set, remove, and visit the items.
4
+ *
5
+ * `set` and `array` are never both non-null. `set` being null means array mode, but the array
6
+ * itself is only allocated on the first `add` (most signals never get a child, and an empty
7
+ * ArraySet is created for every atom and effect, and two for every computed), so array-mode code
8
+ * must handle `array === null`; `arraySize` is 0 in that state. Once promoted to a set, an
9
+ * ArraySet never goes back.
4
10
  * @internal
5
11
  */
6
12
  declare class ArraySet<T> {
7
- private arraySize;
8
- private array;
9
13
  private set;
14
+ private array;
15
+ private arraySize;
10
16
  /**
11
17
  * Get whether this ArraySet has any elements.
12
- *
13
- * @returns True if this ArraySet has any elements, false otherwise.
14
18
  */
15
19
  get isEmpty(): boolean;
16
20
  /**
17
21
  * Add an element to the ArraySet if it is not already present.
18
22
  *
19
- * @param elem - The element to add to the set
20
23
  * @returns `true` if the element was added, `false` if it was already present
21
- * @example
22
- * ```ts
23
- * const arraySet = new ArraySet<string>()
24
- *
25
- * console.log(arraySet.add('hello')) // true
26
- * console.log(arraySet.add('hello')) // false (already exists)
27
- * ```
28
24
  */
29
25
  add(elem: T): boolean;
30
26
  /**
31
27
  * Remove an element from the ArraySet if it is present.
32
28
  *
33
- * @param elem - The element to remove from the set
34
29
  * @returns `true` if the element was removed, `false` if it was not present
35
- * @example
36
- * ```ts
37
- * const arraySet = new ArraySet<string>()
38
- * arraySet.add('hello')
39
- *
40
- * console.log(arraySet.remove('hello')) // true
41
- * console.log(arraySet.remove('hello')) // false (not present)
42
- * ```
43
30
  */
44
31
  remove(elem: T): boolean;
45
32
  /**
46
33
  * Execute a callback function for each element in the ArraySet.
47
- *
48
- * @param visitor - A function to call for each element in the set
49
- * @example
50
- * ```ts
51
- * const arraySet = new ArraySet<string>()
52
- * arraySet.add('hello')
53
- * arraySet.add('world')
54
- *
55
- * arraySet.visit((item) => {
56
- * console.log(item) // 'hello', 'world'
57
- * })
58
- * ```
59
34
  */
60
35
  visit(visitor: (item: T) => void): void;
61
36
  /**
62
37
  * Make the ArraySet iterable, allowing it to be used in for...of loops and with spread syntax.
63
- *
64
- * @returns An iterator that yields each element in the set
65
- * @example
66
- * ```ts
67
- * const arraySet = new ArraySet<number>()
68
- * arraySet.add(1)
69
- * arraySet.add(2)
70
- *
71
- * for (const item of arraySet) {
72
- * console.log(item) // 1, 2
73
- * }
74
- *
75
- * const items = [...arraySet] // [1, 2]
76
- * ```
77
38
  */
78
39
  [Symbol.iterator](): Generator<T, void, unknown>;
79
40
  /**
80
41
  * Check whether an element is present in the ArraySet.
81
- *
82
- * @param elem - The element to check for
83
- * @returns `true` if the element is present, `false` otherwise
84
- * @example
85
- * ```ts
86
- * const arraySet = new ArraySet<string>()
87
- * arraySet.add('hello')
88
- *
89
- * console.log(arraySet.has('hello')) // true
90
- * console.log(arraySet.has('world')) // false
91
- * ```
92
42
  */
93
43
  has(elem: T): boolean;
94
44
  /**
95
45
  * Remove all elements from the ArraySet.
96
- *
97
- * @example
98
- * ```ts
99
- * const arraySet = new ArraySet<string>()
100
- * arraySet.add('hello')
101
- * arraySet.add('world')
102
- *
103
- * arraySet.clear()
104
- * console.log(arraySet.size()) // 0
105
- * ```
106
46
  */
107
47
  clear(): void;
108
48
  /**
109
49
  * Get the number of elements in the ArraySet.
110
- *
111
- * @returns The number of elements in the set
112
- * @example
113
- * ```ts
114
- * const arraySet = new ArraySet<string>()
115
- * console.log(arraySet.size()) // 0
116
- *
117
- * arraySet.add('hello')
118
- * console.log(arraySet.size()) // 1
119
- * ```
120
50
  */
121
51
  size(): number;
122
52
  }
@@ -130,12 +60,12 @@ declare class ArraySet<T> {
130
60
  *
131
61
  * @example
132
62
  * ```ts
133
- * import { atom, getGlobalEpoch, RESET_VALUE } from '@ibodr/state'
63
+ * import { atom, RESET_VALUE } from '@ibodr/state'
134
64
  *
135
- * const count = atom('count', 0, { historyLength: 3 })
136
- * const oldEpoch = getGlobalEpoch()
65
+ * const count = atom('count', 0, { historyLength: 3, computeDiff: (prev, next) => next - prev })
66
+ * const oldEpoch = count.lastChangedEpoch
137
67
  *
138
- * // Make many changes that exceed history length
68
+ * // Make more changes than the history length can hold
139
69
  * count.set(1)
140
70
  * count.set(2)
141
71
  * count.set(3)
@@ -293,8 +223,8 @@ interface Child {
293
223
  * A function type that computes the difference between two values of a signal.
294
224
  *
295
225
  * This function is used to generate incremental diffs that can be applied to
296
- * reconstruct state changes over time. It's particularly useful for features
297
- * like undo/redo, synchronization, and change tracking.
226
+ * reconstruct state changes over time, so that downstream computeds and effects can
227
+ * update incrementally instead of recomputing from scratch.
298
228
  *
299
229
  * The function should analyze the previous and current values and return a
300
230
  * diff object that represents the change. If the diff cannot be computed
@@ -303,7 +233,7 @@ interface Child {
303
233
  *
304
234
  * @param previousValue - The previous value of the signal
305
235
  * @param currentValue - The current value of the signal
306
- * @param lastComputedEpoch - The epoch when the previous value was set
236
+ * @param lastComputedEpoch - For an atom, the epoch when the previous value was set. For a computed, the epoch at which it was last checked (the same value its compute function receives), so that `other.getDiffSince(lastComputedEpoch)` yields exactly the changes not yet accounted for.
307
237
  * @param currentEpoch - The epoch when the current value was set
308
238
  * @returns A diff object representing the change, or the unique symbol RESET_VALUE if no diff can be computed
309
239
  *
@@ -340,7 +270,7 @@ interface AtomOptions<Value, Diff> {
340
270
  /**
341
271
  * The maximum number of diffs to keep in the history buffer.
342
272
  *
343
- * If you don't need to compute diffs, or if you will supply diffs manually via {@link Atom.set}, you can leave this as `undefined` and no history buffer will be created.
273
+ * If you don't need diffs, leave this as `undefined` and no history buffer will be created. Diffs passed to {@link Atom.set} or produced by {@link AtomOptions.computeDiff} are only recorded when this is set.
344
274
  *
345
275
  * If you expect the value to be part of an active effect subscription all the time, and to not change multiple times inside of a single transaction, you can set this to a relatively low number (e.g. 10).
346
276
  *
@@ -447,10 +377,10 @@ declare function isAtom(value: unknown): value is Atom<unknown>;
447
377
  * @example
448
378
  * ```ts
449
379
  * const name = atom('name', 'Sam')
450
- * const time = atom('time', () => new Date().getTime())
380
+ * const time = atom('time', Date.now())
451
381
  *
452
382
  * setInterval(() => {
453
- * time.set(new Date().getTime())
383
+ * time.set(Date.now())
454
384
  * })
455
385
  *
456
386
  * react('log name changes', () => {
@@ -464,7 +394,8 @@ declare function isAtom(value: unknown): value is Atom<unknown>;
464
394
  declare function unsafe__withoutCapture<T>(fn: () => T): T;
465
395
  /**
466
396
  * A debugging tool that tells you why a computed signal or effect is running.
467
- * Call in the body of a computed signal or effect function.
397
+ * Call in the body of a computed signal or effect function. Nothing is logged for the run that
398
+ * calls it; from the next run on, each run logs the ancestors that changed.
468
399
  *
469
400
  * @example
470
401
  * ```ts
@@ -476,8 +407,8 @@ declare function unsafe__withoutCapture<T>(fn: () => T): T;
476
407
  *
477
408
  * name.set('Alice')
478
409
  *
479
- * // 'greeting' is running because:
480
- * // 'name' changed => 'Alice'
410
+ * // Effect(greeting) is executing because:
411
+ * // ↳ Atom(name) changed
481
412
  * ```
482
413
  *
483
414
  * @public
@@ -612,7 +543,7 @@ interface ComputedOptions<Value, Diff> {
612
543
  /**
613
544
  * The maximum number of diffs to keep in the history buffer.
614
545
  *
615
- * If you don't need to compute diffs, or if you will supply diffs manually via {@link Atom.set}, you can leave this as `undefined` and no history buffer will be created.
546
+ * If you don't need diffs, leave this as `undefined` and no history buffer will be created. Diffs supplied via {@link withDiff} or {@link ComputedOptions.computeDiff} are only recorded when this is set.
616
547
  *
617
548
  * If you expect the value to be part of an active effect subscription all the time, and to not change multiple times inside of a single transaction, you can set this to a relatively low number (e.g. 10).
618
549
  *
@@ -673,7 +604,7 @@ interface Computed<Value, Diff = unknown> extends Signal<Value, Diff> {
673
604
  * ```ts
674
605
  * class Counter {
675
606
  * max = 100
676
- * count = atom(0)
607
+ * count = atom('count', 0)
677
608
  *
678
609
  * @computed getRemaining() {
679
610
  * return this.max - this.count.get()
@@ -691,7 +622,7 @@ interface Computed<Value, Diff = unknown> extends Signal<Value, Diff> {
691
622
  * @param propertyName - The property name
692
623
  * @public
693
624
  */
694
- declare function getComputedInstance<Obj extends object, Prop extends keyof Obj>(obj: Obj, propertyName: Prop): Computed<Obj[Prop]>;
625
+ declare function getComputedInstance<Obj extends object, Prop extends keyof Obj>(obj: Obj, propertyName: Prop): Computed<Obj[Prop] extends () => infer Value ? Value : Obj[Prop]>;
695
626
  /**
696
627
  * Creates a computed signal that derives its value from other signals.
697
628
  * Computed signals automatically update when their dependencies change and use lazy evaluation
@@ -710,7 +641,7 @@ declare function getComputedInstance<Obj extends object, Prop extends keyof Obj>
710
641
  * ```ts
711
642
  * class Counter {
712
643
  * max = 100
713
- * count = atom<number>(0)
644
+ * count = atom('count', 0)
714
645
  *
715
646
  * @computed getRemaining() {
716
647
  * return this.max - this.count.get()
@@ -724,7 +655,7 @@ declare function getComputedInstance<Obj extends object, Prop extends keyof Obj>
724
655
  * ```ts
725
656
  * class Counter {
726
657
  * max = 100
727
- * count = atom<number>(0)
658
+ * count = atom('count', 0)
728
659
  *
729
660
  * @computed({isEqual: (a, b) => a === b})
730
661
  * getRemaining() {
@@ -828,12 +759,9 @@ interface EffectSchedulerOptions {
828
759
  * }
829
760
  * }
830
761
  * const stop = react('set page title', () => {
831
- * document.title = doc.title,
832
- * }, scheduleEffect)
762
+ * document.title = doc.title
763
+ * }, { scheduleEffect })
833
764
  * ```
834
- *
835
- * @param execute - A function that will execute the effect.
836
- * @returns void
837
765
  */
838
766
  scheduleEffect?: (execute: () => void) => void;
839
767
  }
@@ -1062,7 +990,7 @@ declare function localStorageAtom<Value, Diff = unknown>(name: string, initialVa
1062
990
  * // Logs "Hello, Jane Smith!"
1063
991
  * ```
1064
992
  *
1065
- * If the function throws, the transaction is aborted and any signals that were updated during the transaction revert to their state before the transaction began.
993
+ * If the function throws, the transaction is aborted and any signals that were updated during the transaction revert to their state before the transaction began. An aborted transaction still flushes effects: effects whose parents went through a change-and-restore round trip are checked again and, if a parent's value differs from what they last saw (an atom they read directly always will), run once more with the restored values.
1066
994
  *
1067
995
  * @example
1068
996
  * ```ts
@@ -1080,8 +1008,9 @@ declare function localStorageAtom<Value, Diff = unknown>(name: string, initialVa
1080
1008
  * throw new Error('oops')
1081
1009
  * })
1082
1010
  *
1083
- * // Does not log
1084
1011
  * // firstName.get() === 'John'
1012
+ * // Logs "Hello, John Doe!" again: effects whose parents were changed and restored still run,
1013
+ * // and observe the restored values.
1085
1014
  * ```
1086
1015
  *
1087
1016
  * A `rollback` callback is passed into the function.
@@ -1104,9 +1033,9 @@ declare function localStorageAtom<Value, Diff = unknown>(name: string, initialVa
1104
1033
  * rollback()
1105
1034
  * })
1106
1035
  *
1107
- * // Does not log
1108
1036
  * // firstName.get() === 'John'
1109
1037
  * // lastName.get() === 'Doe'
1038
+ * // Logs "Hello, John Doe!" again, as above.
1110
1039
  * ```
1111
1040
  *
1112
1041
  * @param fn - The function to run in a transaction, called with a function to roll back the change.