@oscarpalmer/mora 0.30.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.
Files changed (55) hide show
  1. package/dist/batch.d.mts +4 -5
  2. package/dist/batch.mjs +4 -2
  3. package/dist/constants.d.mts +29 -18
  4. package/dist/constants.mjs +19 -2
  5. package/dist/effect.d.mts +4 -5
  6. package/dist/effect.mjs +2 -2
  7. package/dist/helpers/is.d.mts +21 -15
  8. package/dist/helpers/is.mjs +21 -12
  9. package/dist/helpers/proxy.d.mts +12 -10
  10. package/dist/helpers/proxy.mjs +32 -14
  11. package/dist/helpers/subscription.d.mts +8 -0
  12. package/dist/helpers/subscription.mjs +27 -0
  13. package/dist/helpers/value.d.mts +8 -6
  14. package/dist/helpers/value.mjs +30 -4
  15. package/dist/index.d.mts +4 -3
  16. package/dist/index.mjs +2 -2
  17. package/dist/models.d.mts +108 -84
  18. package/dist/mora.full.mjs +504 -247
  19. package/dist/value/array.d.mts +4 -5
  20. package/dist/value/array.mjs +25 -56
  21. package/dist/value/computed.d.mts +3 -4
  22. package/dist/value/computed.mjs +14 -14
  23. package/dist/value/reactive.d.mts +2 -3
  24. package/dist/value/reactive.mjs +0 -1
  25. package/dist/value/readonly.d.mts +5 -0
  26. package/dist/value/readonly.mjs +33 -0
  27. package/dist/value/signal.d.mts +4 -5
  28. package/dist/value/signal.mjs +10 -17
  29. package/dist/value/store.d.mts +4 -5
  30. package/dist/value/store.mjs +17 -41
  31. package/package.json +6 -6
  32. package/src/batch.ts +15 -5
  33. package/src/constants.ts +41 -3
  34. package/src/effect.ts +6 -2
  35. package/src/helpers/is.ts +37 -12
  36. package/src/helpers/proxy.ts +99 -49
  37. package/src/helpers/subscription.ts +78 -0
  38. package/src/helpers/value.ts +69 -4
  39. package/src/index.ts +18 -2
  40. package/src/models.ts +131 -86
  41. package/src/value/array.ts +67 -120
  42. package/src/value/computed.ts +36 -13
  43. package/src/value/reactive.ts +8 -5
  44. package/src/value/readonly.ts +72 -0
  45. package/src/value/signal.ts +22 -16
  46. package/src/value/store.ts +37 -70
  47. package/types/constants.d.ts +1 -1
  48. package/types/index.d.ts +1 -1
  49. package/types/value/array.d.ts +1 -1
  50. package/types/value/computed.d.ts +0 -3
  51. package/types/value/signal.d.ts +1 -1
  52. package/types/value/store.d.ts +1 -1
  53. package/dist/subscription.d.mts +0 -7
  54. package/dist/subscription.mjs +0 -27
  55. 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
- if (typeof handler.destroy === "function") handler.callback(handler.state.value);
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
  }
@@ -1,19 +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_SIGNAL = "signal";
15
- declare const NAME_STORE = "store";
16
- declare const NAME_ALL: Set<string>;
17
- declare const PROPERTY_LENGTH = "length";
18
- //#endregion
19
- 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 };
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
@@ -6,7 +6,7 @@ const ARRAY_PEEK = 10;
6
6
  const BATCH = {
7
7
  depth: 0,
8
8
  flushing: false,
9
- handlers: /* @__PURE__ */ new Set()
9
+ handlers: /* @__PURE__ */ new Map()
10
10
  };
11
11
  const METHODS_AFFECTING_LENGTH = /* @__PURE__ */ new Set([
12
12
  "pop",
@@ -26,14 +26,31 @@ const NAME_ARRAY = "array";
26
26
  const NAME_COMPUTED = "computed";
27
27
  const NAME_EFFECT = "effect";
28
28
  const NAME_MORA = "$mora";
29
+ const NAME_READONLY = "readonly";
29
30
  const NAME_SIGNAL = "signal";
30
31
  const NAME_STORE = "store";
32
+ const NAME_SUBSCRIPTION = "subscription";
31
33
  const NAME_ALL = /* @__PURE__ */ new Set([
32
34
  NAME_ARRAY,
33
35
  NAME_COMPUTED,
36
+ NAME_READONLY,
34
37
  NAME_SIGNAL,
35
38
  NAME_STORE
36
39
  ]);
37
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
+ ]);
38
55
  //#endregion
