@oscarpalmer/mora 0.31.0 → 0.33.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 (58) hide show
  1. package/dist/batch.d.mts +4 -5
  2. package/dist/batch.mjs +4 -2
  3. package/dist/constants.d.mts +31 -19
  4. package/dist/constants.mjs +19 -2
  5. package/dist/effect.d.mts +5 -5
  6. package/dist/effect.mjs +8 -5
  7. package/dist/helpers/is.d.mts +14 -9
  8. package/dist/helpers/is.mjs +6 -0
  9. package/dist/helpers/misc.d.mts +4 -0
  10. package/dist/helpers/misc.mjs +9 -0
  11. package/dist/helpers/proxy.d.mts +10 -10
  12. package/dist/helpers/proxy.mjs +83 -27
  13. package/dist/helpers/subscription.d.mts +8 -0
  14. package/dist/helpers/subscription.mjs +30 -0
  15. package/dist/helpers/value.d.mts +10 -7
  16. package/dist/helpers/value.mjs +56 -17
  17. package/dist/index.d.mts +3 -2
  18. package/dist/models.d.mts +82 -101
  19. package/dist/models.mjs +1 -0
  20. package/dist/mora.full.mjs +659 -412
  21. package/dist/value/array.d.mts +5 -5
  22. package/dist/value/array.mjs +70 -109
  23. package/dist/value/computed.d.mts +5 -4
  24. package/dist/value/computed.mjs +45 -31
  25. package/dist/value/readonly.d.mts +4 -6
  26. package/dist/value/readonly.mjs +34 -31
  27. package/dist/value/signal.d.mts +5 -5
  28. package/dist/value/signal.mjs +28 -34
  29. package/dist/value/store.d.mts +5 -5
  30. package/dist/value/store.mjs +25 -83
  31. package/package.json +6 -6
  32. package/src/batch.ts +15 -5
  33. package/src/constants.ts +36 -2
  34. package/src/effect.ts +20 -11
  35. package/src/helpers/is.ts +16 -6
  36. package/src/helpers/misc.ts +15 -0
  37. package/src/helpers/proxy.ts +151 -74
  38. package/src/helpers/subscription.ts +89 -0
  39. package/src/helpers/value.ts +121 -30
  40. package/src/index.ts +1 -1
  41. package/src/models.ts +81 -98
  42. package/src/value/array.ts +116 -226
  43. package/src/value/computed.ts +96 -43
  44. package/src/value/readonly.ts +68 -54
  45. package/src/value/signal.ts +54 -46
  46. package/src/value/store.ts +42 -168
  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/dist/value/reactive.d.mts +0 -5
  56. package/dist/value/reactive.mjs +0 -16
  57. package/src/subscription.ts +0 -45
  58. package/src/value/reactive.ts +0 -24
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, peekSignalValue } 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) : peekSignalValue.call(subscription.instance, subscription.copy));
15
17
  else runEffect(handler);
16
18
  }
17
19
  }
@@ -1,20 +1,32 @@
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 NAME_ALL: Set<string>;
18
- declare const PROPERTY_LENGTH = "length";
19
- //#endregion
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_READONLY, 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 SYMBOL_EFFECT: unique symbol;
21
+ export declare const SYMBOL_STATE: unique symbol;
22
+ export declare const SUBSCRIPTION_PROPERTY: {
23
+ key: string;
24
+ value: string;
25
+ };
26
+ export declare const SUBSCRIPTION_TYPE_COPY: SubscriptionType;
27
+ export declare const SUBSCRIPTION_TYPE_FROZEN: SubscriptionType;
28
+ export declare const SUBSCRIPTION_TYPE_ORIGINAL: SubscriptionType;
29
+ export declare const SUBSCRIPTION_TYPE_READONLY: SubscriptionType;
30
+ export declare const SUBSCRIPTION_TYPES_COPY: Set<SubscriptionType>;
31
+ export declare const SUBSCRIPTION_TYPES: Set<SubscriptionType>;
32
+ //#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",
@@ -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,21 @@ const NAME_ALL = /* @__PURE__ */ new Set([
37
38
  NAME_STORE
38
39
  ]);
39
40
  const PROPERTY_LENGTH = "length";
