@adbl/cells 0.0.11 → 0.0.13

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/README.md CHANGED
@@ -72,20 +72,6 @@ count.value = 3; // Output: "Count changed to: 3"
72
72
  count.value = 7; // Output: "Count changed to: 7"
73
73
  ```
74
74
 
75
- ### 4. Global Effects
76
-
77
- Cells allows you to set up global effects that run before or after any cell is updated, giving you fine-grained control over your application's reactive behavior.
78
-
79
- ```javascript
80
- Cell.beforeUpdate((value) => {
81
- console.log(`About to update a cell with value: ${value}`);
82
- });
83
-
84
- Cell.afterUpdate((value) => {
85
- console.log(`Just updated a cell with value: ${value}`);
86
- });
87
- ```
88
-
89
75
  ### 5. Batch Updates
90
76
 
91
77
  When you need to perform multiple updates but only want to trigger effects once, you can use batch updates to optimize performance:
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from './library/index.js';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.js"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC"}
@@ -1,59 +1,23 @@
1
1
  /**
2
- * @template {*} T
2
+ * @template {*} T The type of value stored in the cell
3
+ *
4
+ * Base class for reactive cells.
5
+ * This class should not be instantiated directly - use `Cell.source` or
6
+ * `Cell.derived` instead.
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * // Create a source cell
11
+ * const count = Cell.source(0);
12
+ *
13
+ * // Listen to changes
14
+ * count.listen(value => console.log('Count changed:', value));
15
+ *
16
+ * // Update the value
17
+ * count.value = 1; // Logs: Count changed: 1
18
+ * ```
3
19
  */
