@oscarpalmer/mora 0.27.0 → 0.28.0
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/constants.d.mts +19 -1
- package/dist/effect.d.mts +16 -1
- package/dist/helpers/is.d.mts +4 -2
- package/dist/helpers/proxy.d.mts +3 -2
- package/dist/helpers/proxy.mjs +36 -14
- package/dist/helpers/value.d.mts +1 -1
- package/dist/index.d.mts +5 -2
- package/dist/models.d.mts +61 -1
- package/dist/mora.full.mjs +144 -63
- package/dist/subscription.d.mts +14 -1
- package/dist/value/array.d.mts +20 -4
- package/dist/value/array.mjs +19 -16
- package/dist/value/computed.d.mts +20 -1
- package/dist/value/computed.mjs +32 -11
- package/dist/value/reactive.d.mts +39 -1
- package/dist/value/signal.d.mts +43 -1
- package/dist/value/signal.mjs +27 -11
- package/dist/value/store.d.mts +18 -3
- package/dist/value/store.mjs +31 -11
- package/package.json +3 -3
- package/src/helpers/proxy.ts +64 -17
- package/src/models.ts +3 -1
- package/src/value/array.ts +74 -18
- package/src/value/computed.ts +65 -30
- package/src/value/signal.ts +77 -9
- package/src/value/store.ts +100 -11
- package/dist/constants-Cy6_M9Vk.d.mts +0 -20
- package/dist/effect-BkupB1p9.d.mts +0 -17
- package/dist/models-QwNga87R.d.mts +0 -148
package/dist/constants.d.mts
CHANGED
|
@@ -1,2 +1,20 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Active, Batch } from "./models.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/constants.d.ts
|
|
4
|
+
declare const ACTIVE: Active;
|
|
5
|
+
declare const ARRAY_THRESHOLD = 100;
|
|
6
|
+
declare const ARRAY_OFFSET = 25;
|
|
7
|
+
declare const ARRAY_PEEK = 10;
|
|
8
|
+
declare const BATCH: Batch;
|
|
9
|
+
declare const METHODS_AFFECTING_LENGTH: Set<string>;
|
|
10
|
+
declare const METHODS_UPDATE: Set<string>;
|
|
11
|
+
declare const NAME_ARRAY = "array";
|
|
12
|
+
declare const NAME_COMPUTED = "computed";
|
|
13
|
+
declare const NAME_EFFECT = "effect";
|
|
14
|
+
declare const NAME_MORA = "$mora";
|
|
15
|
+
declare const NAME_SIGNAL = "signal";
|
|
16
|
+
declare const NAME_STORE = "store";
|
|
17
|
+
declare const NAME_ALL: Set<string>;
|
|
18
|
+
declare const PROPERTY_LENGTH = "length";
|
|
19
|
+
//#endregion
|
|
2
20
|
export { ACTIVE, ARRAY_OFFSET, ARRAY_PEEK, ARRAY_THRESHOLD, BATCH, METHODS_AFFECTING_LENGTH, METHODS_UPDATE, NAME_ALL, NAME_ARRAY, NAME_COMPUTED, NAME_EFFECT, NAME_MORA, NAME_SIGNAL, NAME_STORE, PROPERTY_LENGTH };
|
package/dist/effect.d.mts
CHANGED
|
@@ -1,2 +1,17 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { GenericCallback } from "@oscarpalmer/atoms/models";
|
|
2
|
+
|
|
3
|
+
//#region src/effect.d.ts
|
|
4
|
+
declare class Effect {
|
|
5
|
+
private readonly $mora;
|
|
6
|
+
private readonly state;
|
|
7
|
+
constructor(callback: GenericCallback);
|
|
8
|
+
}
|
|
9
|
+
declare function runEffect(effect: Effect): void;
|
|
10
|
+
/**
|
|
11
|
+
* Create an effect
|
|
12
|
+
* @param callback Callback for handling signal effects
|
|
13
|
+
* @returns Effect
|
|
14
|
+
*/
|
|
15
|
+
declare function effect(callback: GenericCallback): Effect;
|
|
16
|
+
//#endregion
|
|
2
17
|
export { Effect, effect, runEffect };
|
package/dist/helpers/is.d.mts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { Effect } from "../effect.mjs";
|
|
2
|
+
import { Reactive } from "../value/reactive.mjs";
|
|
3
|
+
import { Computed } from "../value/computed.mjs";
|
|
4
|
+
import { Signal } from "../value/signal.mjs";
|
|
3
5
|
import { ReactiveArray } from "../value/array.mjs";
|
|
4
6
|
import { Store } from "../value/store.mjs";
|
|
5
7
|
import { PlainObject } from "@oscarpalmer/atoms/models";
|
package/dist/helpers/proxy.d.mts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Computed } from "../value/computed.mjs";
|
|
2
|
+
import { ReactiveState, SetValueInProxyParameters } from "../models.mjs";
|
|
2
3
|
import { ReactiveArray } from "../value/array.mjs";
|
|
3
4
|
import { Store } from "../value/store.mjs";
|
|
4
5
|
import { ArrayOrPlainObject, Key, PlainObject } from "@oscarpalmer/atoms/models";
|
|
@@ -8,7 +9,7 @@ declare function emityProxyValues<Value>(state: ReactiveState<Value, Value>, map
|
|
|
8
9
|
declare function emityProxyValues<Value>(state: ReactiveState<Value[], Value>, mapped: Map<Key, Computed<unknown>>): void;
|
|
9
10
|
declare function getReactiveValueInProxy<Value>(array: ReactiveArray<Value>, mapped: Map<Key, Computed<unknown>>, index: number, isArray: true): Computed<unknown>;
|
|
10
11
|
declare function getReactiveValueInProxy<Value extends PlainObject>(store: Store<Value>, mapped: Map<Key, Computed<unknown>>, key: Key, isArray: false): Computed<unknown>;
|
|
11
|
-
declare function setProxyValue(
|
|
12
|
+
declare function setProxyValue<Value, Item = Value>(array: boolean, state: ReactiveState<Value, Item>, isObject: (value: unknown) => value is Value | undefined, isProperty: (value: unknown) => boolean, setObject: (state: ReactiveState<Value, Item>, value: Value | undefined) => void, setProperty: (state: ReactiveState<Value, Item>, property: unknown, value: Item) => void, first?: unknown, second?: unknown): void;
|
|
12
13
|
declare function setValueInProxy<Value extends ArrayOrPlainObject, Equal>(parameters: SetValueInProxyParameters<Value, Equal>): boolean;
|
|
13
14
|
//#endregion
|
|
14
15
|
export { emityProxyValues, getReactiveValueInProxy, setProxyValue, setValueInProxy };
|
package/dist/helpers/proxy.mjs
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import "../constants.mjs";
|
|
2
|
-
import { startBatch, stopBatch } from "../batch.mjs";
|
|
3
2
|
import { computed } from "../value/computed.mjs";
|
|
4
3
|
import { emitValue } from "./value.mjs";
|
|
5
4
|
//#region src/helpers/proxy.ts
|
|
@@ -17,21 +16,44 @@ function getReactiveValueInProxy(reactive, mapped, key, isArray) {
|
|
|
17
16
|
}
|
|
18
17
|
return item;
|
|
19
18
|
}
|
|
20
|
-
function setProxyValue(
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
let { length } = proxyKeys;
|
|
25
|
-
for (let index = 0; index < length; index += 1) {
|
|
26
|
-
const key = proxyKeys[index];
|
|
27
|
-
proxy[key] = valueKeys.includes(key) ? value[key] : void 0;
|
|
19
|
+
function setProxyValue(array, state, isObject, isProperty, setObject, setProperty, first, second) {
|
|
20
|
+
if (array && first === "length") {
|
|
21
|
+
state.value.length = second;
|
|
22
|
+
return;
|
|
28
23
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
if (!proxyKeys.includes(key)) proxy[key] = value[key];
|
|
24
|
+
if (isObject(first)) {
|
|
25
|
+
setObject(state, first);
|
|
26
|
+
return;
|
|
33
27
|
}
|
|
34
|
-
|
|
28
|
+
const property = isProperty(first);
|
|
29
|
+
if (array && property && Number.isNaN(first)) return;
|
|
30
|
+
let actual = property ? second : first;
|
|
31
|
+
if (typeof actual === "function") try {
|
|
32
|
+
actual = actual();
|
|
33
|
+
} catch {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
if (actual instanceof Promise) {
|
|
37
|
+
if (property) {
|
|
38
|
+
state.promises ??= /* @__PURE__ */ new Map();
|
|
39
|
+
state.promises.set(first, actual);
|
|
40
|
+
} else state.promise = actual;
|
|
41
|
+
actual.then((value) => {
|
|
42
|
+
if (property && state.promises.get(first) === actual) {
|
|
43
|
+
state.promises.delete(first);
|
|
44
|
+
setProperty(state, first, value);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (!property && isObject(value) && state.promise === actual) {
|
|
48
|
+
state.promise = void 0;
|
|
49
|
+
setObject(state, value);
|
|
50
|
+
}
|
|
51
|
+
}).catch(() => {
|
|
52
|
+
if (property && state.promises.get(first) === actual) state.promises.delete(first);
|
|
53
|
+
else if (!property && state.promise === actual) state.promise = void 0;
|
|
54
|
+
});
|
|
55
|
+
} else if (property) setProperty(state, first, actual);
|
|
56
|
+
else if (isObject(actual)) setObject(state, actual);
|
|
35
57
|
}
|
|
36
58
|
function setValueInProxy(parameters) {
|
|
37
59
|
const { isArray, length, property, state, target, value } = parameters;
|
package/dist/helpers/value.d.mts
CHANGED
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { startBatch, stopBatch } from "./batch.mjs";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { Effect, effect } from "./effect.mjs";
|
|
3
|
+
import { Reactive } from "./value/reactive.mjs";
|
|
4
|
+
import { Computed, computed } from "./value/computed.mjs";
|
|
5
|
+
import { Signal, signal } from "./value/signal.mjs";
|
|
6
|
+
import { Unsubscribe } from "./models.mjs";
|
|
4
7
|
import { ReactiveArray, array } from "./value/array.mjs";
|
|
5
8
|
import { Store, store } from "./value/store.mjs";
|
|
6
9
|
import { isArray, isComputed, isEffect, isReactive, isSignal } from "./helpers/is.mjs";
|
package/dist/models.d.mts
CHANGED
|
@@ -1,2 +1,62 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Effect } from "./effect.mjs";
|
|
2
|
+
import { Subscription } from "./subscription.mjs";
|
|
3
|
+
import { Computed } from "./value/computed.mjs";
|
|
4
|
+
import { Signal } from "./value/signal.mjs";
|
|
5
|
+
import { GenericCallback, Key } from "@oscarpalmer/atoms/models";
|
|
6
|
+
|
|
7
|
+
//#region src/models.d.ts
|
|
8
|
+
type Active = {
|
|
9
|
+
computed?: Computed<unknown>;
|
|
10
|
+
effect?: Effect;
|
|
11
|
+
};
|
|
12
|
+
type Batch = {
|
|
13
|
+
depth: number;
|
|
14
|
+
handlers: Set<Effect | Subscription>;
|
|
15
|
+
};
|
|
16
|
+
type ComputedEffect = {
|
|
17
|
+
dirty: boolean;
|
|
18
|
+
instance: Effect;
|
|
19
|
+
};
|
|
20
|
+
type EffectState = {
|
|
21
|
+
callback: GenericCallback;
|
|
22
|
+
};
|
|
23
|
+
type InternalComputed = {
|
|
24
|
+
readonly effect: ComputedEffect;
|
|
25
|
+
readonly state: ReactiveState<unknown, unknown>;
|
|
26
|
+
};
|
|
27
|
+
type InternalEffect = {
|
|
28
|
+
state: EffectState;
|
|
29
|
+
};
|
|
30
|
+
type ReactiveOptions<Value> = {
|
|
31
|
+
/**
|
|
32
|
+
* Method for comparing values for equality
|
|
33
|
+
* @param first First value
|
|
34
|
+
* @param second Second value
|
|
35
|
+
* @returns Are the values equal?
|
|
36
|
+
* @default Object.is
|
|
37
|
+
*/
|
|
38
|
+
equal?: (first: Value, second: Value) => boolean;
|
|
39
|
+
};
|
|
40
|
+
type ReactiveState<Value, Equal> = {
|
|
41
|
+
computeds: Set<Computed<unknown>>;
|
|
42
|
+
effects: Set<Effect>;
|
|
43
|
+
equal: (first: Equal, second: Equal) => boolean;
|
|
44
|
+
promise?: Promise<Value>;
|
|
45
|
+
promises?: Map<Key, Promise<never>>;
|
|
46
|
+
subscriptions: Map<GenericCallback, Subscription>;
|
|
47
|
+
value: Value;
|
|
48
|
+
};
|
|
49
|
+
type SetValueInProxyParameters<Value, Equal> = {
|
|
50
|
+
target: Value;
|
|
51
|
+
property: PropertyKey;
|
|
52
|
+
value: unknown;
|
|
53
|
+
state: ReactiveState<Value, Equal>;
|
|
54
|
+
isArray: boolean;
|
|
55
|
+
length?: Signal<number>;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Unsubscribe from changes
|
|
59
|
+
*/
|
|
60
|
+
type Unsubscribe = () => void;
|
|
61
|
+
//#endregion
|
|
2
62
|
export { Active, Batch, ComputedEffect, EffectState, InternalComputed, InternalEffect, ReactiveOptions, ReactiveState, SetValueInProxyParameters, Unsubscribe };
|
package/dist/mora.full.mjs
CHANGED
|
@@ -215,18 +215,10 @@ var Computed = class extends Reactive {
|
|
|
215
215
|
constructor(callback, options) {
|
|
216
216
|
super(NAME_COMPUTED, void 0, options);
|
|
217
217
|
this.effect.instance = effect(() => {
|
|
218
|
-
if (
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
const value = callback();
|
|
222
|
-
ACTIVE.computed = previousComputed;
|
|
223
|
-
if (!this.state.equal(this.state.value, value)) {
|
|
224
|
-
this.state.value = value;
|
|
225
|
-
for (const computed of this.state.computeds) computed.effect.dirty = true;
|
|
226
|
-
for (const effect of this.state.effects) BATCH.handlers.add(effect);
|
|
227
|
-
for (const [, subscription] of this.state.subscriptions) subscription.callback(value);
|
|
218
|
+
if (this.effect.dirty) {
|
|
219
|
+
setValue$1(this, this.state, callback);
|
|
220
|
+
this.effect.dirty = false;
|
|
228
221
|
}
|
|
229
|
-
this.effect.dirty = false;
|
|
230
222
|
});
|
|
231
223
|
}
|
|
232
224
|
/**
|
|
@@ -248,6 +240,34 @@ var Computed = class extends Reactive {
|
|
|
248
240
|
function computed(callback, options) {
|
|
249
241
|
return new Computed(callback, options);
|
|
250
242
|
}
|
|
243
|
+
function setAndEmit$1(state, value) {
|
|
244
|
+
if (state.equal(state.value, value)) return;
|
|
245
|
+
state.value = value;
|
|
246
|
+
for (const computed of state.computeds) computed.effect.dirty = true;
|
|
247
|
+
for (const effect of state.effects) BATCH.handlers.add(effect);
|
|
248
|
+
for (const [, subscription] of state.subscriptions) subscription.callback(value);
|
|
249
|
+
flushHandlers();
|
|
250
|
+
}
|
|
251
|
+
function setValue$1(instance, state, callback) {
|
|
252
|
+
const previousComputed = ACTIVE.computed;
|
|
253
|
+
ACTIVE.computed = instance;
|
|
254
|
+
try {
|
|
255
|
+
const value = callback();
|
|
256
|
+
if (value instanceof Promise) {
|
|
257
|
+
state.promise = value;
|
|
258
|
+
value.then((resolvedValue) => {
|
|
259
|
+
if (state.promise === value) {
|
|
260
|
+
state.promise = void 0;
|
|
261
|
+
setAndEmit$1(state, resolvedValue);
|
|
262
|
+
}
|
|
263
|
+
}).catch(() => {
|
|
264
|
+
if (state.promise === value) state.promise = void 0;
|
|
265
|
+
});
|
|
266
|
+
} else setAndEmit$1(state, value);
|
|
267
|
+
} finally {
|
|
268
|
+
ACTIVE.computed = previousComputed;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
251
271
|
//#endregion
|
|
252
272
|
//#region src/helpers/value.ts
|
|
253
273
|
function emitValue(state) {
|
|
@@ -290,21 +310,44 @@ function getReactiveValueInProxy(reactive, mapped, key, isArray) {
|
|
|
290
310
|
}
|
|
291
311
|
return item;
|
|
292
312
|
}
|
|
293
|
-
function setProxyValue(
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
313
|
+
function setProxyValue(array, state, isObject, isProperty, setObject, setProperty, first, second) {
|
|
314
|
+
if (array && first === "length") {
|
|
315
|
+
state.value.length = second;
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
if (isObject(first)) {
|
|
319
|
+
setObject(state, first);
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
const property = isProperty(first);
|
|
323
|
+
if (array && property && Number.isNaN(first)) return;
|
|
324
|
+
let actual = property ? second : first;
|
|
325
|
+
if (typeof actual === "function") try {
|
|
326
|
+
actual = actual();
|
|
327
|
+
} catch {
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
if (actual instanceof Promise) {
|
|
331
|
+
if (property) {
|
|
332
|
+
state.promises ??= /* @__PURE__ */ new Map();
|
|
333
|
+
state.promises.set(first, actual);
|
|
334
|
+
} else state.promise = actual;
|
|
335
|
+
actual.then((value) => {
|
|
336
|
+
if (property && state.promises.get(first) === actual) {
|
|
337
|
+
state.promises.delete(first);
|
|
338
|
+
setProperty(state, first, value);
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
if (!property && isObject(value) && state.promise === actual) {
|
|
342
|
+
state.promise = void 0;
|
|
343
|
+
setObject(state, value);
|
|
344
|
+
}
|
|
345
|
+
}).catch(() => {
|
|
346
|
+
if (property && state.promises.get(first) === actual) state.promises.delete(first);
|
|
347
|
+
else if (!property && state.promise === actual) state.promise = void 0;
|
|
348
|
+
});
|
|
349
|
+
} else if (property) setProperty(state, first, actual);
|
|
350
|
+
else if (isObject(actual)) setObject(state, actual);
|
|
308
351
|
}
|
|
309
352
|
function setValueInProxy(parameters) {
|
|
310
353
|
const { isArray, length, property, state, target, value } = parameters;
|
|
@@ -336,10 +379,7 @@ var Signal = class extends Reactive {
|
|
|
336
379
|
* @param value New value
|
|
337
380
|
*/
|
|
338
381
|
set(value) {
|
|
339
|
-
|
|
340
|
-
this.state.value = value;
|
|
341
|
-
emitValue(this.state);
|
|
342
|
-
}
|
|
382
|
+
setValue(this.state, value);
|
|
343
383
|
}
|
|
344
384
|
/**
|
|
345
385
|
* Update the value _(based on the current value)_
|
|
@@ -349,14 +389,33 @@ var Signal = class extends Reactive {
|
|
|
349
389
|
this.set(callback(this.state.value));
|
|
350
390
|
}
|
|
351
391
|
};
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
392
|
+
function setAndEmit(state, value) {
|
|
393
|
+
if (!state.equal(state.value, value)) {
|
|
394
|
+
state.value = value;
|
|
395
|
+
emitValue(state);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
function setValue(state, value) {
|
|
399
|
+
try {
|
|
400
|
+
let actual = value;
|
|
401
|
+
if (typeof value === "function") actual = value();
|
|
402
|
+
if (actual instanceof Promise) {
|
|
403
|
+
state.promise = actual;
|
|
404
|
+
actual.then((value) => {
|
|
405
|
+
if (actual === state.promise) {
|
|
406
|
+
state.promise = void 0;
|
|
407
|
+
setAndEmit(state, value);
|
|
408
|
+
}
|
|
409
|
+
}).catch(() => {
|
|
410
|
+
if (actual === state.promise) state.promise = void 0;
|
|
411
|
+
});
|
|
412
|
+
} else setAndEmit(state, actual);
|
|
413
|
+
} catch {}
|
|
414
|
+
}
|
|
358
415
|
function signal(value, options) {
|
|
359
|
-
|
|
416
|
+
const instance = new Signal(void 0, options);
|
|
417
|
+
instance.set(value);
|
|
418
|
+
return instance;
|
|
360
419
|
}
|
|
361
420
|
//#endregion
|
|
362
421
|
//#region src/value/array.ts
|
|
@@ -426,7 +485,7 @@ var ReactiveArray = class extends Reactive {
|
|
|
426
485
|
}
|
|
427
486
|
peek(value) {
|
|
428
487
|
if (value === "length") return this.#size.peek();
|
|
429
|
-
return typeof value === "number" ? this.state.value.at(value) :
|
|
488
|
+
return typeof value === "number" ? this.state.value.at(value) : this.state.value.slice();
|
|
430
489
|
}
|
|
431
490
|
/**
|
|
432
491
|
* Remove and return the last item of the array
|
|
@@ -444,9 +503,7 @@ var ReactiveArray = class extends Reactive {
|
|
|
444
503
|
return this.state.value.push(...items);
|
|
445
504
|
}
|
|
446
505
|
set(first, second) {
|
|
447
|
-
|
|
448
|
-
else if (first === "length") this.length = second;
|
|
449
|
-
else if (typeof first === "number" && !Number.isNaN(first)) setAtIndex(this.state.value, first, second);
|
|
506
|
+
setProxyValue(true, this.state, isArrayValue, isArrayIndex, setArray, setAtIndex, first, second);
|
|
450
507
|
}
|
|
451
508
|
/**
|
|
452
509
|
* Remove and return the first item of the array
|
|
@@ -486,18 +543,20 @@ var ReactiveArray = class extends Reactive {
|
|
|
486
543
|
if (updated == null || Array.isArray(updated)) this.set(updated);
|
|
487
544
|
}
|
|
488
545
|
};
|
|
489
|
-
/**
|
|
490
|
-
* Create a reactive array
|
|
491
|
-
* @param value Initial array of items
|
|
492
|
-
* @param options Reactivity options
|
|
493
|
-
* @returns Reactive array
|
|
494
|
-
*/
|
|
495
546
|
function array(value, options) {
|
|
496
|
-
|
|
547
|
+
const instance = new ReactiveArray([], options);
|
|
548
|
+
instance.set(value);
|
|
549
|
+
return instance;
|
|
550
|
+
}
|
|
551
|
+
function isArrayIndex(value) {
|
|
552
|
+
return typeof value === "number";
|
|
553
|
+
}
|
|
554
|
+
function isArrayValue(value) {
|
|
555
|
+
return value == null || Array.isArray(value);
|
|
497
556
|
}
|
|
498
557
|
function updateArray(type, array, state, length) {
|
|
499
558
|
const affectsLength = METHODS_AFFECTING_LENGTH.has(type);
|
|
500
|
-
const previousArray = affectsLength ? [] :
|
|
559
|
+
const previousArray = affectsLength ? [] : array.slice();
|
|
501
560
|
const previousLength = array.length;
|
|
502
561
|
return (...args) => {
|
|
503
562
|
const result = array[type](...args);
|
|
@@ -508,9 +567,12 @@ function updateArray(type, array, state, length) {
|
|
|
508
567
|
return result;
|
|
509
568
|
};
|
|
510
569
|
}
|
|
511
|
-
function
|
|
512
|
-
|
|
513
|
-
|
|
570
|
+
function setArray(state, value) {
|
|
571
|
+
state.value.splice(0, state.value.length, ...value ?? []);
|
|
572
|
+
}
|
|
573
|
+
function setAtIndex(state, index, value) {
|
|
574
|
+
const actual = index < 0 ? state.value.length + index : index;
|
|
575
|
+
if (actual > -1) state.value[actual] = value;
|
|
514
576
|
}
|
|
515
577
|
//#endregion
|
|
516
578
|
//#region node_modules/@oscarpalmer/atoms/dist/internal/is.mjs
|
|
@@ -562,8 +624,7 @@ var Store = class extends Reactive {
|
|
|
562
624
|
return isKey(key) ? this.state.value[key] : { ...this.state.value };
|
|
563
625
|
}
|
|
564
626
|
set(first, second) {
|
|
565
|
-
|
|
566
|
-
else if (first == null || isPlainObject(first)) setProxyValue(this.state.value, first ?? {});
|
|
627
|
+
setProxyValue(false, this.state, isStoreObject, isKey, setObject, setProperty, first, second);
|
|
567
628
|
}
|
|
568
629
|
subscribe(first, second) {
|
|
569
630
|
if (isKey(first) && typeof second === "function") return getReactiveValueInProxy(this, this.#keyed, first, false).subscribe(second);
|
|
@@ -574,18 +635,38 @@ var Store = class extends Reactive {
|
|
|
574
635
|
* @param callback Callback to update the value
|
|
575
636
|
*/
|
|
576
637
|
update(callback) {
|
|
577
|
-
const updated = callback(
|
|
578
|
-
if (updated == null || isPlainObject(updated))
|
|
638
|
+
const updated = callback(this.state.value);
|
|
639
|
+
if (updated == null || isPlainObject(updated)) setObject(this.state, updated);
|
|
579
640
|
}
|
|
580
641
|
};
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
642
|
+
function isStoreObject(value) {
|
|
643
|
+
return value == null || isPlainObject(value);
|
|
644
|
+
}
|
|
645
|
+
function setObject(state, value) {
|
|
646
|
+
startBatch();
|
|
647
|
+
const actual = value ?? {};
|
|
648
|
+
const proxy = state.value;
|
|
649
|
+
const proxyKeys = Object.keys(proxy);
|
|
650
|
+
const actualKeys = Object.keys(actual);
|
|
651
|
+
let { length } = proxyKeys;
|
|
652
|
+
for (let index = 0; index < length; index += 1) {
|
|
653
|
+
const key = proxyKeys[index];
|
|
654
|
+
proxy[key] = actualKeys.includes(key) ? actual[key] : void 0;
|
|
655
|
+
}
|
|
656
|
+
length = actualKeys.length;
|
|
657
|
+
for (let index = 0; index < length; index += 1) {
|
|
658
|
+
const key = actualKeys[index];
|
|
659
|
+
if (!proxyKeys.includes(key)) proxy[key] = actual[key];
|
|
660
|
+
}
|
|
661
|
+
stopBatch();
|
|
662
|
+
}
|
|
663
|
+
function setProperty(state, key, value) {
|
|
664
|
+
state.value[key] = value;
|
|
665
|
+
}
|
|
587
666
|
function store(value, options) {
|
|
588
|
-
|
|
667
|
+
const instance = new Store({}, options);
|
|
668
|
+
instance.set(value);
|
|
669
|
+
return instance;
|
|
589
670
|
}
|
|
590
671
|
//#endregion
|
|
591
672
|
export { array, computed, effect, isArray, isComputed, isEffect, isReactive, isSignal, signal, startBatch, stopBatch, store };
|
package/dist/subscription.d.mts
CHANGED
|
@@ -1,2 +1,15 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ReactiveState, Unsubscribe } from "./models.mjs";
|
|
2
|
+
import { GenericCallback } from "@oscarpalmer/atoms/models";
|
|
3
|
+
|
|
4
|
+
//#region src/subscription.d.ts
|
|
5
|
+
declare class Subscription {
|
|
6
|
+
callback: GenericCallback;
|
|
7
|
+
state: ReactiveState<unknown, never>;
|
|
8
|
+
constructor(state: ReactiveState<unknown, never>, callback: GenericCallback);
|
|
9
|
+
destroy(): void;
|
|
10
|
+
}
|
|
11
|
+
declare function noop(): void;
|
|
12
|
+
declare function subscribe<Value>(state: ReactiveState<Value, never>, callback: (value: Value) => void): Unsubscribe;
|
|
13
|
+
declare function unsubscribe<Value>(state: ReactiveState<Value, never>, callback: (value: Value) => void): void;
|
|
14
|
+
//#endregion
|
|
2
15
|
export { Subscription, noop, subscribe, unsubscribe };
|
package/dist/value/array.d.mts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { Reactive } from "./reactive.mjs";
|
|
2
|
+
import { Computed } from "./computed.mjs";
|
|
3
|
+
import { ReactiveOptions, Unsubscribe } from "../models.mjs";
|
|
4
|
+
import { PROPERTY_LENGTH } from "../constants.mjs";
|
|
3
5
|
|
|
4
6
|
//#region src/value/array.d.ts
|
|
5
7
|
declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
|
|
@@ -81,13 +83,13 @@ declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
|
|
|
81
83
|
* Set the value
|
|
82
84
|
* @param value New array of items _(defaults to an empty array)_
|
|
83
85
|
*/
|
|
84
|
-
set(value?: Item[]): void;
|
|
86
|
+
set(value?: Item[] | (() => null | undefined | Item[] | Promise<null | undefined | Item[]>) | Promise<null | undefined | Item[]>): void;
|
|
85
87
|
/**
|
|
86
88
|
* Set the value at an index
|
|
87
89
|
* @param index Index of item to set __(if negative, starts from the end)_
|
|
88
90
|
* @param value New item
|
|
89
91
|
*/
|
|
90
|
-
set(index: number, value: Item): void;
|
|
92
|
+
set(index: number, value: Item | (() => Item | Promise<Item>) | Promise<Item>): void;
|
|
91
93
|
/**
|
|
92
94
|
* Set the length of the array
|
|
93
95
|
* @param value New array length
|
|
@@ -129,6 +131,20 @@ declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
|
|
|
129
131
|
*/
|
|
130
132
|
update(callback: (value: Item[]) => Item[]): void;
|
|
131
133
|
}
|
|
134
|
+
/**
|
|
135
|
+
* Create a reactive array from a function result
|
|
136
|
+
* @param value Initial array of items
|
|
137
|
+
* @param options Reactivity options
|
|
138
|
+
* @returns Reactive array
|
|
139
|
+
*/
|
|
140
|
+
declare function array<Item>(value: () => Item[] | Promise<Item[]>, options?: ReactiveOptions<Item>): ReactiveArray<Item>;
|
|
141
|
+
/**
|
|
142
|
+
* Create a reactive array from a promise
|
|
143
|
+
* @param value Initial array of items
|
|
144
|
+
* @param options Reactivity options
|
|
145
|
+
* @returns Reactive array
|
|
146
|
+
*/
|
|
147
|
+
declare function array<Item>(value: Promise<Item[]>, options?: ReactiveOptions<Item>): ReactiveArray<Item>;
|
|
132
148
|
/**
|
|
133
149
|
* Create a reactive array
|
|
134
150
|
* @param value Initial array of items
|
package/dist/value/array.mjs
CHANGED
|
@@ -3,7 +3,7 @@ import { noop, subscribe } from "../subscription.mjs";
|
|
|
3
3
|
import { Reactive } from "./reactive.mjs";
|
|
4
4
|
import { computed } from "./computed.mjs";
|
|
5
5
|
import { emitValue, equalArrays, getValue } from "../helpers/value.mjs";
|
|
6
|
-
import { emityProxyValues, getReactiveValueInProxy, setValueInProxy } from "../helpers/proxy.mjs";
|
|
6
|
+
import { emityProxyValues, getReactiveValueInProxy, setProxyValue, setValueInProxy } from "../helpers/proxy.mjs";
|
|
7
7
|
import { signal } from "./signal.mjs";
|
|
8
8
|
//#region src/value/array.ts
|
|
9
9
|
var ReactiveArray = class extends Reactive {
|
|
@@ -72,7 +72,7 @@ var ReactiveArray = class extends Reactive {
|
|
|
72
72
|
}
|
|
73
73
|
peek(value) {
|
|
74
74
|
if (value === "length") return this.#size.peek();
|
|
75
|
-
return typeof value === "number" ? this.state.value.at(value) :
|
|
75
|
+
return typeof value === "number" ? this.state.value.at(value) : this.state.value.slice();
|
|
76
76
|
}
|
|
77
77
|
/**
|
|
78
78
|
* Remove and return the last item of the array
|
|
@@ -90,9 +90,7 @@ var ReactiveArray = class extends Reactive {
|
|
|
90
90
|
return this.state.value.push(...items);
|
|
91
91
|
}
|
|
92
92
|
set(first, second) {
|
|
93
|
-
|
|
94
|
-
else if (first === "length") this.length = second;
|
|
95
|
-
else if (typeof first === "number" && !Number.isNaN(first)) setAtIndex(this.state.value, first, second);
|
|
93
|
+
setProxyValue(true, this.state, isArrayValue, isArrayIndex, setArray, setAtIndex, first, second);
|
|
96
94
|
}
|
|
97
95
|
/**
|
|
98
96
|
* Remove and return the first item of the array
|
|
@@ -132,18 +130,20 @@ var ReactiveArray = class extends Reactive {
|
|
|
132
130
|
if (updated == null || Array.isArray(updated)) this.set(updated);
|
|
133
131
|
}
|
|
134
132
|
};
|
|
135
|
-
/**
|
|
136
|
-
* Create a reactive array
|
|
137
|
-
* @param value Initial array of items
|
|
138
|
-
* @param options Reactivity options
|
|
139
|
-
* @returns Reactive array
|
|
140
|
-
*/
|
|
141
133
|
function array(value, options) {
|
|
142
|
-
|
|
134
|
+
const instance = new ReactiveArray([], options);
|
|
135
|
+
instance.set(value);
|
|
136
|
+
return instance;
|
|
137
|
+
}
|
|
138
|
+
function isArrayIndex(value) {
|
|
139
|
+
return typeof value === "number";
|
|
140
|
+
}
|
|
141
|
+
function isArrayValue(value) {
|
|
142
|
+
return value == null || Array.isArray(value);
|
|
143
143
|
}
|
|
144
144
|
function updateArray(type, array, state, length) {
|
|
145
145
|
const affectsLength = METHODS_AFFECTING_LENGTH.has(type);
|
|
146
|
-
const previousArray = affectsLength ? [] :
|
|
146
|
+
const previousArray = affectsLength ? [] : array.slice();
|
|
147
147
|
const previousLength = array.length;
|
|
148
148
|
return (...args) => {
|
|
149
149
|
const result = array[type](...args);
|
|
@@ -154,9 +154,12 @@ function updateArray(type, array, state, length) {
|
|
|
154
154
|
return result;
|
|
155
155
|
};
|
|
156
156
|
}
|
|
157
|
-
function
|
|
158
|
-
|
|
159
|
-
|
|
157
|
+
function setArray(state, value) {
|
|
158
|
+
state.value.splice(0, state.value.length, ...value ?? []);
|
|
159
|
+
}
|
|
160
|
+
function setAtIndex(state, index, value) {
|
|
161
|
+
const actual = index < 0 ? state.value.length + index : index;
|
|
162
|
+
if (actual > -1) state.value[actual] = value;
|
|
160
163
|
}
|
|
161
164
|
//#endregion
|
|
162
165
|
export { ReactiveArray, array };
|
|
@@ -1,2 +1,21 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Reactive } from "./reactive.mjs";
|
|
2
|
+
import { ReactiveOptions } from "../models.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/value/computed.d.ts
|
|
5
|
+
declare class Computed<Value> extends Reactive<Value> {
|
|
6
|
+
private readonly effect;
|
|
7
|
+
constructor(callback: () => Value | Promise<Value>, options?: ReactiveOptions<Value>);
|
|
8
|
+
/**
|
|
9
|
+
* @inheritdoc
|
|
10
|
+
*/
|
|
11
|
+
get(): Value;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Create a computed value
|
|
15
|
+
* @param callback Callback to compute the value
|
|
16
|
+
* @param options Reactivity options
|
|
17
|
+
* @returns Computed value
|
|
18
|
+
*/
|
|
19
|
+
declare function computed<Value>(callback: () => Value | Promise<Value>, options?: ReactiveOptions<Value>): Computed<Value>;
|
|
20
|
+
//#endregion
|
|
2
21
|
export { Computed, computed };
|