@adbl/cells 0.0.5 → 0.0.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.
@@ -157,7 +157,7 @@ class Effect {
157
157
  }
158
158
 
159
159
  /**
160
- * @template T
160
+ * @template {*} T
161
161
  */
162
162
  export class Cell {
163
163
  /**
@@ -170,20 +170,13 @@ export class Cell {
170
170
  */
171
171
  #derivedCells = [];
172
172
 
173
- /**
174
- * @readonly
175
- */
176
- get effects() {
177
- return this.#effects;
178
- }
179
-
180
173
  /**
181
174
  * @readonly
182
175
  * @returns {Array<DerivedCell<any>>}
183
176
  */
184
177
  get derivedCells() {
185
178
  // @ts-ignore
186
- return this.#derivedCells.map((cell) => cell.deref()).filter(Boolean);
179
+ return this.#derivedCells.map((cell) => cell[0].deref()).filter(Boolean);
187
180
  }
188
181
 
189
182
  /**
@@ -242,14 +235,6 @@ export class Cell {
242
235
  return this.wvalue;
243
236
  }
244
237
 
245
- /**
246
- * Sets a callback function that will be called whenever the value of the Cell changes.
247
- * @param {(newValue: T) => void} callback - The function to be called when the value changes.
248
- */
249
- set onchange(callback) {
250
- this.listen(callback);
251
- }
252
-
253
238
  /**
254
239
  * Adds the provided effect callback to the list of effects for this cell, and returns a function that can be called to remove the effect.
255
240
  * @param {(newValue: T) => void} callback - The effect callback to add.
@@ -386,16 +371,20 @@ export class Cell {
386
371
  */
387
372
  update() {
388
373
  // Run watchers.
389
- for (const effect of this.#effects) {
390
- const watcher = effect.callback;
374
+ const batchNestingLevel = root.batchNestingLevel;
375
+ const wvalue = this.wvalue;
376
+ const effects = this.#effects;
377
+ const len = effects.length;
378
+
379
+ for (let i = 0; i < len; i++) {
380
+ const watcher = effects[i].callback;
391
381
  if (watcher === undefined) continue;
392
382
 
393
- if (root.batchNestingLevel > 0) {
394
- root.batchedEffects.set(watcher, [this.wvalue]);
395
- continue;
383
+ if (batchNestingLevel > 0) {
384
+ root.batchedEffects.set(watcher, [wvalue]);
385
+ } else {
386
+ watcher(wvalue);
396
387
  }
397
-
398
- watcher(this.wvalue);
399
388
  }
400
389
 
401
390
  // Remove dead effects.
@@ -710,7 +699,7 @@ export class Cell {
710
699
  /**
711
700
  * A class that represents a computed value that depends on other reactive values.
712
701
  * The computed value is automatically updated when any of its dependencies change.
713
- * @template T
702
+ * @template {*} T
714
703
  * @extends {Cell<T>}
715
704
  */
716
705
  export class DerivedCell extends Cell {
@@ -719,9 +708,14 @@ export class DerivedCell extends Cell {
719
708
  */
720
709
  constructor(computedFn) {
721
710
  super();
722
- activeComputedValues.push([this, computedFn]);
723
- this.setValue(computedFn());
724
- activeComputedValues.pop();
711
+ // Ensures that the cell is derived every time the computing function is called.
712
+ const derivationWrapper = () => {
713
+ activeComputedValues.push([this, derivationWrapper]);
714
+ const value = computedFn();
715
+ activeComputedValues.pop();
716
+ return value;
717
+ };
718
+ this.setValue(derivationWrapper());
725
719
  }
726
720
 
727
721
  /**
@@ -740,7 +734,7 @@ export class DerivedCell extends Cell {
740
734
  }
741
735
 
742
736
  /**
743
- * @template T
737
+ * @template {*} T
744
738
  * @extends {Cell<T>}
745
739
  */
746
740
  export class SourceCell extends Cell {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adbl/cells",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "description": "A simple implementation of reactive updates for JavaScript",
5
5
  "main": "index.js",
6
6
  "private": false,
@@ -1,7 +1,7 @@
1
1
  /**
2
- * @template T
2
+ * @template {*} T
3
3
  */
4
- export class Cell<T> {
4
+ export class Cell<T extends unknown> {
5
5
  /**
6
6
  * Adds a global effect that runs before any Cell is updated.
7
7
  * @param {(value: unknown) => void} effect - The effect function.
@@ -122,7 +122,7 @@ export class Cell<T> {
122
122
  * @param {T} object - The object to be flattened.
123
123
  * @returns {{ [K in keyof T]: T[K] extends Cell<infer U> ? U : T[K] }} A new object with the flattened values.
124
124
  */
125
- static flattenObject: <T_6 extends object>(object: T_6) => { [K in keyof T_6]: T_6[K] extends Cell<infer U_1> ? U_1 : T_6[K]; };
125
+ static flattenObject: <T_6 extends object>(object: T_6) => { [K in keyof T_6]: T_6[K] extends Cell<infer U_1 extends unknown> ? U_1 : T_6[K]; };
126
126
  /**
127
127
  * Wraps an asynchronous function with managed state.
128
128
  *
@@ -141,10 +141,6 @@ export class Cell<T> {
141
141
  * run('input');
142
142
  */
143
143
  static async<X, Y>(getter: (input: X) => Promise<Y>): AsyncRequestAtoms<X, Y>;
144
- /**
145
- * @readonly
146
- */
147
- readonly get effects(): Effect<T>[];
148
144
  /**
149
145
  * @readonly
150
146
  * @returns {Array<DerivedCell<any>>}
@@ -175,11 +171,6 @@ export class Cell<T> {
175
171
  * @protected @type {T}
176
172
  */
177
173
  protected get revalued(): T;
178
- /**
179
- * Sets a callback function that will be called whenever the value of the Cell changes.
180
- * @param {(newValue: T) => void} callback - The function to be called when the value changes.
181
- */
182
- set onchange(callback: (newValue: T) => void);
183
174
  /**
184
175
  * Adds the provided effect callback to the list of effects for this cell, and returns a function that can be called to remove the effect.
185
176
  * @param {(newValue: T) => void} callback - The effect callback to add.
@@ -225,10 +216,10 @@ export class Cell<T> {
225
216
  /**
226
217
  * A class that represents a computed value that depends on other reactive values.
227
218
  * The computed value is automatically updated when any of its dependencies change.
228
- * @template T
219
+ * @template {*} T
229
220
  * @extends {Cell<T>}
230
221
  */
231
- export class DerivedCell<T> extends Cell<T> {
222
+ export class DerivedCell<T extends unknown> extends Cell<T> {
232
223
  /**
233
224
  * @param {() => T} computedFn - A function that generates the value of the computed.
234
225
  */
@@ -243,10 +234,10 @@ export class DerivedCell<T> extends Cell<T> {
243
234
  readonly get value(): T;
244
235
  }
245
236
  /**
246
- * @template T
237
+ * @template {*} T
247
238
  * @extends {Cell<T>}
248
239
  */
249
- export class SourceCell<T> extends Cell<T> {
240
+ export class SourceCell<T extends unknown> extends Cell<T> {
250
241
  /**
251
242
  * Creates a new Cell with the provided value.
252
243
  * @param {T} value
@@ -340,28 +331,3 @@ export type NeverIfAny<T> = 0 extends (1 & T) ? never : T;
340
331
  export type Reference<T> = {
341
332
  deref: () => T | undefined;
342
333
  };
343
- /**
344
- * @template T
345
- * @typedef {{
346
- * deref: () => T | undefined
347
- * }} Reference
348
- */
349
- /** @template T */
350
- declare class Effect<T> {
351
- /**
352
- * @param {(newValue: T) => void} callback
353
- * @param {EffectOptions} [options]
354
- */
355
- constructor(callback: (newValue: T) => void, options?: EffectOptions | undefined);
356
- /**
357
- * @type {EffectOptions | undefined}
358
- */
359
- options: EffectOptions | undefined;
360
- /**
361
- * Returns the callback function, if it still exists.
362
- * @returns {((newValue: T) => void) | undefined}
363
- */
364
- get callback(): ((newValue: T) => void) | undefined;
365
- #private;
366
- }
367
- export {};