41
+ const SYMBOL_EFFECT = Symbol("effect");
42
+ const SYMBOL_STATE = Symbol("state");
43
+ const SUBSCRIPTION_PROPERTY = {
44
+ key: NAME_MORA,
45
+ value: NAME_SUBSCRIPTION
46
+ };
47
+ const SUBSCRIPTION_TYPE_COPY = "copy";
48
+ const SUBSCRIPTION_TYPE_FROZEN = "frozen";
49
+ const SUBSCRIPTION_TYPE_ORIGINAL = "original";
50
+ const SUBSCRIPTION_TYPE_READONLY = "readonly";
51
+ const SUBSCRIPTION_TYPES_COPY = /* @__PURE__ */ new Set([SUBSCRIPTION_TYPE_COPY, SUBSCRIPTION_TYPE_READONLY]);
52
+ const SUBSCRIPTION_TYPES = /* @__PURE__ */ new Set([
53
+ ...SUBSCRIPTION_TYPES_COPY,
54
+ SUBSCRIPTION_TYPE_FROZEN,
55
+ SUBSCRIPTION_TYPE_ORIGINAL
56
+ ]);
40
57
  //#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 };
58
+ 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, SYMBOL_EFFECT, SYMBOL_STATE };
package/dist/effect.d.mts CHANGED
@@ -1,14 +1,14 @@
1
1
  import { Effect, EffectState } from "./models.mjs";
2
2
  import { GenericCallback } from "@oscarpalmer/atoms/models";
3
3
  //#region src/effect.d.ts
4
+ declare function Effect(this: any, callback: GenericCallback): void;
4
5
  /**
5
6
  * Create an effect
6
7
  *
7
8
  * @param callback Callback for handling signal effects
8
9
  * @returns Effect
9
10
  */
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 };
11
+ export declare function effect(callback: GenericCallback): Effect;
12
+ export declare function internalEffect(callback: GenericCallback): EffectState;
13
+ export declare function runEffect(state: EffectState): void;
14
+ //#endregion
package/dist/effect.mjs CHANGED
@@ -1,5 +1,10 @@
1
- import { ACTIVE, NAME_EFFECT } from "./constants.mjs";
1
+ import { ACTIVE, NAME_EFFECT, NAME_MORA, SYMBOL_STATE } from "./constants.mjs";
2
2
  //#region src/effect.ts
3
+ function Effect(callback) {
4
+ this[SYMBOL_STATE] = { callback };
5
+ runEffect(this[SYMBOL_STATE]);
6
+ }
7
+ Effect.prototype[NAME_MORA] = NAME_EFFECT;
3
8
  /**
4
9
  * Create an effect
5
10
  *
@@ -11,10 +16,8 @@ function effect(callback) {
11
16
  return getEffect(callback)[0];
12
17
  }
13
18
  function getEffect(callback) {
14
- const state = { callback };
15
- const instance = Object.freeze({ $mora: NAME_EFFECT });
16
- runEffect(state);
17
- return [instance, state];
19
+ const instance = new Effect(callback);
20
+ return [instance, instance[SYMBOL_STATE]];
18
21
  }
19
22
  function internalEffect(callback) {
20
23
  return getEffect(callback)[1];
@@ -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 = unknown>(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 = unknown>(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 = unknown>(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 = unknown>(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
- declare function isReadonlySignal<Value>(value: unknown): value is ReadonlySignal<Value>;
47
- //#endregion
48
- export { isComputed, isEffect, isReactive, isReactiveArray, isReactiveStore, isReadonlySignal, isSignal };
45
+ export declare function isReactiveStore<Value extends PlainObject = 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 = unknown>(value: unknown): value is ReadonlySignal<Value>;
53
+ //#endregion
@@ -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
  }
@@ -0,0 +1,4 @@
1
+ import { ReactiveOptions, ReactiveState } from "../models.mjs";
2
+ //#region src/helpers/misc.d.ts
3
+ export declare function getState(value: unknown, options?: ReactiveOptions<unknown>): ReactiveState;
4
+ //#endregion
@@ -0,0 +1,9 @@
1
+ //#region src/helpers/misc.ts
2
+ function getState(value, options) {
3
+ return {
4
+ value,
5
+ equal: typeof options === "object" && typeof options?.equal === "function" ? options.equal : void 0
6
+ };
7
+ }
8
+ //#endregion
9
+ export { getState };
@@ -1,11 +1,11 @@
1
- import { Computed, ComputedEffect, ReactiveArray, ReactiveState, ReactiveStore, SetValueInProxyParameters } from "../models.mjs";
2
- import { ArrayOrPlainObject, Key, PlainObject } from "@oscarpalmer/atoms/models";
1
+ import { Computed, InternalProxy, ReactiveArray, ReactiveStore } 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
+ export declare function emitProxyValues(this: InternalProxy): void;
5
+ export declare function getReactiveValueInProxy(instance: ReactiveArray<unknown> | ReactiveStore<unknown>, key: Key): Computed<unknown>;
6
+ export declare function getValueInProxy(this: InternalProxy, first?: unknown): unknown;
7
+ export declare function peekValueInProxy(this: InternalProxy, first?: unknown, second?: boolean): unknown;
8
+ export declare function setProxyValue(this: InternalProxy, first?: unknown, second?: unknown): void;
9
+ export declare function setValueInProxy(instance: InternalProxy, target: object, property: PropertyKey, value: unknown): boolean;
10
+ export declare function updateProxyValue(this: InternalProxy, callback: unknown): void;
11
+ //#endregion
@@ -1,32 +1,84 @@
1
- import "../constants.mjs";
2
- import { emitValue } from "./value.mjs";
1
+ import { SYMBOL_STATE } from "../constants.mjs";
2
+ import { emitValue, getSignalValue, peekSignalValue } from "./value.mjs";
3
+ import { startBatch, stopBatch } from "../batch.mjs";
3
4
  import { internalComputed } from "../value/computed.mjs";
5
+ import { isKey, isPlainObject } from "@oscarpalmer/atoms/is";
4
6
  //#region src/helpers/proxy.ts
5
- function emityProxyValues(state, mapped) {
6
- const values = [...mapped.values()];
7
+ function emitProxyValues() {
8
+ const state = this[SYMBOL_STATE];
9
+ state.mapped ??= /* @__PURE__ */ new Map();
10
+ const values = [...state.mapped.values()];
7
11
  const { length } = values;