4
20
  export class Cell<T extends unknown> {
5
- /**
6
- * Adds a global effect that runs before any Cell is updated.
7
- * @param {(value: unknown) => void} effect - The effect function.
8
- * @param {Partial<import('./root.js').GlobalEffectOptions>} [options] - The options for the effect.
9
- * @example
10
- * ```
11
- * import { Cell } from '@adbl/cells';
12
- *
13
- * const cell = Cell.source(0);
14
- * Cell.beforeUpdate((value) => console.log(value));
15
- *
16
- * cell.value = 1; // prints 1
17
- * cell.value = 2; // prints 2
18
- * ```
19
- */
20
- static beforeUpdate: (effect: (value: unknown) => void, options?: Partial<import("./root.js").GlobalEffectOptions> | undefined) => void;
21
- /**
22
- * Adds a global post-update effect to the Cell system.
23
- * @param {(value: unknown) => void} effect - The effect function to add.
24
- * @param {Partial<import('./root.js').GlobalEffectOptions>} [options] - Options for the effect.
25
- * @example
26
- * ```
27
- * import { Cell } from '@adbl/cells';
28
- *
29
- * const effect = (value) => console.log(value);
30
- * Cell.afterUpdate(effect);
31
- *
32
- * const cell = Cell.source(0);
33
- * cell.value = 1; // prints 1
34
- * ```
35
- */
36
- static afterUpdate: (effect: (value: unknown) => void, options?: Partial<import("./root.js").GlobalEffectOptions> | undefined) => void;
37
- static removeGlobalEffects: () => void;
38
- /**
39
- * Removes a global effect.
40
- * @param {(value: unknown) => void} effect - The effect function added previously.
41
- * @example
42
- * ```
43
- * import { Cell } from '@adbl/cells';
44
- *
45
- * const effect = (value) => console.log(value);
46
- * Cell.beforeUpdate(effect);
47
- *
48
- * const cell = Cell.source(0);
49
- * cell.value = 1; // prints 1
50
- *
51
- * Cell.removeGlobalEffect(effect);
52
- *
53
- * cell.value = 2; // prints nothing
54
- * ```
55
- */
56
- static removeGlobalEffect: (effect: (value: unknown) => void) => void;
57
21
  /**
58
22
  * @template T
59
23
  * Creates a new Cell instance with the provided value.
@@ -162,6 +126,9 @@ export class Cell<T extends unknown> {
162
126
  * @returns {T} The value of the Cell.
163
127
  */
164
128
  valueOf(): T;
129
+ /**
130
+ * Gets the current value of the cell.
131
+ */
165
132
  get value(): T;
166
133
  /**
167
134
  * Stringifies the value of the Cell.
@@ -204,10 +171,11 @@ export class Cell<T extends unknown> {
204
171
  */
205
172
  stopListeningTo(name: string): void;
206
173
  /**
174
+ * @protected
207
175
  * Updates the root object and notifies any registered watchers and computed dependents.
208
176
  * This method is called whenever the root object's value changes.
209
177
  */
210
- update(): void;
178
+ protected update(): void;
211
179
  /**
212
180
  * Returns the current value of the cell without registering a watcher.
213
181
  * @returns {T} - The current value of the cell.
@@ -226,8 +194,11 @@ export class DerivedCell<T extends unknown> extends Cell<T> {
226
194
  * @param {() => T} computedFn - A function that generates the value of the computed.
227
195
  */
228
196
  constructor(computedFn: () => T);
197
+ /** @type {WeakRef<this>} */
198
+ ref: WeakRef<this>;
229
199
  /** @type {() => T} */
230
200
  computedFn: () => T;
201
+ initialized: boolean;
231
202
  /**
232
203
  * @readonly
233
204
  */
@@ -236,10 +207,41 @@ export class DerivedCell<T extends unknown> extends Cell<T> {
236
207
  * @readonly
237
208
  */
238
209
  readonly get value(): T;
210
+ /**
211
+ * Listens for changes to the cell, initializing the value if not already done.
212
+ * @param {(newValue: T) => void} callback - The function to call when the cell's value changes.
213
+ * @param {object} [options] - Optional configuration for listening.
214
+ */
215
+ listen(callback: (newValue: T) => void, options?: object | undefined): () => void;
216
+ /**
217
+ * Runs the callback and sets up a listener, initializing the cell's value if not already done.
218
+ * @param {(newValue: T) => void} callback - The function to call when the cell's value changes.
219
+ * @param {object} [options] - Optional configuration for listening and running.
220
+ * @returns {*} The result of the parent class's runAndListen method.
221
+ */
222
+ runAndListen(callback: (newValue: T) => void, options?: object | undefined): any;
223
+ deproxy(): void;
239
224
  }
240
225
  /**
241
226
  * @template {*} T
242
227
  * @extends {Cell<T>}
228
+ * A cell whose value can be directly modified.
229
+ * Source cells are the primary way to introduce reactivity.
230
+ * They can hold any value type and will automatically handle proxying of objects
231
+ * to enable deep reactivity when needed.
232
+ *
233
+ * @example
234
+ * ```typescript
235
+ * const count = Cell.source(0);
236
+ * ```
237
+ *
238
+ * @example
239
+ * ```typescript
240
+ * // With options
241
+ * const immutableCell = Cell.source(42, { immutable: true });
242
+ * // Will throw error:
243
+ * immutableCell.value = 43;
244
+ * ```
243
245
  */
244
246
  export class SourceCell<T extends unknown> extends Cell<T> {
245
247
  /**
@@ -248,8 +250,7 @@ export class SourceCell<T extends unknown> extends Cell<T> {
248
250
  * @param {Partial<CellOptions<T>>} [options]
249
251
  */
250
252
  constructor(value: T, options?: Partial<CellOptions<T>> | undefined);
251
- /** @type {Partial<CellOptions<T>>} */
252
- options: Partial<CellOptions<T>>;
253
+ options: Partial<CellOptions<T>> | undefined;
253
254
  /**
254
255
  * For cells containing objects, returns the object itself.
255
256
  * This can be useful in scenarios where unfettered access to the original object is needed,
@@ -257,10 +258,10 @@ export class SourceCell<T extends unknown> extends Cell<T> {
257
258
  *
258
259
  * @example
259
260
  * const cell = new SourceCell({ a: 1, b: 2 });
260
- * console.log(cell.originalObject); // { a: 1, b: 2 }
261
+ * console.log(cell.deproxy()); // { a: 1, b: 2 }
261
262
  *
262
263
  * cell.value = { a: 3, b: 4 };
263
- * console.log(cell.originalObject); // { a: 3, b: 4 }
264
+ * console.log(cell.deproxy()); // { a: 3, b: 4 }
264
265
  *
265
266
  * @returns {T extends object ? T : never} The original object if T is an object, otherwise never.
266
267
  */
@@ -273,6 +274,27 @@ export class SourceCell<T extends unknown> extends Cell<T> {
273
274
  get value(): T;
274
275
  #private;
275
276
  }
277
+ /**
278
+ * An error class that aggregates multiple errors thrown during a cell update cycle.
279
+ *
280
+ * This error is thrown when one or more errors are encountered while updating cells,
281
+ * such as when running effect callbacks or computed updates. The `errors` property
282
+ * contains an array of all the errors that occurred during the update cycle.
283
+ *
284
+ * @example
285
+ * try {
286
+ * // Some cell update logic that may throw
287
+ * } catch (e) {
288
+ * if (e instanceof CellUpdateError) {
289
+ * console.error('Multiple errors occurred:', e.errors);
290
+ * }
291
+ * }
292
+ */
293
+ export class CellUpdateError extends Error {
294
+ /** @param {Error[]} errors */
295
+ constructor(errors: Error[]);
296
+ errors: Error[];
297
+ }
276
298
  export type AsyncRequestAtoms<Input, Output> = {
277
299
  /**
278
300
  * Represents the loading state of an asynchronous request.