39
- 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 };
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({ $mora: NAME_EFFECT });
15
+ const instance = Object.freeze({ [NAME_MORA]: NAME_EFFECT });
16
16
  runEffect(state);
17
17
  return [instance, state];
18
18
  }
@@ -1,47 +1,53 @@
1
- import { Computed, Effect, Reactive, ReactiveArray, ReactiveStore, Signal } from "../models.mjs";
1
+ import { Computed, Effect, Reactive, ReactiveArray, ReactiveStore, ReadonlySignal, Signal } from "../models.mjs";
2
2
  import { PlainObject } from "@oscarpalmer/atoms/models";
3
3
  //#region src/helpers/is.d.ts
4
- /**
5
- * Is the value a reactive array?
6
- *
7
- * @param value Value to check
8
- * @returns True if value is a {@link ReactiveArray}
9
- */
10
- declare function isArray<Item>(value: unknown): value is ReactiveArray<Item>;
11
4
  /**
12
5
  * Is the value a computed signal?
13
6
  *
14
7
  * @param value Value to check
15
8
  * @returns True if value is a {@link Computed}
16
9
  */
17
- declare function isComputed<Value>(value: unknown): value is Computed<Value>;
10
+ export declare function isComputed<Value>(value: unknown): value is Computed<Value>;
18
11
  /**
19
12
  * Is the value an effect?
20
13
  *
21
14
  * @param value Value to check
22
15
  * @returns True if value is an {@link Effect}
23
16
  */
24
- declare function isEffect(value: unknown): value is Effect;
17
+ export declare function isEffect(value: unknown): value is Effect;
25
18
  /**
26
19
  * Is the value reactive?
27
20
  *
28
21
  * @param value Value to check
29
22
  * @returns True if value is a {@link Reactive}
30
23
  */
31
- declare function isReactive<Value>(value: unknown): value is Reactive<Value>;
24
+ export declare function isReactive<Value>(value: unknown): value is Reactive<Value>;
32
25
  /**
33
26
  * Is the value a signal?
34
27
  *
35
28
  * @param value Value to check
36
29
  * @returns True if value is a {@link Signal}
37
30
  */
38
- declare function isSignal<Value>(value: unknown): value is Signal<Value>;
31
+ export declare function isSignal<Value>(value: unknown): value is Signal<Value>;
32
+ /**
33
+ * Is the value a reactive array?
34
+ *
35
+ * @param value Value to check
36
+ * @returns True if value is a {@link ReactiveArray}
37
+ */
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 isStore<Value extends PlainObject>(value: unknown): value is ReactiveStore<Value>;
46
- //#endregion
47
- export { isArray, isComputed, isEffect, isReactive, isSignal, isStore };
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
@@ -1,15 +1,6 @@
1
- import { NAME_ALL, NAME_ARRAY, NAME_COMPUTED, NAME_EFFECT, NAME_SIGNAL, NAME_STORE } from "../constants.mjs";
1
+ import { NAME_ALL, NAME_ARRAY, NAME_COMPUTED, NAME_EFFECT, NAME_READONLY, NAME_SIGNAL, NAME_STORE } from "../constants.mjs";
2
2
  //#region src/helpers/is.ts
3
3
  /**
4
- * Is the value a reactive array?
5
- *
6
- * @param value Value to check
7
- * @returns True if value is a {@link ReactiveArray}
8
- */
9
- function isArray(value) {
10
- return isMora(value, NAME_ARRAY);
11
- }
12
- /**
13
4
  * Is the value a computed signal?
14
5
  *
15
6
  * @param value Value to check
@@ -49,13 +40,31 @@ function isSignal(value) {
49
40
  return isMora(value, NAME_SIGNAL);
50
41
  }
51
42
  /**
43
+ * Is the value a reactive array?
44
+ *
45
+ * @param value Value to check
46
+ * @returns True if value is a {@link ReactiveArray}
47
+ */
48
+ function isReactiveArray(value) {
49
+ return isMora(value, NAME_ARRAY);
50
+ }
51
+ /**
52
52
  * Is the value a reactive store?
53
53
  *
54
54
  * @param value Value to check
55
55
  * @returns True if value is a {@link Store}
56
56
  */