8
12
  for (let index = 0; index < length; index += 1) values[index][1].dirty = true;
9
- emitValue(state);
13
+ emitValue(this);
10
14
  }
11
- function getReactiveValueInProxy(reactive, mapped, key, isArray) {
12
- let item = mapped.get(key);
15
+ function getReactiveValueInProxy(instance, key) {
16
+ const state = instance[SYMBOL_STATE];
17
+ state.mapped ??= /* @__PURE__ */ new Map();
18
+ const mapKey = String(key);
19
+ let item = state.mapped.get(mapKey);
13
20
  if (item == null) {
14
- item = internalComputed(() => isArray ? reactive.get().at(key) : reactive.get()[key]);
15
- mapped.set(key, item);
21
+ item = internalComputed(() => state.isArray ? instance.get().at(key) : instance.get()[key]);
22
+ state.mapped.set(mapKey, item);
16
23
  }
17
24
  return item[0];
18
25
  }
19
- function setProxyValue(array, state, isObject, isProperty, setObject, setProperty, first, second) {
20
- if (array && first === "length") {
26
+ function getValueInProxy(first) {
27
+ if (this[SYMBOL_STATE].isArray ? typeof first === "number" : isKey(first)) return getReactiveValueInProxy(this, first).get();
28
+ return getSignalValue.call(this);
29
+ }
30
+ function isProxyObject(isArray, value) {
31
+ return value == null || (isArray ? Array.isArray(value) : isPlainObject(value));
32
+ }
33
+ function peekValueInProxy(first, second) {
34
+ const state = this[SYMBOL_STATE];
35
+ let value;
36
+ if (state.isArray ? typeof first === "number" : isKey(first)) value = state.isArray ? state.value.at(first) : state.value[first];
37
+ else value = state.value;
38
+ return peekSignalValue.call([this, value], first === true || second === true);
39
+ }
40
+ function setProxyObject(state, value) {
41
+ if (state.isArray) {
42
+ state.value.splice(0, state.value.length, ...value ?? []);
43
+ return;
44
+ }
45
+ startBatch();
46
+ const actual = value ?? {};
47
+ const proxy = state.value;
48
+ const proxyKeys = Object.keys(proxy);
49
+ const actualKeys = Object.keys(actual);
50
+ let { length } = proxyKeys;
51
+ for (let index = 0; index < length; index += 1) {
52
+ const key = proxyKeys[index];
53
+ proxy[key] = actualKeys.includes(key) ? actual[key] : void 0;
54
+ }
55
+ length = actualKeys.length;
56
+ for (let index = 0; index < length; index += 1) {
57
+ const key = actualKeys[index];
58
+ if (!proxyKeys.includes(key)) proxy[key] = actual[key];
59
+ }
60
+ stopBatch();
61
+ }
62
+ function setProxyProperty(state, property, value) {
63
+ if (!state.isArray) {
64
+ state.value[property] = value;
65
+ return;
66
+ }
67
+ const actual = property < 0 ? state.value.length + property : property;
68
+ if (actual > -1) state.value[actual] = value;
69
+ }
70
+ function setProxyValue(first, second) {
71
+ const state = this[SYMBOL_STATE];
72
+ if (state.isArray && first === "length") {
21
73
  state.value.length = second;
22
74
  return;
23
75
  }
24
- if (isObject(first)) {
25
- setObject(state, first);
76
+ if (isProxyObject(state.isArray, first)) {
77
+ setProxyObject(state, first);
26
78
  return;
27
79
  }
28
- const property = isProperty(first);
29
- if (array && property && Number.isNaN(first)) return;
80
+ const property = state.isArray ? typeof first === "number" : isKey(first);
81
+ if (state.isArray && property && Number.isNaN(first)) return;
30
82
  let actual = property ? second : first;
31
83
  if (typeof actual === "function") try {
32
84
  actual = actual();
@@ -41,32 +93,36 @@ function setProxyValue(array, state, isObject, isProperty, setObject, setPropert
41
93
  actual.then((value) => {
42
94
  if (property && state.promises.get(first) === actual) {
43
95
  state.promises.delete(first);
44
- setProperty(state, first, value);
96
+ setProxyProperty(state, first, value);
45
97
  return;
46
98
  }
47
- if (!property && isObject(value) && state.promise === actual) {
99
+ if (!property && isProxyObject(state.isArray, value) && state.promise === actual) {
48
100
  state.promise = void 0;
49
- setObject(state, value);
101
+ setProxyObject(state, value);
50
102
  }
51
103
  }).catch(() => {
52
104
  if (property && state.promises.get(first) === actual) state.promises.delete(first);
53
105
  else if (!property && state.promise === actual) state.promise = void 0;
54
106
  });
55
- } else if (property) setProperty(state, first, actual);
56
- else if (isObject(actual)) setObject(state, actual);
107
+ } else if (property) setProxyProperty(state, first, actual);
108
+ else if (isProxyObject(state.isArray, actual)) setProxyObject(state, actual);
57
109
  }
58
- function setValueInProxy(parameters) {
59
- const { isArray, length, property, state, target, value } = parameters;
60
- if (isArray) {
110
+ function setValueInProxy(instance, target, property, value) {
111
+ const state = instance[SYMBOL_STATE];
112
+ if (state.isArray) {
61
113
  if (!(!Number.isNaN(Number(property)) || property === "length")) return Reflect.set(target, property, value);
62
114
  }
63
115
  const previous = Reflect.get(target, property);
64
- if (!state.equal(previous, value)) {
116
+ if (!(state.equal ?? Object.is)(previous, value)) {
65
117
  Reflect.set(target, property, value);
66
- emitValue(state);
67
- if (isArray) length?.set(target.length);
118
+ emitValue(instance);
119
+ if (state.isArray) state.length?.set(target.length);
68
120
  }
69
121
  return true;
70
122
  }
123
+ function updateProxyValue(callback) {
124
+ if (typeof callback !== "function") throw new TypeError("Callback must be a function");
125
+ setProxyValue.call(this, callback(this[SYMBOL_STATE].value));
126
+ }
71
127
  //#endregion
72
- export { emityProxyValues, getReactiveValueInProxy, setProxyValue, setValueInProxy };
128
+ export { emitProxyValues, getReactiveValueInProxy, getValueInProxy, peekValueInProxy, setProxyValue, setValueInProxy, updateProxyValue };
@@ -0,0 +1,8 @@
1
+ import { InternalProxy, InternalReadonly, InternalSignal, Subscriber } 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(this: InternalProxy, first: Key | Subscriber<unknown>, second?: Subscriber<unknown> | boolean, third?: boolean): Subscription;
6
+ export declare function subscribeToReadonly(this: InternalReadonly, subscriber: Subscriber<unknown>): Subscription;
7
+ export declare function subscribeToSignal(this: InternalSignal, subscriber: Subscriber<unknown>, copy?: unknown): Subscription;
8
+ //#endregion
@@ -0,0 +1,30 @@
1
+ import { SUBSCRIPTION_PROPERTY, SUBSCRIPTION_TYPES, SUBSCRIPTION_TYPES_COPY, SUBSCRIPTION_TYPE_COPY, SUBSCRIPTION_TYPE_FROZEN, SUBSCRIPTION_TYPE_ORIGINAL, SUBSCRIPTION_TYPE_READONLY, SYMBOL_STATE } from "../constants.mjs";
2
+ import { getFrozenValue, peekSignalValue } 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(first, second, third) {
8
+ if (isKey(first)) return getReactiveValueInProxy(this, first).subscribe(second, third);
9
+ return subscribeToSignal.call(this, first, second);
10
+ }
11
+ function subscribeToReactive(type, subscriber) {
12
+ this[SYMBOL_STATE].subscriptions ??= subscriptions({
13
+ keys: SUBSCRIPTION_TYPES,
14
+ property: SUBSCRIPTION_PROPERTY
15
+ });
16
+ const [subscription, existing] = this[SYMBOL_STATE].subscriptions.create({
17
+ key: type,
18
+ value: subscriber
19
+ });
20
+ if (!existing) subscriber(type === "frozen" ? getFrozenValue(this[SYMBOL_STATE].value) : peekSignalValue.call(this, SUBSCRIPTION_TYPES_COPY.has(type)), subscription);
21
+ return subscription;
22
+ }
23
+ function subscribeToReadonly(subscriber) {
24
+ return subscribeToReactive.call(this, this.frozen ? SUBSCRIPTION_TYPE_FROZEN : SUBSCRIPTION_TYPE_READONLY, subscriber);
25
+ }
26
+ function subscribeToSignal(subscriber, copy) {
27
+ return subscribeToReactive.call(this, copy === true ? SUBSCRIPTION_TYPE_COPY : SUBSCRIPTION_TYPE_ORIGINAL, subscriber);
28
+ }
29
+ //#endregion
30
+ export { subscribeToProxy, subscribeToReadonly, subscribeToSignal };
@@ -1,8 +1,11 @@
1
- import { ReactiveState } from "../models.mjs";
1
+ import { InternalSignal, 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(instance: InternalSignal): void;
4
+ export declare function equalArrays(state: ReactiveState, first: unknown[], second: unknown[]): boolean;
5
+ export declare function getFrozenValue(value: unknown): unknown;
6
+ export declare function getSignalValue(this: InternalSignal): unknown;
7
+ export declare function getStringValue(this: InternalSignal, json?: boolean): string;
8
+ export declare function handleSignalValue(instance: InternalSignal, origin: unknown, setValue: (instance: InternalSignal, value: unknown) => void, onAfter?: () => void): void;
9
+ export declare function peekSignalValue(this: InternalSignal | [InternalSignal, unknown], copy?: boolean): unknown;
10
+ export declare function updateSignalValue(instance: InternalSignal, callback: (value: unknown) => unknown, setValue: (instance: InternalSignal, value: unknown) => void): void;
11
+ //#endregion
@@ -1,31 +1,59 @@
1
- import { ACTIVE, BATCH } from "../constants.mjs";
1
+ import { ACTIVE, BATCH, SUBSCRIPTION_TYPES, SUBSCRIPTION_TYPES_COPY, SUBSCRIPTION_TYPE_FROZEN, SYMBOL_STATE } 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
- function emitValue(state) {
5
- 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);
5
+ function emitValue(instance) {
6
+ const state = instance[SYMBOL_STATE];
7
+ if (state.computeds != null && state.computeds.size > 0) for (const computed of state.computeds) computed.dirty = true;
8
+ if (state.effects != null && state.effects.size > 0) for (const effect of state.effects) BATCH.handlers.set(effect, effect);
9
+ for (const type of SUBSCRIPTION_TYPES) {
10
+ const subscriptions = state.subscriptions?.values.to.keyed?.get(type);
11
+ if (subscriptions != null && subscriptions.size > 0) for (const [subscription, callback] of subscriptions) BATCH.handlers.set(subscription, {
12
+ callback,
13
+ instance,
14
+ state,
15
+ frozen: type === SUBSCRIPTION_TYPE_FROZEN,
16
+ copy: SUBSCRIPTION_TYPES_COPY.has(type)
17
+ });
18
+ }
8
19
  if (BATCH.depth === 0) flushHandlers();
9
20
  }
10
21
  function equalArrays(state, first, second) {
11
- let { length } = first;
22
+ const { length } = first;
12
23
  if (length !== second.length) return false;
24
+ const eq = state.equal ?? Object.is;
13
25
  let offset = 0;
14
26
  if (length >= 100) {
15
27
  offset = Math.round(length / 10);
16
28
  offset = offset > 25 ? 25 : offset;
17
- for (let index = 0; index < offset; index += 1) if (!state.equal(first[index], second[index])) return false;
29
+ for (let index = 0; index < offset; index += 1) if (!(eq(first[index], second[index]) && eq(first[length - index - 1], second[length - index - 1]))) return false;
18
30
  }
19
- length -= offset;
20
- for (let index = offset; index < length; index += 1) if (!state.equal(first[index], second[index])) return false;
31
+ const end = length - offset;
32
+ for (let index = offset; index < end; index += 1) if (!eq(first[index], second[index])) return false;
21
33
  return true;
22
34
  }
23
- function getSimpleValue(state) {
24
- if (ACTIVE.computed != null) state.computeds.add(ACTIVE.computed);
25
- if (ACTIVE.effect != null) state.effects.add(ACTIVE.effect);
26
- return state.value;
35
+ function getFrozenValue(value) {
36
+ let frozen = value;
37
+ if (Array.isArray(frozen)) frozen = Object.freeze(frozen.slice());
38
+ else if (isPlainObject(frozen)) frozen = Object.freeze({ ...frozen });
39
+ return frozen;
40
+ }
41
+ function getSignalValue() {
42
+ if (ACTIVE.computed != null) {
43
+ this[SYMBOL_STATE].computeds ??= /* @__PURE__ */ new Set();
44
+ this[SYMBOL_STATE].computeds.add(ACTIVE.computed);
45
+ }
46
+ if (ACTIVE.effect != null) {
47
+ this[SYMBOL_STATE].effects ??= /* @__PURE__ */ new Set();
48
+ this[SYMBOL_STATE].effects.add(ACTIVE.effect);
49
+ }
50
+ return this[SYMBOL_STATE].value;
27
51
  }
28
- function handleSimpleValue(state, origin, setValue, onAfter) {
52
+ function getStringValue(json) {
53
+ return json === true ? JSON.stringify(this[SYMBOL_STATE].value) : String(this[SYMBOL_STATE].value);
54
+ }
55
+ function handleSignalValue(instance, origin, setValue, onAfter) {
56
+ const state = instance[SYMBOL_STATE];
29
57
  try {
30
58
  let actual = typeof origin === "function" ? origin() : origin;
31
59
  if (actual instanceof Promise) {
@@ -33,15 +61,26 @@ function handleSimpleValue(state, origin, setValue, onAfter) {
33
61
  actual.then((value) => {
34
62
  if (actual === state.promise) {
35
63
  state.promise = void 0;
36
- setValue(state, value);
64
+ setValue(instance, value);
37
65
  }
38
66
  }).catch(() => {
39
67
  if (actual === state.promise) state.promise = void 0;
40
68
  });
41
- } else setValue(state, actual);
69
+ } else setValue(instance, actual);
42
70
  } catch {} finally {
43
71
  onAfter?.();
44
72
  }
45
73
  }
74
+ function peekSignalValue(copy) {
75
+ let peeked = Array.isArray(this) ? this[1] : this[SYMBOL_STATE].value;
76
+ if (copy !== true) return peeked;
77
+ if (Array.isArray(peeked)) peeked = peeked.slice();
78
+ else if (isPlainObject(peeked)) peeked = { ...peeked };
79
+ return peeked;
80
+ }
81
+ function updateSignalValue(instance, callback, setValue) {
82
+ if (typeof callback !== "function") throw new TypeError("Callback must be a function");
83
+ handleSignalValue(instance, callback(instance[SYMBOL_STATE].value), setValue);
84
+ }
46
85
  //#endregion
47
- export { emitValue, equalArrays, getSimpleValue, handleSimpleValue };
86
+ export { emitValue, equalArrays, getFrozenValue, getSignalValue, getStringValue, handleSignalValue, peekSignalValue, updateSignalValue };
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, Unsubscribe } from "./models.mjs";
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
- export { type Computed, type Effect, type ReactiveArray, type ReactiveStore, type ReadonlySignal, type Signal, type Unsubscribe, array, computed, effect, isComputed, isEffect, isReactive, isReactiveArray, isReactiveStore, isReadonlySignal, 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 };