@adbl/cells 0.0.11 → 0.0.12
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 +0 -14
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/{types → dist}/library/classes.d.ts +25 -56
- package/dist/library/classes.js +958 -0
- package/dist/library/classes.js.map +1 -0
- package/{types → dist}/library/index.d.ts +2 -1
- package/{library → dist/library}/index.js +3 -5
- package/dist/library/index.js.map +1 -0
- package/index.js +0 -2
- package/package.json +12 -4
- package/.vscode/settings.json +0 -4
- package/bun.lockb +0 -0
- package/library/classes.js +0 -958
- package/library/root.js +0 -44
- package/types/library/root.d.ts +0 -24
- /package/{types → dist}/index.d.ts +0 -0
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 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.js"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC"}
|
|
@@ -2,58 +2,6 @@
|
|
|
2
2
|
* @template {*} T
|
|
3
3
|
*/
|
|
4
4
|
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
5
|
/**
|
|
58
6
|
* @template T
|
|
59
7
|
* Creates a new Cell instance with the provided value.
|
|
@@ -226,8 +174,11 @@ export class DerivedCell<T extends unknown> extends Cell<T> {
|
|
|
226
174
|
* @param {() => T} computedFn - A function that generates the value of the computed.
|
|
227
175
|
*/
|
|
228
176
|
constructor(computedFn: () => T);
|
|
177
|
+
/** @type {WeakRef<this>} */
|
|
178
|
+
ref: WeakRef<this>;
|
|
229
179
|
/** @type {() => T} */
|
|
230
180
|
computedFn: () => T;
|
|
181
|
+
initialized: boolean;
|
|
231
182
|
/**
|
|
232
183
|
* @readonly
|
|
233
184
|
*/
|
|
@@ -236,6 +187,20 @@ export class DerivedCell<T extends unknown> extends Cell<T> {
|
|
|
236
187
|
* @readonly
|
|
237
188
|
*/
|
|
238
189
|
readonly get value(): T;
|
|
190
|
+
/**
|
|
191
|
+
* Listens for changes to the cell, initializing the value if not already done.
|
|
192
|
+
* @param {(newValue: T) => void} callback - The function to call when the cell's value changes.
|
|
193
|
+
* @param {object} [options] - Optional configuration for listening.
|
|
194
|
+
*/
|
|
195
|
+
listen(callback: (newValue: T) => void, options?: object | undefined): () => void;
|
|
196
|
+
/**
|
|
197
|
+
* Runs the callback and sets up a listener, initializing the cell's value if not already done.
|
|
198
|
+
* @param {(newValue: T) => void} callback - The function to call when the cell's value changes.
|
|
199
|
+
* @param {object} [options] - Optional configuration for listening and running.
|
|
200
|
+
* @returns {*} The result of the parent class's runAndListen method.
|
|
201
|
+
*/
|
|
202
|
+
runAndListen(callback: (newValue: T) => void, options?: object | undefined): any;
|
|
203
|
+
deproxy(): void;
|
|
239
204
|
}
|
|
240
205
|
/**
|
|
241
206
|
* @template {*} T
|
|
@@ -248,8 +213,7 @@ export class SourceCell<T extends unknown> extends Cell<T> {
|
|
|
248
213
|
* @param {Partial<CellOptions<T>>} [options]
|
|
249
214
|
*/
|
|
250
215
|
constructor(value: T, options?: Partial<CellOptions<T>> | undefined);
|
|
251
|
-
|
|
252
|
-
options: Partial<CellOptions<T>>;
|
|
216
|
+
options: Partial<CellOptions<T>> | undefined;
|
|
253
217
|
/**
|
|
254
218
|
* For cells containing objects, returns the object itself.
|
|
255
219
|
* This can be useful in scenarios where unfettered access to the original object is needed,
|
|
@@ -257,10 +221,10 @@ export class SourceCell<T extends unknown> extends Cell<T> {
|
|
|
257
221
|
*
|
|
258
222
|
* @example
|
|
259
223
|
* const cell = new SourceCell({ a: 1, b: 2 });
|
|
260
|
-
* console.log(cell.
|
|
224
|
+
* console.log(cell.deproxy()); // { a: 1, b: 2 }
|
|
261
225
|
*
|
|
262
226
|
* cell.value = { a: 3, b: 4 };
|
|
263
|
-
* console.log(cell.
|
|
227
|
+
* console.log(cell.deproxy()); // { a: 3, b: 4 }
|
|
264
228
|
*
|
|
265
229
|
* @returns {T extends object ? T : never} The original object if T is an object, otherwise never.
|
|
266
230
|
*/
|
|
@@ -273,6 +237,11 @@ export class SourceCell<T extends unknown> extends Cell<T> {
|
|
|
273
237
|
get value(): T;
|
|
274
238
|
#private;
|
|
275
239
|
}
|
|
240
|
+
export class CellUpdateError extends Error {
|
|
241
|
+
/** @param {Error[]} errors */
|
|
242
|
+
constructor(errors: Error[]);
|
|
243
|
+
errors: Error[];
|
|
244
|
+
}
|
|
276
245
|
export type AsyncRequestAtoms<Input, Output> = {
|
|
277
246
|
/**
|
|
278
247
|
* Represents the loading state of an asynchronous request.
|