@oscarpalmer/mora 0.31.0 → 0.32.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/batch.d.mts +4 -5
- package/dist/batch.mjs +4 -2
- package/dist/constants.d.mts +29 -19
- package/dist/constants.mjs +17 -2
- package/dist/effect.d.mts +4 -5
- package/dist/effect.mjs +2 -2
- package/dist/helpers/is.d.mts +14 -9
- package/dist/helpers/is.mjs +6 -0
- package/dist/helpers/proxy.d.mts +12 -10
- package/dist/helpers/proxy.mjs +32 -14
- package/dist/helpers/subscription.d.mts +8 -0
- package/dist/helpers/subscription.mjs +27 -0
- package/dist/helpers/value.d.mts +8 -6
- package/dist/helpers/value.mjs +30 -4
- package/dist/index.d.mts +3 -2
- package/dist/models.d.mts +45 -87
- package/dist/mora.full.mjs +461 -285
- package/dist/value/array.d.mts +4 -5
- package/dist/value/array.mjs +22 -69
- package/dist/value/computed.d.mts +3 -4
- package/dist/value/computed.mjs +14 -14
- package/dist/value/reactive.d.mts +2 -3
- package/dist/value/reactive.mjs +0 -1
- package/dist/value/readonly.d.mts +3 -5
- package/dist/value/readonly.mjs +15 -19
- package/dist/value/signal.d.mts +4 -5
- package/dist/value/signal.mjs +9 -22
- package/dist/value/store.d.mts +4 -5
- package/dist/value/store.mjs +16 -56
- package/package.json +6 -6
- package/src/batch.ts +15 -5
- package/src/constants.ts +32 -2
- package/src/effect.ts +6 -2
- package/src/helpers/is.ts +10 -0
- package/src/helpers/proxy.ts +99 -49
- package/src/helpers/subscription.ts +78 -0
- package/src/helpers/value.ts +69 -4
- package/src/index.ts +1 -1
- package/src/models.ts +38 -81
- package/src/value/array.ts +61 -150
- package/src/value/computed.ts +36 -13
- package/src/value/reactive.ts +8 -5
- package/src/value/readonly.ts +26 -25
- package/src/value/signal.ts +19 -22
- package/src/value/store.ts +35 -114
- package/types/constants.d.ts +1 -1
- package/types/index.d.ts +1 -1
- package/types/value/array.d.ts +1 -1
- package/types/value/computed.d.ts +0 -3
- package/types/value/signal.d.ts +1 -1
- package/types/value/store.d.ts +1 -1
- package/dist/subscription.d.mts +0 -7
- package/dist/subscription.mjs +0 -27
- package/src/subscription.ts +0 -45
package/dist/batch.d.mts
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
//#region src/batch.d.ts
|
|
2
|
-
declare function flushHandlers(): void;
|
|
2
|
+
export declare function flushHandlers(): void;
|
|
3
3
|
/**
|
|
4
4
|
* Start batching effects
|
|
5
5
|
*
|
|
6
6
|
* _(Use {@link stopBatch} to flush and run batched effects)_
|
|
7
7
|
*/
|
|
8
|
-
declare function startBatch(): void;
|
|
8
|
+
export declare function startBatch(): void;
|
|
9
9
|
/**
|
|
10
10
|
* Stop batching effects and flush _(run)_ them
|
|
11
11
|
*/
|
|
12
|
-
declare function stopBatch(): void;
|
|
13
|
-
//#endregion
|
|
14
|
-
export { flushHandlers, startBatch, stopBatch };
|
|
12
|
+
export declare function stopBatch(): void;
|
|
13
|
+
//#endregion
|
package/dist/batch.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { BATCH } from "./constants.mjs";
|
|
2
2
|
import { runEffect } from "./effect.mjs";
|
|
3
|
+
import { getFrozenValue, peekSimpleValue } from "./helpers/value.mjs";
|
|
3
4
|
//#region src/batch.ts
|
|
4
5
|
function flushHandlers() {
|
|
5
6
|
if (BATCH.flushing) return;
|
|
@@ -10,8 +11,9 @@ function flushHandlers() {
|
|
|
10
11
|
const { length } = handlers;
|
|
11
12
|
BATCH.handlers.clear();
|
|
12
13
|
for (let index = 0; index < length; index += 1) {
|
|
13
|
-
const handler = handlers[index];
|
|
14
|
-
|
|
14
|
+
const [, handler] = handlers[index];
|
|
15
|
+
const subscription = handler;
|
|
16
|
+
if (typeof subscription.frozen === "boolean") subscription.callback(subscription.frozen ? getFrozenValue(subscription.state.value) : peekSimpleValue(subscription.state.value, subscription.copy));
|
|
15
17
|
else runEffect(handler);
|
|
16
18
|
}
|
|
17
19
|
}
|
package/dist/constants.d.mts
CHANGED
|
@@ -1,20 +1,30 @@
|
|
|
1
|
-
import { Active, Batch } from "./models.mjs";
|
|
1
|
+
import { Active, Batch, SubscriptionType } from "./models.mjs";
|
|
2
2
|
//#region src/constants.d.ts
|
|
3
|
-
declare const ACTIVE: Active;
|
|
4
|
-
declare const ARRAY_THRESHOLD = 100;
|
|
5
|
-
declare const ARRAY_OFFSET = 25;
|
|
6
|
-
declare const ARRAY_PEEK = 10;
|
|
7
|
-
declare const BATCH: Batch;
|
|
8
|
-
declare const METHODS_AFFECTING_LENGTH: Set<string>;
|
|
9
|
-
declare const METHODS_UPDATE: Set<string>;
|
|
10
|
-
declare const NAME_ARRAY = "array";
|
|
11
|
-
declare const NAME_COMPUTED = "computed";
|
|
12
|
-
declare const NAME_EFFECT = "effect";
|
|
13
|
-
declare const NAME_MORA = "$mora";
|
|
14
|
-
declare const NAME_READONLY = "readonly";
|
|
15
|
-
declare const NAME_SIGNAL = "signal";
|
|
16
|
-
declare const NAME_STORE = "store";
|
|
17
|
-
declare const
|
|
18
|
-
declare const
|
|
19
|
-
|
|
20
|
-
export
|
|
3
|
+
export declare const ACTIVE: Active;
|
|
4
|
+
export declare const ARRAY_THRESHOLD = 100;
|
|
5
|
+
export declare const ARRAY_OFFSET = 25;
|
|
6
|
+
export declare const ARRAY_PEEK = 10;
|
|
7
|
+
export declare const BATCH: Batch;
|
|
8
|
+
export declare const METHODS_AFFECTING_LENGTH: Set<string>;
|
|
9
|
+
export declare const METHODS_UPDATE: Set<string>;
|
|
10
|
+
export declare const NAME_ARRAY = "array";
|
|
11
|
+
export declare const NAME_COMPUTED = "computed";
|
|
12
|
+
export declare const NAME_EFFECT = "effect";
|
|
13
|
+
export declare const NAME_MORA = "$mora";
|
|
14
|
+
export declare const NAME_READONLY = "readonly";
|
|
15
|
+
export declare const NAME_SIGNAL = "signal";
|
|
16
|
+
export declare const NAME_STORE = "store";
|
|
17
|
+
export declare const NAME_SUBSCRIPTION = "subscription";
|
|
18
|
+
export declare const NAME_ALL: Set<string>;
|
|
19
|
+
export declare const PROPERTY_LENGTH = "length";
|
|
20
|
+
export declare const SUBSCRIPTION_PROPERTY: {
|
|
21
|
+
key: string;
|
|
22
|
+
value: string;
|
|
23
|
+
};
|
|
24
|
+
export declare const SUBSCRIPTION_TYPE_COPY: SubscriptionType;
|
|
25
|
+
export declare const SUBSCRIPTION_TYPE_FROZEN: SubscriptionType;
|
|
26
|
+
export declare const SUBSCRIPTION_TYPE_ORIGINAL: SubscriptionType;
|
|
27
|
+
export declare const SUBSCRIPTION_TYPE_READONLY: SubscriptionType;
|
|
28
|
+
export declare const SUBSCRIPTION_TYPES_COPY: Set<SubscriptionType>;
|
|
29
|
+
export declare const SUBSCRIPTION_TYPES: Set<SubscriptionType>;
|
|
30
|
+
//#endregion
|
package/dist/constants.mjs
CHANGED
|
@@ -6,7 +6,7 @@ const ARRAY_PEEK = 10;
|
|
|
6
6
|
const BATCH = {
|
|
7
7
|
depth: 0,
|
|
8
8
|
flushing: false,
|
|
9
|
-
handlers: /* @__PURE__ */ new
|
|
9
|
+
handlers: /* @__PURE__ */ new Map()
|
|
10
10
|
};
|
|
11
11
|
const METHODS_AFFECTING_LENGTH = /* @__PURE__ */ new Set([
|
|
12
12
|
"pop",
|
|
@@ -29,6 +29,7 @@ const NAME_MORA = "$mora";
|
|
|
29
29
|
const NAME_READONLY = "readonly";
|
|
30
30
|
const NAME_SIGNAL = "signal";
|
|
31
31
|
const NAME_STORE = "store";
|
|
32
|
+
const NAME_SUBSCRIPTION = "subscription";
|
|
32
33
|
const NAME_ALL = /* @__PURE__ */ new Set([
|
|
33
34
|
NAME_ARRAY,
|
|
34
35
|
NAME_COMPUTED,
|
|
@@ -37,5 +38,19 @@ const NAME_ALL = /* @__PURE__ */ new Set([
|
|
|
37
38
|
NAME_STORE
|
|
38
39
|
]);
|
|
39
40
|
const PROPERTY_LENGTH = "length";
|
|
41
|
+
const SUBSCRIPTION_PROPERTY = {
|
|
42
|
+
key: NAME_MORA,
|
|
43
|
+
value: NAME_SUBSCRIPTION
|
|
44
|
+
};
|
|
45
|
+
const SUBSCRIPTION_TYPE_COPY = "copy";
|
|
46
|
+
const SUBSCRIPTION_TYPE_FROZEN = "frozen";
|
|
47
|
+
const SUBSCRIPTION_TYPE_ORIGINAL = "original";
|
|
48
|
+
const SUBSCRIPTION_TYPE_READONLY = "readonly";
|
|
49
|
+
const SUBSCRIPTION_TYPES_COPY = /* @__PURE__ */ new Set([SUBSCRIPTION_TYPE_COPY, SUBSCRIPTION_TYPE_READONLY]);
|
|
50
|
+
const SUBSCRIPTION_TYPES = /* @__PURE__ */ new Set([
|
|
51
|
+
...SUBSCRIPTION_TYPES_COPY,
|
|
52
|
+
SUBSCRIPTION_TYPE_FROZEN,
|
|
53
|
+
SUBSCRIPTION_TYPE_ORIGINAL
|
|
54
|
+
]);
|
|
40
55
|
//#endregion
|
|
41
|
-
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_READONLY, NAME_SIGNAL, NAME_STORE, PROPERTY_LENGTH };
|
|
56
|
+
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_READONLY, NAME_SIGNAL, NAME_STORE, NAME_SUBSCRIPTION, PROPERTY_LENGTH, SUBSCRIPTION_PROPERTY, SUBSCRIPTION_TYPES, SUBSCRIPTION_TYPES_COPY, SUBSCRIPTION_TYPE_COPY, SUBSCRIPTION_TYPE_FROZEN, SUBSCRIPTION_TYPE_ORIGINAL, SUBSCRIPTION_TYPE_READONLY };
|
package/dist/effect.d.mts
CHANGED
|
@@ -7,8 +7,7 @@ import { GenericCallback } from "@oscarpalmer/atoms/models";
|
|
|
7
7
|
* @param callback Callback for handling signal effects
|
|
8
8
|
* @returns Effect
|
|
9
9
|
*/
|
|
10
|
-
declare function effect(callback: GenericCallback): Effect;
|
|
11
|
-
declare function internalEffect(callback: GenericCallback): EffectState;
|
|
12
|
-
declare function runEffect(state: EffectState): void;
|
|
13
|
-
//#endregion
|
|
14
|
-
export { effect, internalEffect, runEffect };
|
|
10
|
+
export declare function effect(callback: GenericCallback): Effect;
|
|
11
|
+
export declare function internalEffect(callback: GenericCallback): EffectState;
|
|
12
|
+
export declare function runEffect(state: EffectState): void;
|
|
13
|
+
//#endregion
|
package/dist/effect.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ACTIVE, NAME_EFFECT } from "./constants.mjs";
|
|
1
|
+
import { ACTIVE, NAME_EFFECT, NAME_MORA } from "./constants.mjs";
|
|
2
2
|
//#region src/effect.ts
|
|
3
3
|
/**
|
|
4
4
|
* Create an effect
|
|
@@ -12,7 +12,7 @@ function effect(callback) {
|
|
|
12
12
|
}
|
|
13
13
|
function getEffect(callback) {
|
|
14
14
|
const state = { callback };
|
|
15
|
-
const instance = Object.freeze({
|
|
15
|
+
const instance = Object.freeze({ [NAME_MORA]: NAME_EFFECT });
|
|
16
16
|
runEffect(state);
|
|
17
17
|
return [instance, state];
|
|
18
18
|
}
|
package/dist/helpers/is.d.mts
CHANGED
|
@@ -7,42 +7,47 @@ import { PlainObject } from "@oscarpalmer/atoms/models";
|
|
|
7
7
|
* @param value Value to check
|
|
8
8
|
* @returns True if value is a {@link Computed}
|
|
9
9
|
*/
|
|
10
|
-
declare function isComputed<Value>(value: unknown): value is Computed<Value>;
|
|
10
|
+
export declare function isComputed<Value>(value: unknown): value is Computed<Value>;
|
|
11
11
|
/**
|
|
12
12
|
* Is the value an effect?
|
|
13
13
|
*
|
|
14
14
|
* @param value Value to check
|
|
15
15
|
* @returns True if value is an {@link Effect}
|
|
16
16
|
*/
|
|
17
|
-
declare function isEffect(value: unknown): value is Effect;
|
|
17
|
+
export declare function isEffect(value: unknown): value is Effect;
|
|
18
18
|
/**
|
|
19
19
|
* Is the value reactive?
|
|
20
20
|
*
|
|
21
21
|
* @param value Value to check
|
|
22
22
|
* @returns True if value is a {@link Reactive}
|
|
23
23
|
*/
|
|
24
|
-
declare function isReactive<Value>(value: unknown): value is Reactive<Value>;
|
|
24
|
+
export declare function isReactive<Value>(value: unknown): value is Reactive<Value>;
|
|
25
25
|
/**
|
|
26
26
|
* Is the value a signal?
|
|
27
27
|
*
|
|
28
28
|
* @param value Value to check
|
|
29
29
|
* @returns True if value is a {@link Signal}
|
|
30
30
|
*/
|
|
31
|
-
declare function isSignal<Value>(value: unknown): value is Signal<Value>;
|
|
31
|
+
export declare function isSignal<Value>(value: unknown): value is Signal<Value>;
|
|
32
32
|
/**
|
|
33
33
|
* Is the value a reactive array?
|
|
34
34
|
*
|
|
35
35
|
* @param value Value to check
|
|
36
36
|
* @returns True if value is a {@link ReactiveArray}
|
|
37
37
|
*/
|
|
38
|
-
declare function isReactiveArray<Item>(value: unknown): value is ReactiveArray<Item>;
|
|
38
|
+
export declare function isReactiveArray<Item>(value: unknown): value is ReactiveArray<Item>;
|
|
39
39
|
/**
|
|
40
40
|
* Is the value a reactive store?
|
|
41
41
|
*
|
|
42
42
|
* @param value Value to check
|
|
43
43
|
* @returns True if value is a {@link Store}
|
|
44
44
|
*/
|
|
45
|
-
declare function isReactiveStore<Value extends PlainObject>(value: unknown): value is ReactiveStore<Value>;
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
45
|
+
export declare function isReactiveStore<Value extends PlainObject>(value: unknown): value is ReactiveStore<Value>;
|
|
46
|
+
/**
|
|
47
|
+
* Is the value a readonly signal?
|
|
48
|
+
*
|
|
49
|
+
* @param value Value to check
|
|
50
|
+
* @returns True if value is a {@link ReadonlySignal}
|
|
51
|
+
*/
|
|
52
|
+
export declare function isReadonlySignal<Value>(value: unknown): value is ReadonlySignal<Value>;
|
|
53
|
+
//#endregion
|
package/dist/helpers/is.mjs
CHANGED
|
@@ -57,6 +57,12 @@ function isReactiveArray(value) {
|
|
|
57
57
|
function isReactiveStore(value) {
|
|
58
58
|
return isMora(value, NAME_STORE);
|
|
59
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Is the value a readonly signal?
|
|
62
|
+
*
|
|
63
|
+
* @param value Value to check
|
|
64
|
+
* @returns True if value is a {@link ReadonlySignal}
|
|
65
|
+
*/
|
|
60
66
|
function isReadonlySignal(value) {
|
|
61
67
|
return isMora(value, NAME_READONLY);
|
|
62
68
|
}
|
package/dist/helpers/proxy.d.mts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import { Computed, ComputedEffect, ReactiveArray, ReactiveState, ReactiveStore,
|
|
2
|
-
import {
|
|
1
|
+
import { Computed, ComputedEffect, ReactiveArray, ReactiveState, ReactiveStore, Signal } from "../models.mjs";
|
|
2
|
+
import { Key } from "@oscarpalmer/atoms/models";
|
|
3
3
|
//#region src/helpers/proxy.d.ts
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
declare function
|
|
7
|
-
declare function getReactiveValueInProxy<Value
|
|
8
|
-
declare function
|
|
9
|
-
declare function
|
|
10
|
-
|
|
11
|
-
export
|
|
4
|
+
type SetObjectCallback<Value, Item> = (state: ReactiveState<Value, Item>, value: Value | undefined) => void;
|
|
5
|
+
type SetPropertyCallback<Value, Item> = (state: ReactiveState<Value, Item>, property: unknown, value: Item) => void;
|
|
6
|
+
export declare function emitProxyValues<Value, Item = Value>(state: ReactiveState<Value, Item>, mapped: Map<Key, [Computed<unknown>, ComputedEffect]>): void;
|
|
7
|
+
export declare function getReactiveValueInProxy<Value>(reactive: ReactiveArray<Value> | ReactiveStore<Value>, mapped: Map<Key, [Computed<unknown>, ComputedEffect]>, key: Key, isArray: boolean): Computed<unknown>;
|
|
8
|
+
export declare function getValueInProxy<Value, Item = Value>(isArray: boolean, instance: ReactiveArray<Value> | ReactiveStore<Value>, state: ReactiveState<Value, Item>, mapped: Map<Key, [Computed<unknown>, ComputedEffect]>, first?: unknown): unknown;
|
|
9
|
+
export declare function peekValueInProxy<Value, Item = Value>(isArray: boolean, state: ReactiveState<Value, Item>, first?: unknown, second?: boolean): unknown;
|
|
10
|
+
export declare function setProxyValue<Value, Item = Value>(isArray: boolean, state: ReactiveState<Value, Item>, setObject: SetObjectCallback<Value, Item>, setProperty: SetPropertyCallback<Value, Item>, first?: unknown, second?: unknown): void;
|
|
11
|
+
export declare function setValueInProxy<Value, Item = Value>(isArray: boolean, state: ReactiveState<Value, Item>, target: object, property: PropertyKey, value: unknown, length?: Signal<number>): boolean;
|
|
12
|
+
export declare function updateProxyValue<Value, Item = Value>(isArray: boolean, state: ReactiveState<Value, Item>, setObject: SetObjectCallback<Value, Item>, setProperty: SetPropertyCallback<Value, Item>, callback: unknown): void;
|
|
13
|
+
//#endregion
|
package/dist/helpers/proxy.mjs
CHANGED
|
@@ -1,32 +1,47 @@
|
|
|
1
1
|
import "../constants.mjs";
|
|
2
|
-
import { emitValue } from "./value.mjs";
|
|
2
|
+
import { emitValue, getSimpleValue, peekSimpleValue } from "./value.mjs";
|
|
3
3
|
import { internalComputed } from "../value/computed.mjs";
|
|
4
|
+
import { isKey, isPlainObject } from "@oscarpalmer/atoms/is";
|
|
4
5
|
//#region src/helpers/proxy.ts
|
|
5
|
-
function
|
|
6
|
+
function emitProxyValues(state, mapped) {
|
|
6
7
|
const values = [...mapped.values()];
|
|
7
8
|
const { length } = values;
|
|
8
9
|
for (let index = 0; index < length; index += 1) values[index][1].dirty = true;
|
|
9
10
|
emitValue(state);
|
|
10
11
|
}
|
|
11
12
|
function getReactiveValueInProxy(reactive, mapped, key, isArray) {
|
|
12
|
-
|
|
13
|
+
const mapKey = String(key);
|
|
14
|
+
let item = mapped.get(mapKey);
|
|
13
15
|
if (item == null) {
|
|
14
16
|
item = internalComputed(() => isArray ? reactive.get().at(key) : reactive.get()[key]);
|
|
15
|
-
mapped.set(
|
|
17
|
+
mapped.set(mapKey, item);
|
|
16
18
|
}
|
|
17
19
|
return item[0];
|
|
18
20
|
}
|
|
19
|
-
function
|
|
20
|
-
if (
|
|
21
|
+
function getValueInProxy(isArray, instance, state, mapped, first) {
|
|
22
|
+
if (isArray ? typeof first === "number" : isKey(first)) return getReactiveValueInProxy(instance, mapped, first, isArray).get();
|
|
23
|
+
return getSimpleValue(state);
|
|
24
|
+
}
|
|
25
|
+
function isProxyObject(isArray, value) {
|
|
26
|
+
return value == null || (isArray ? Array.isArray(value) : isPlainObject(value));
|
|
27
|
+
}
|
|
28
|
+
function peekValueInProxy(isArray, state, first, second) {
|
|
29
|
+
let value;
|
|
30
|
+
if (isArray ? typeof first === "number" : isKey(first)) value = isArray ? state.value.at(first) : state.value[first];
|
|
31
|
+
else value = state.value;
|
|
32
|
+
return peekSimpleValue(value, first === true || second === true);
|
|
33
|
+
}
|
|
34
|
+
function setProxyValue(isArray, state, setObject, setProperty, first, second) {
|
|
35
|
+
if (isArray && first === "length") {
|
|
21
36
|
state.value.length = second;
|
|
22
37
|
return;
|
|
23
38
|
}
|
|
24
|
-
if (
|
|
39
|
+
if (isProxyObject(isArray, first)) {
|
|
25
40
|
setObject(state, first);
|
|
26
41
|
return;
|
|
27
42
|
}
|
|
28
|
-
const property =
|
|
29
|
-
if (
|
|
43
|
+
const property = isArray ? typeof first === "number" : isKey(first);
|
|
44
|
+
if (isArray && property && Number.isNaN(first)) return;
|
|
30
45
|
let actual = property ? second : first;
|
|
31
46
|
if (typeof actual === "function") try {
|
|
32
47
|
actual = actual();
|
|
@@ -44,7 +59,7 @@ function setProxyValue(array, state, isObject, isProperty, setObject, setPropert
|
|
|
44
59
|
setProperty(state, first, value);
|
|
45
60
|
return;
|
|
46
61
|
}
|
|
47
|
-
if (!property &&
|
|
62
|
+
if (!property && isProxyObject(isArray, value) && state.promise === actual) {
|
|
48
63
|
state.promise = void 0;
|
|
49
64
|
setObject(state, value);
|
|
50
65
|
}
|
|
@@ -53,10 +68,9 @@ function setProxyValue(array, state, isObject, isProperty, setObject, setPropert
|
|
|
53
68
|
else if (!property && state.promise === actual) state.promise = void 0;
|
|
54
69
|
});
|
|
55
70
|
} else if (property) setProperty(state, first, actual);
|
|
56
|
-
else if (
|
|
71
|
+
else if (isProxyObject(isArray, actual)) setObject(state, actual);
|
|
57
72
|
}
|
|
58
|
-
function setValueInProxy(
|
|
59
|
-
const { isArray, length, property, state, target, value } = parameters;
|
|
73
|
+
function setValueInProxy(isArray, state, target, property, value, length) {
|
|
60
74
|
if (isArray) {
|
|
61
75
|
if (!(!Number.isNaN(Number(property)) || property === "length")) return Reflect.set(target, property, value);
|
|
62
76
|
}
|
|
@@ -68,5 +82,9 @@ function setValueInProxy(parameters) {
|
|
|
68
82
|
}
|
|
69
83
|
return true;
|
|
70
84
|
}
|
|
85
|
+
function updateProxyValue(isArray, state, setObject, setProperty, callback) {
|
|
86
|
+
if (typeof callback !== "function") throw new TypeError("Callback must be a function");
|
|
87
|
+
setProxyValue(isArray, state, setObject, setProperty, callback(state.value));
|
|
88
|
+
}
|
|
71
89
|
//#endregion
|
|
72
|
-
export {
|
|
90
|
+
export { emitProxyValues, getReactiveValueInProxy, getValueInProxy, peekValueInProxy, setProxyValue, setValueInProxy, updateProxyValue };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Computed, ComputedEffect, ReactiveArray, ReactiveState, ReactiveStore, SubscriptionType } from "../models.mjs";
|
|
2
|
+
import { Subscription } from "@oscarpalmer/atoms/subscription";
|
|
3
|
+
import { Key } from "@oscarpalmer/atoms/models";
|
|
4
|
+
//#region src/helpers/subscription.d.ts
|
|
5
|
+
export declare function subscribeToProxy<Value, Item = Value>(isArray: boolean, instance: ReactiveArray<Value> | ReactiveStore<Value>, state: ReactiveState<Value, Item>, mapped: Map<Key, [Computed<unknown>, ComputedEffect]>, first: Key | ((value: unknown) => unknown), second?: ((value: unknown) => unknown) | boolean, third?: boolean): Subscription;
|
|
6
|
+
export declare function subscribeToReactive<Value, Item = Value>(type: SubscriptionType, state: ReactiveState<Value, Item>, callback: (value: unknown) => unknown): Subscription;
|
|
7
|
+
export declare function subscribeToSignal<Value, Item = Value>(state: ReactiveState<Value, Item>, callback: (value: unknown) => unknown, copy: boolean): Subscription;
|
|
8
|
+
//#endregion
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { SUBSCRIPTION_PROPERTY, SUBSCRIPTION_TYPES, SUBSCRIPTION_TYPES_COPY, SUBSCRIPTION_TYPE_COPY, SUBSCRIPTION_TYPE_ORIGINAL } from "../constants.mjs";
|
|
2
|
+
import { getFrozenValue, peekSimpleValue } from "./value.mjs";
|
|
3
|
+
import { getReactiveValueInProxy } from "./proxy.mjs";
|
|
4
|
+
import { isKey } from "@oscarpalmer/atoms/is";
|
|
5
|
+
import { subscriptions } from "@oscarpalmer/atoms/subscription";
|
|
6
|
+
//#region src/helpers/subscription.ts
|
|
7
|
+
function subscribeToProxy(isArray, instance, state, mapped, first, second, third) {
|
|
8
|
+
if (isKey(first)) return getReactiveValueInProxy(instance, mapped, first, isArray).subscribe(second, third);
|
|
9
|
+
return subscribeToSignal(state, first, second === true);
|
|
10
|
+
}
|
|
11
|
+
function subscribeToReactive(type, state, callback) {
|
|
12
|
+
state.subscriptions ??= subscriptions({
|
|
13
|
+
keys: SUBSCRIPTION_TYPES,
|
|
14
|
+
property: SUBSCRIPTION_PROPERTY
|
|
15
|
+
});
|
|
16
|
+
const [subscription, existing] = state.subscriptions.create({
|
|
17
|
+
key: type,
|
|
18
|
+
value: callback
|
|
19
|
+
});
|
|
20
|
+
if (!existing) callback(type === "frozen" ? getFrozenValue(state.value) : peekSimpleValue(state.value, SUBSCRIPTION_TYPES_COPY.has(type)));
|
|
21
|
+
return subscription;
|
|
22
|
+
}
|
|
23
|
+
function subscribeToSignal(state, callback, copy) {
|
|
24
|
+
return subscribeToReactive(copy ? SUBSCRIPTION_TYPE_COPY : SUBSCRIPTION_TYPE_ORIGINAL, state, callback);
|
|
25
|
+
}
|
|
26
|
+
//#endregion
|
|
27
|
+
export { subscribeToProxy, subscribeToReactive, subscribeToSignal };
|
package/dist/helpers/value.d.mts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { ReactiveState } from "../models.mjs";
|
|
2
2
|
//#region src/helpers/value.d.ts
|
|
3
|
-
declare function emitValue<Value>(state: ReactiveState<Value, never>): void;
|
|
4
|
-
declare function equalArrays<Value>(state: ReactiveState<Value[], Value>, first: Value[], second: Value[]): boolean;
|
|
5
|
-
declare function
|
|
6
|
-
declare function
|
|
7
|
-
|
|
8
|
-
export
|
|
3
|
+
export declare function emitValue<Value>(state: ReactiveState<Value, never>): void;
|
|
4
|
+
export declare function equalArrays<Value>(state: ReactiveState<Value[], Value>, first: Value[], second: Value[]): boolean;
|
|
5
|
+
export declare function getFrozenValue(value: unknown): unknown;
|
|
6
|
+
export declare function getSimpleValue<Value>(state: ReactiveState<Value, never>): Value;
|
|
7
|
+
export declare function handleSimpleValue<Value>(state: ReactiveState<Value, Value>, origin: Value | (() => Value | Promise<Value>) | Promise<Value>, setValue: (state: ReactiveState<Value, Value>, value: Value) => void, onAfter?: () => void): void;
|
|
8
|
+
export declare function peekSimpleValue(value: unknown, copy: boolean): unknown;
|
|
9
|
+
export declare function updateSimpleValue<Value>(state: ReactiveState<Value, Value>, callback: (value: Value) => Value, setValue: (state: ReactiveState<Value, Value>, value: Value) => void): void;
|
|
10
|
+
//#endregion
|
package/dist/helpers/value.mjs
CHANGED
|
@@ -1,10 +1,19 @@
|
|
|
1
|
-
import { ACTIVE, BATCH } from "../constants.mjs";
|
|
1
|
+
import { ACTIVE, BATCH, SUBSCRIPTION_TYPES, SUBSCRIPTION_TYPES_COPY, SUBSCRIPTION_TYPE_FROZEN } from "../constants.mjs";
|
|
2
2
|
import { flushHandlers } from "../batch.mjs";
|
|
3
|
+
import { isPlainObject } from "@oscarpalmer/atoms/is";
|
|
3
4
|
//#region src/helpers/value.ts
|
|
4
5
|
function emitValue(state) {
|
|
5
6
|
for (const computed of state.computeds) computed.dirty = true;
|
|
6
|
-
for (const effect of state.effects) BATCH.handlers.
|
|
7
|
-
for (const
|
|
7
|
+
for (const effect of state.effects) BATCH.handlers.set(effect, effect);
|
|
8
|
+
for (const type of SUBSCRIPTION_TYPES) {
|
|
9
|
+
const subscriptions = state.subscriptions?.values.to.keyed?.get(type);
|
|
10
|
+
if (subscriptions != null) for (const [subscription, callback] of subscriptions) BATCH.handlers.set(subscription, {
|
|
11
|
+
callback,
|
|
12
|
+
state,
|
|
13
|
+
frozen: type === SUBSCRIPTION_TYPE_FROZEN,
|
|
14
|
+
copy: SUBSCRIPTION_TYPES_COPY.has(type)
|
|
15
|
+
});
|
|
16
|
+
}
|
|
8
17
|
if (BATCH.depth === 0) flushHandlers();
|
|
9
18
|
}
|
|
10
19
|
function equalArrays(state, first, second) {
|
|
@@ -20,6 +29,12 @@ function equalArrays(state, first, second) {
|
|
|
20
29
|
for (let index = offset; index < length; index += 1) if (!state.equal(first[index], second[index])) return false;
|
|
21
30
|
return true;
|
|
22
31
|
}
|
|
32
|
+
function getFrozenValue(value) {
|
|
33
|
+
let frozen = value;
|
|
34
|
+
if (Array.isArray(frozen)) frozen = Object.freeze(frozen.slice());
|
|
35
|
+
else if (isPlainObject(frozen)) frozen = Object.freeze({ ...frozen });
|
|
36
|
+
return frozen;
|
|
37
|
+
}
|
|
23
38
|
function getSimpleValue(state) {
|
|
24
39
|
if (ACTIVE.computed != null) state.computeds.add(ACTIVE.computed);
|
|
25
40
|
if (ACTIVE.effect != null) state.effects.add(ACTIVE.effect);
|
|
@@ -43,5 +58,16 @@ function handleSimpleValue(state, origin, setValue, onAfter) {
|
|
|
43
58
|
onAfter?.();
|
|
44
59
|
}
|
|
45
60
|
}
|
|
61
|
+
function peekSimpleValue(value, copy) {
|
|
62
|
+
let peeked = value;
|
|
63
|
+
if (!copy) return peeked;
|
|
64
|
+
if (Array.isArray(peeked)) peeked = peeked.slice();
|
|
65
|
+
else if (isPlainObject(peeked)) peeked = { ...peeked };
|
|
66
|
+
return peeked;
|
|
67
|
+
}
|
|
68
|
+
function updateSimpleValue(state, callback, setValue) {
|
|
69
|
+
if (typeof callback !== "function") throw new TypeError("Callback must be a function");
|
|
70
|
+
handleSimpleValue(state, callback(state.value), setValue);
|
|
71
|
+
}
|
|
46
72
|
//#endregion
|
|
47
|
-
export { emitValue, equalArrays, getSimpleValue, handleSimpleValue };
|
|
73
|
+
export { emitValue, equalArrays, getFrozenValue, getSimpleValue, handleSimpleValue, peekSimpleValue, updateSimpleValue };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { startBatch, stopBatch } from "./batch.mjs";
|
|
2
|
-
import { Computed, Effect, ReactiveArray, ReactiveStore, ReadonlySignal, Signal
|
|
2
|
+
import { Computed, Effect, ReactiveArray, ReactiveStore, ReadonlySignal, Signal } from "./models.mjs";
|
|
3
3
|
import { effect } from "./effect.mjs";
|
|
4
4
|
import { isComputed, isEffect, isReactive, isReactiveArray, isReactiveStore, isReadonlySignal, isSignal } from "./helpers/is.mjs";
|
|
5
5
|
import { array } from "./value/array.mjs";
|
|
6
6
|
import { computed } from "./value/computed.mjs";
|
|
7
7
|
import { signal } from "./value/signal.mjs";
|
|
8
8
|
import { store } from "./value/store.mjs";
|
|
9
|
-
|
|
9
|
+
import { Subscription } from "@oscarpalmer/atoms/subscription";
|
|
10
|
+
export { type Computed, type Effect, type ReactiveArray, type ReactiveStore, type ReadonlySignal, type Signal, type Subscription, array, computed, effect, isComputed, isEffect, isReactive, isReactiveArray, isReactiveStore, isReadonlySignal, isSignal, signal, startBatch, stopBatch, store };
|