@adbl/cells 0.0.10 → 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 +32 -59
- 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 -950
- 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.
|
|
@@ -90,10 +38,12 @@ export class Cell<T extends unknown> {
|
|
|
90
38
|
*/
|
|
91
39
|
static derived: <T_2>(callback: () => T_2) => DerivedCell<T_2>;
|
|
92
40
|
/**
|
|
41
|
+
* @template X
|
|
93
42
|
* Batches all the effects created to run only once.
|
|
94
|
-
* @param {() =>
|
|
43
|
+
* @param {() => X} callback - The function to be executed in a batched manner.
|
|
44
|
+
* @returns {X} The return value of the callback.
|
|
95
45
|
*/
|
|
96
|
-
static batch: (callback: () =>
|
|
46
|
+
static batch: <X>(callback: () => X) => X;
|
|
97
47
|
/**
|
|
98
48
|
* Checks if the provided value is an instance of the Cell class.
|
|
99
49
|
* @template [T=any]
|
|
@@ -140,7 +90,7 @@ export class Cell<T extends unknown> {
|
|
|
140
90
|
*
|
|
141
91
|
* run('input');
|
|
142
92
|
*/
|
|
143
|
-
static async<
|
|
93
|
+
static async<X_1, Y>(getter: (input: X_1) => Promise<Y>): AsyncRequestAtoms<X_1, Y>;
|
|
144
94
|
/**
|
|
145
95
|
* @readonly
|
|
146
96
|
* @returns {Array<DerivedCell<any>>}
|
|
@@ -224,6 +174,11 @@ export class DerivedCell<T extends unknown> extends Cell<T> {
|
|
|
224
174
|
* @param {() => T} computedFn - A function that generates the value of the computed.
|
|
225
175
|
*/
|
|
226
176
|
constructor(computedFn: () => T);
|
|
177
|
+
/** @type {WeakRef<this>} */
|
|
178
|
+
ref: WeakRef<this>;
|
|
179
|
+
/** @type {() => T} */
|
|
180
|
+
computedFn: () => T;
|
|
181
|
+
initialized: boolean;
|
|
227
182
|
/**
|
|
228
183
|
* @readonly
|
|
229
184
|
*/
|
|
@@ -232,6 +187,20 @@ export class DerivedCell<T extends unknown> extends Cell<T> {
|
|
|
232
187
|
* @readonly
|
|
233
188
|
*/
|
|
234
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;
|
|
235
204
|
}
|
|
236
205
|
/**
|
|
237
206
|
* @template {*} T
|
|
@@ -244,8 +213,7 @@ export class SourceCell<T extends unknown> extends Cell<T> {
|
|
|
244
213
|
* @param {Partial<CellOptions<T>>} [options]
|
|
245
214
|
*/
|
|
246
215
|
constructor(value: T, options?: Partial<CellOptions<T>> | undefined);
|
|
247
|
-
|
|
248
|
-
options: Partial<CellOptions<T>>;
|
|
216
|
+
options: Partial<CellOptions<T>> | undefined;
|
|
249
217
|
/**
|
|
250
218
|
* For cells containing objects, returns the object itself.
|
|
251
219
|
* This can be useful in scenarios where unfettered access to the original object is needed,
|
|
@@ -253,10 +221,10 @@ export class SourceCell<T extends unknown> extends Cell<T> {
|
|
|
253
221
|
*
|
|
254
222
|
* @example
|
|
255
223
|
* const cell = new SourceCell({ a: 1, b: 2 });
|
|
256
|
-
* console.log(cell.
|
|
224
|
+
* console.log(cell.deproxy()); // { a: 1, b: 2 }
|
|
257
225
|
*
|
|
258
226
|
* cell.value = { a: 3, b: 4 };
|
|
259
|
-
* console.log(cell.
|
|
227
|
+
* console.log(cell.deproxy()); // { a: 3, b: 4 }
|
|
260
228
|
*
|
|
261
229
|
* @returns {T extends object ? T : never} The original object if T is an object, otherwise never.
|
|
262
230
|
*/
|
|
@@ -269,6 +237,11 @@ export class SourceCell<T extends unknown> extends Cell<T> {
|
|
|
269
237
|
get value(): T;
|
|
270
238
|
#private;
|
|
271
239
|
}
|
|
240
|
+
export class CellUpdateError extends Error {
|
|
241
|
+
/** @param {Error[]} errors */
|
|
242
|
+
constructor(errors: Error[]);
|
|
243
|
+
errors: Error[];
|
|
244
|
+
}
|
|
272
245
|
export type AsyncRequestAtoms<Input, Output> = {
|
|
273
246
|
/**
|
|
274
247
|
* Represents the loading state of an asynchronous request.
|