57
- function isStore(value) {
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
+ */
66
+ function isReadonlySignal(value) {
67
+ return isMora(value, NAME_READONLY);
68
+ }
60
69
  //#endregion
61
- export { isArray, isComputed, isEffect, isReactive, isSignal, isStore };
70
+ export { isComputed, isEffect, isReactive, isReactiveArray, isReactiveStore, isReadonlySignal, isSignal };
@@ -1,11 +1,13 @@
1
- import { Computed, ComputedEffect, ReactiveArray, ReactiveState, ReactiveStore, SetValueInProxyParameters } from "../models.mjs";
2
- import { ArrayOrPlainObject, Key, PlainObject } from "@oscarpalmer/atoms/models";
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
- declare function emityProxyValues<Value>(state: ReactiveState<Value, Value>, mapped: Map<Key, [Computed<unknown>, ComputedEffect]>): void;
5
- declare function emityProxyValues<Value>(state: ReactiveState<Value[], Value>, mapped: Map<Key, [Computed<unknown>, ComputedEffect]>): void;
6
- declare function getReactiveValueInProxy<Value, Result = Value>(array: ReactiveArray<Value>, mapped: Map<Key, [Computed<unknown>, ComputedEffect]>, index: number, isArray: true): Computed<Result>;
7
- declare function getReactiveValueInProxy<Value extends PlainObject>(store: ReactiveStore<Value>, mapped: Map<Key, [Computed<unknown>, ComputedEffect]>, key: Key, isArray: false): Computed<unknown>;
8
- 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;
9
- declare function setValueInProxy<Value extends ArrayOrPlainObject, Equal>(parameters: SetValueInProxyParameters<Value, Equal>): boolean;
10
- //#endregion
11
- export { emityProxyValues, getReactiveValueInProxy, setProxyValue, setValueInProxy };
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
@@ -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 emityProxyValues(state, mapped) {
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
- let item = mapped.get(key);
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(key, item);
17
+ mapped.set(mapKey, item);
16
18
  }
17
19
  return item[0];
18
20
  }
19
- function setProxyValue(array, state, isObject, isProperty, setObject, setProperty, first, second) {
20
- if (array && first === "length") {
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 (isObject(first)) {
39
+ if (isProxyObject(isArray, first)) {
25
40
  setObject(state, first);
26
41
  return;
27
42
  }
28
- const property = isProperty(first);
29
- if (array && property && Number.isNaN(first)) return;
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 && isObject(value) && state.promise === actual) {
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 (isObject(actual)) setObject(state, actual);
71
+ else if (isProxyObject(isArray, actual)) setObject(state, actual);
57
72
  }
58
- function setValueInProxy(parameters) {
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 { emityProxyValues, getReactiveValueInProxy, setProxyValue, setValueInProxy };
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 };
@@ -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 getSimpleValue<Value>(state: ReactiveState<Value, never>): Value;
6
- 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;
7
- //#endregion
8
- export { emitValue, equalArrays, getSimpleValue, handleSimpleValue };
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
@@ -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.add(effect);
7
- for (const [, subscription] of state.subscriptions) BATCH.handlers.add(subscription);
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, Signal, Unsubscribe } from "./models.mjs";
2
+ import { Computed, Effect, ReactiveArray, ReactiveStore, ReadonlySignal, Signal } from "./models.mjs";
3
3
  import { effect } from "./effect.mjs";
4
- import { isArray, isComputed, isEffect, isReactive, isSignal } from "./helpers/is.mjs";
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
- export { type Computed, type Effect, type ReactiveArray, type ReactiveStore, type Signal, type Unsubscribe, array, computed, effect, isArray, isComputed, isEffect, isReactive, isSignal, signal, startBatch, stopBatch, store };
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 };
package/dist/index.mjs CHANGED
@@ -1,8 +1,8 @@
1
1
  import { effect } from "./effect.mjs";
2
2
  import { startBatch, stopBatch } from "./batch.mjs";
3
- import { isArray, isComputed, isEffect, isReactive, isSignal } from "./helpers/is.mjs";
3
+ import { isComputed, isEffect, isReactive, isReactiveArray, isReactiveStore, isReadonlySignal, isSignal } from "./helpers/is.mjs";
4
4
  import { computed } from "./value/computed.mjs";
5
5
  import { signal } from "./value/signal.mjs";
6
6
  import { array } from "./value/array.mjs";
7
7
  import { store } from "./value/store.mjs";
8
- export { array, computed, effect, isArray, isComputed, isEffect, isReactive, isSignal, signal, startBatch, stopBatch, store };
8
+ export { array, computed, effect, isComputed, isEffect, isReactive, isReactiveArray, isReactiveStore, isReadonlySignal, isSignal, signal, startBatch, stopBatch, store };