@oscarpalmer/mora 0.21.0 → 0.23.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 (70) hide show
  1. package/dist/batch.js +11 -25
  2. package/dist/constants.js +34 -0
  3. package/dist/effect.js +18 -28
  4. package/dist/helpers/is.js +9 -26
  5. package/dist/helpers/proxy.js +36 -48
  6. package/dist/helpers/value.js +21 -48
  7. package/dist/index.js +3 -16
  8. package/dist/models.js +0 -0
  9. package/dist/mora.full.js +218 -130
  10. package/dist/subscription.js +22 -28
  11. package/dist/value/array.js +97 -182
  12. package/dist/value/computed.js +33 -58
  13. package/dist/value/reactive.js +29 -53
  14. package/dist/value/signal.js +21 -33
  15. package/dist/value/store.js +37 -60
  16. package/package.json +17 -24
  17. package/src/batch.ts +11 -13
  18. package/src/constants.ts +49 -0
  19. package/src/effect.ts +11 -16
  20. package/src/helpers/is.ts +41 -19
  21. package/src/helpers/proxy.ts +34 -38
  22. package/src/helpers/value.ts +19 -14
  23. package/src/index.ts +1 -1
  24. package/src/models.ts +66 -0
  25. package/src/subscription.ts +14 -16
  26. package/src/value/array.ts +87 -45
  27. package/src/value/computed.ts +29 -39
  28. package/src/value/reactive.ts +9 -24
  29. package/src/value/signal.ts +9 -3
  30. package/src/value/store.ts +40 -9
  31. package/types/batch.d.ts +3 -5
  32. package/types/constants.d.ts +14 -0
  33. package/types/effect.d.ts +2 -1
  34. package/types/helpers/is.d.ts +23 -10
  35. package/types/helpers/proxy.d.ts +2 -3
  36. package/types/helpers/value.d.ts +1 -1
  37. package/types/index.d.ts +1 -0
  38. package/types/models.d.ts +56 -0
  39. package/types/subscription.d.ts +3 -6
  40. package/types/value/array.d.ts +48 -7
  41. package/types/value/computed.d.ts +2 -11
  42. package/types/value/reactive.d.ts +8 -17
  43. package/types/value/signal.d.ts +7 -1
  44. package/types/value/store.d.ts +28 -4
  45. package/dist/batch.cjs +0 -32
  46. package/dist/effect.cjs +0 -30
  47. package/dist/helpers/is.cjs +0 -40
  48. package/dist/helpers/proxy.cjs +0 -56
  49. package/dist/helpers/value.cjs +0 -54
  50. package/dist/index.cjs +0 -21
  51. package/dist/node_modules/@oscarpalmer/atoms/dist/internal/is.cjs +0 -17
  52. package/dist/node_modules/@oscarpalmer/atoms/dist/internal/is.js +0 -17
  53. package/dist/subscription.cjs +0 -32
  54. package/dist/value/array.cjs +0 -187
  55. package/dist/value/computed.cjs +0 -60
  56. package/dist/value/reactive.cjs +0 -55
  57. package/dist/value/signal.cjs +0 -36
  58. package/dist/value/store.cjs +0 -63
  59. package/types/batch.d.cts +0 -79
  60. package/types/effect.d.cts +0 -16
  61. package/types/helpers/is.d.cts +0 -259
  62. package/types/helpers/proxy.d.cts +0 -244
  63. package/types/helpers/value.d.cts +0 -71
  64. package/types/index.d.cts +0 -281
  65. package/types/subscription.d.cts +0 -71
  66. package/types/value/array.d.cts +0 -161
  67. package/types/value/computed.d.cts +0 -81
  68. package/types/value/reactive.d.cts +0 -68
  69. package/types/value/signal.d.cts +0 -87
  70. package/types/value/store.d.cts +0 -136
@@ -7,24 +7,37 @@ import type { Signal } from '../value/signal';
7
7
  import type { Store } from '../value/store';
8
8
  /**
9
9
  * Is the value a reactive array?
10
+ * @param value Value to check
11
+ * @returns True if value is a {@link ReactiveArray}
10
12
  */
11
- export declare function isArray(value: unknown): value is ReactiveArray<unknown>;
13
+ export declare function isArray<Item>(value: unknown): value is ReactiveArray<Item>;
12
14
  /**
13
15
  * Is the value a computed signal?
16
+ * @param value Value to check
17
+ * @returns True if value is a {@link Computed}
14
18
  */
15
- export declare function isComputed(value: unknown): value is Computed<unknown>;
19
+ export declare function isComputed<Value>(value: unknown): value is Computed<Value>;
16
20
  /**
17
21
  * Is the value an effect?
22
+ * @param value Value to check
23
+ * @returns True if value is an {@link Effect}
18
24
  */
19
25
  export declare function isEffect(value: unknown): value is Effect;
20
- export declare function isReactive(value: unknown): value is Reactive<unknown>;
26
+ /**
27
+ * Is the value reactive?
28
+ * @param value Value to check
29
+ * @returns True if value is a {@link Reactive}
30
+ */
31
+ export declare function isReactive<Value, Equal = Value>(value: unknown): value is Reactive<Value, Equal>;
21
32
  /**
22
33
  * Is the value a signal?
34
+ * @param value Value to check
35
+ * @returns True if value is a {@link Signal}
36
+ */
37
+ export declare function isSignal<Value>(value: unknown): value is Signal<Value>;
38
+ /**
39
+ * Is the value a reactive store?
40
+ * @param value Value to check
41
+ * @returns True if value is a {@link Store}
23
42
  */
24
- export declare function isSignal(value: unknown): value is Signal<unknown>;
25
- export declare function isStore(value: unknown): value is Store<PlainObject>;
26
- export declare const arrayName = "array";
27
- export declare const computedName = "computed";
28
- export declare const effectName = "effect";
29
- export declare const signalName = "signal";
30
- export declare const storeName = "store";
43
+ export declare function isStore<Value extends PlainObject>(value: unknown): value is Store<Value>;
@@ -1,10 +1,9 @@
1
1
  import type { ArrayOrPlainObject, Key, PlainObject } from '@oscarpalmer/atoms/models';
2
+ import type { SetValueInProxyParameters } from '../models';
2
3
  import type { ReactiveArray } from '../value/array';
3
4
  import { type Computed } from '../value/computed';
4
- import type { ReactiveState } from '../value/reactive';
5
- import type { Signal } from '../value/signal';
6
5
  import type { Store } from '../value/store';
7
6
  export declare function getReactiveValueInProxy<Value>(array: ReactiveArray<Value>, mapped: Map<Key, Computed<unknown>>, index: number, isArray: true): Computed<unknown>;
8
7
  export declare function getReactiveValueInProxy<Value extends PlainObject>(store: Store<Value>, mapped: Map<Key, Computed<unknown>>, key: Key, isArray: false): Computed<unknown>;
9
8
  export declare function setProxyValue(proxy: ArrayOrPlainObject, value: ArrayOrPlainObject): void;
10
- export declare function setValueInProxy<Value extends ArrayOrPlainObject, Equal>(target: Value, property: PropertyKey, value: unknown, state: ReactiveState<Value, Equal>, isArray: boolean, length?: Signal<number>): boolean;
9
+ export declare function setValueInProxy<Value extends ArrayOrPlainObject, Equal>(parameters: SetValueInProxyParameters<Value, Equal>): boolean;
@@ -1,4 +1,4 @@
1
- import type { ReactiveState } from '../value/reactive';
1
+ import type { ReactiveState } from '../models';
2
2
  export declare function emitValue<Value>(state: ReactiveState<Value, never>): void;
3
3
  export declare function equalArrays<Value>(state: ReactiveState<Value[], Value>, first: Value[], second: Value[]): boolean;
4
4
  export declare function getValue<Value>(state: ReactiveState<Value, never>): Value;
package/types/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export { startBatch, stopBatch } from './batch';
2
2
  export { effect, type Effect } from './effect';
3
3
  export { isArray, isComputed, isEffect, isReactive, isSignal, } from './helpers/is';
4
+ export type { Unsubscribe } from './models';
4
5
  export { array, type ReactiveArray } from './value/array';
5
6
  export { computed, type Computed } from './value/computed';
6
7
  export type { Reactive } from './value/reactive';
@@ -0,0 +1,56 @@
1
+ import type { GenericCallback } from '@oscarpalmer/atoms/models';
2
+ import type { Effect } from './effect';
3
+ import type { Subscription } from './subscription';
4
+ import type { Computed } from './value/computed';
5
+ import type { Signal } from './value/signal';
6
+ export type Active = {
7
+ computed?: Computed<unknown>;
8
+ effect?: Effect;
9
+ };
10
+ export type Batch = {
11
+ depth: number;
12
+ handlers: Set<Effect | Subscription>;
13
+ };
14
+ export type ComputedEffect = {
15
+ dirty: boolean;
16
+ instance: Effect;
17
+ };
18
+ export type EffectState = {
19
+ callback: GenericCallback;
20
+ };
21
+ export type InternalComputed = {
22
+ readonly effect: ComputedEffect;
23
+ readonly state: ReactiveState<unknown, unknown>;
24
+ };
25
+ export type InternalEffect = {
26
+ state: EffectState;
27
+ };
28
+ export type ReactiveOptions<Value> = {
29
+ /**
30
+ * Method for comparing values for equality
31
+ * @param first First value
32
+ * @param second Second value
33
+ * @returns Are the values equal?
34
+ * @default Object.is
35
+ */
36
+ equal?: (first: Value, second: Value) => boolean;
37
+ };
38
+ export type ReactiveState<Value, Equal> = {
39
+ computeds: Set<Computed<unknown>>;
40
+ effects: Set<Effect>;
41
+ equal: (first: Equal, second: Equal) => boolean;
42
+ subscriptions: Map<GenericCallback, Subscription>;
43
+ value: Value;
44
+ };
45
+ export type SetValueInProxyParameters<Value, Equal> = {
46
+ target: Value;
47
+ property: PropertyKey;
48
+ value: unknown;
49
+ state: ReactiveState<Value, Equal>;
50
+ isArray: boolean;
51
+ length?: Signal<number>;
52
+ };
53
+ /**
54
+ * Unsubscribe from changes
55
+ */
56
+ export type Unsubscribe = () => void;
@@ -1,14 +1,11 @@
1
1
  import type { GenericCallback } from '@oscarpalmer/atoms/models';
2
- import type { ReactiveState } from './value/reactive';
2
+ import type { ReactiveState, Unsubscribe } from './models';
3
3
  export declare class Subscription {
4
- state: ReactiveState<unknown, never>;
5
4
  callback: GenericCallback;
5
+ state: ReactiveState<unknown, never>;
6
6
  constructor(state: ReactiveState<unknown, never>, callback: GenericCallback);
7
+ destroy(): void;
7
8
  }
8
- /**
9
- * Unsubscribe from changes
10
- */
11
- export type Unsubscribe = () => void;
12
9
  export declare function noop(): void;
13
10
  export declare function subscribe<Value>(state: ReactiveState<Value, never>, callback: (value: Value) => void): Unsubscribe;
14
11
  export declare function unsubscribe<Value>(state: ReactiveState<Value, never>, callback: (value: Value) => void): void;
@@ -1,6 +1,6 @@
1
- import { type Unsubscribe } from '../subscription';
1
+ import type { ReactiveOptions, Unsubscribe } from '../models';
2
2
  import { type Computed } from './computed';
3
- import { Reactive, type ReactiveOptions } from './reactive';
3
+ import { Reactive } from './reactive';
4
4
  export declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
5
5
  #private;
6
6
  /**
@@ -16,61 +16,93 @@ export declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
16
16
  * Clear the array
17
17
  */
18
18
  clear(): void;
19
+ /**
20
+ * Create a computed, filtered array
21
+ * @param callback Callback to evaluate each item
22
+ * @return Computed array of filtered items
23
+ */
24
+ filter(callback: (item: Item, index: number, array: Item[]) => boolean): Computed<Item[]>;
19
25
  /**
20
26
  * @inheritdoc
21
27
  */
22
28
  get(): Item[];
23
29
  /**
24
30
  * Get the value at an index
31
+ * @param index Index of item to get _(if negative, starts from the end)_
32
+ * @returns Item at index, or `undefined` if it doesn't exist
25
33
  */
26
34
  get(index: number): Item | undefined;
27
35
  /**
28
36
  * Get the length of the array
37
+ * @returns Length of the array
29
38
  */
30
39
  get(property: 'length'): number;
31
- /**
32
- * Create a computed, filtered array
33
- */
34
- filter(callback: (item: Item, index: number, array: Item[]) => boolean): Computed<Item[]>;
35
40
  /**
36
41
  * Create a computed, mapped array
42
+ * @param callback Callback to transform each item
43
+ * @return Computed array of mapped items
37
44
  */
38
45
  map<Mapped>(callback: (item: Item, index: number, array: Item[]) => Mapped): Computed<Mapped[]>;
46
+ /**
47
+ * Notify dependents of changes
48
+ *
49
+ * _This bypasses equality checks and will immediately notify dependents.
50
+ * Use this only if you're modifying nested data that would be ignored by equality checks._
51
+ */
52
+ notify(): void;
39
53
  /**
40
54
  * @inheritdoc
41
55
  */
42
56
  peek(): Item[];
57
+ /**
58
+ * Get the value at an index _(without reactivity)_
59
+ * @param index Index of item to get _(if negative, starts from the end)_
60
+ * @returns Item at index, or `undefined` if it doesn't exist
61
+ */
43
62
  peek(index: number): Item | undefined;
44
63
  /**
45
64
  * Get the length of the array _(without reactivity)_
65
+ * @returns Length of the array
46
66
  */
47
- peek(length: true): number;
67
+ peek(property: 'length'): number;
48
68
  /**
49
69
  * Remove and return the last item of the array
70
+ * @returns Removed item, or `undefined` if the array is empty
50
71
  */
51
72
  pop(): Item | undefined;
52
73
  /**
53
74
  * Add items to the end of the array
75
+ * @param items Items to add
76
+ * @returns New array length
54
77
  */
55
78
  push(...items: Item[]): number;
56
79
  /**
57
80
  * Set the value
81
+ * @param value New array of items _(defaults to an empty array)_
58
82
  */
59
83
  set(value?: Item[]): void;
60
84
  /**
61
85
  * Set the value at an index
86
+ * @param index Index of item to set __(if negative, starts from the end)_
87
+ * @param value New item
62
88
  */
63
89
  set(index: number, value: Item): void;
64
90
  /**
65
91
  * Set the length of the array
92
+ * @param value New array length
66
93
  */
67
94
  set(property: 'length', value: number): void;
68
95
  /**
69
96
  * Remove and return the first item of the array
97
+ * @returns Removed item, or `undefined` if the array is empty
70
98
  */
71
99
  shift(): Item | undefined;
72
100
  /**
73
101
  * Remove and return items from the array _(and optionally add new items)_
102
+ * @param from Index to start removing items from
103
+ * @param to Index to stop removing items at _(defaults to the end of the array)_
104
+ * @param items Optional items to add
105
+ * @returns Removed items
74
106
  */
75
107
  splice(from: number, to?: number, ...items: Item[]): Item[];
76
108
  /**
@@ -79,18 +111,27 @@ export declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
79
111
  subscribe(callback: (value: Item[]) => void): Unsubscribe;
80
112
  /**
81
113
  * Subscribe to changes at a specific index
114
+ * @param index Index of item to subscribe to
115
+ * @param callback Callback for changes
116
+ * @returns Unsubscribe callback
82
117
  */
83
118
  subscribe(index: number, callback: (value: Item | undefined) => void): Unsubscribe;
84
119
  /**
85
120
  * Add items to the beginning of the array
121
+ * @param items Items to add
122
+ * @returns New array length
86
123
  */
87
124
  unshift(...items: Item[]): number;
88
125
  /**
89
126
  * Update the value _(based on the current value)_
127
+ * @param callback Callback to update the value
90
128
  */
91
129
  update(callback: (value: Item[]) => Item[]): void;
92
130
  }
93
131
  /**
94
132
  * Create a reactive array
133
+ * @param value Initial array of items
134
+ * @param options Optional reactivity options
135
+ * @returns Reactive array
95
136
  */
96
137
  export declare function array<Item>(value: Item[], options?: ReactiveOptions<Item>): ReactiveArray<Item>;
@@ -1,14 +1,5 @@
1
- import { type Effect } from '../effect';
2
- import { Reactive, type ReactiveOptions, type ReactiveState } from './reactive';
3
- export type ComputedEffect = {
4
- dirty: boolean;
5
- instance: Effect;
6
- };
7
- export type InternalComputed = {
8
- readonly effect: ComputedEffect;
9
- readonly state: ReactiveState<unknown, unknown>;
10
- };
11
- export declare let activeComputed: Computed<unknown> | undefined;
1
+ import type { ReactiveOptions } from '../models';
2
+ import { Reactive } from './reactive';
12
3
  export declare class Computed<Value> extends Reactive<Value> {
13
4
  private readonly effect;
14
5
  constructor(callback: () => Value, options?: ReactiveOptions<Value>);
@@ -1,45 +1,36 @@
1
- import type { GenericCallback } from '@oscarpalmer/atoms/models';
2
- import type { Effect } from '../effect';
3
- import { type Subscription, type Unsubscribe } from '../subscription';
4
- import type { Computed } from './computed';
1
+ import type { ReactiveOptions, ReactiveState, Unsubscribe } from '../models';
5
2
  export declare abstract class Reactive<Value, Equal = Value> {
6
3
  protected readonly state: ReactiveState<Value, Equal>;
7
4
  constructor(name: string, value: Value, options?: ReactiveOptions<Equal>);
8
5
  /**
9
6
  * Get the value
7
+ * @return Current value
10
8
  */
11
9
  abstract get(): Value;
12
10
  /**
13
11
  * Get the value _(without reactivity)_
12
+ * @return Current value
14
13
  */
15
14
  peek(): Value;
16
15
  /**
17
16
  * Subscribe to changes
17
+ * @param callback Callback for changes
18
+ * @return Unsubscribe callback
18
19
  */
19
20
  subscribe(callback: (value: Value) => void): Unsubscribe;
20
21
  /**
21
22
  * JSON representation of the value
23
+ * @return JSON value
22
24
  */
23
25
  toJSON(): Value;
24
26
  /**
25
27
  * String representation of the value
28
+ * @return Value as string
26
29
  */
27
30
  toString(): string;
28
31
  /**
29
32
  * Unsubscribe from changes
33
+ * @param callback Callback to unsubscribe
30
34
  */
31
35
  unsubscribe(callback: (value: Value) => void): void;
32
36
  }
33
- export type ReactiveOptions<Value> = {
34
- /**
35
- * Method to compare values for equality
36
- */
37
- equal?: (first: Value, second: Value) => boolean;
38
- };
39
- export type ReactiveState<Value, Equal> = {
40
- computeds: Set<Computed<unknown>>;
41
- effects: Set<Effect>;
42
- equal: (first: Equal, second: Equal) => boolean;
43
- subscriptions: Map<GenericCallback, Subscription>;
44
- value: Value;
45
- };
@@ -1,4 +1,5 @@
1
- import { Reactive, type ReactiveOptions } from './reactive';
1
+ import type { ReactiveOptions } from '../models';
2
+ import { Reactive } from './reactive';
2
3
  export declare class Signal<Value> extends Reactive<Value> {
3
4
  constructor(value: Value, options?: ReactiveOptions<Value>);
4
5
  /**
@@ -7,14 +8,19 @@ export declare class Signal<Value> extends Reactive<Value> {
7
8
  get(): Value;
8
9
  /**
9
10
  * Set the value
11
+ * @param value New value
10
12
  */
11
13
  set(value: Value): void;
12
14
  /**
13
15
  * Update the value _(based on the current value)_
16
+ * @param callback Callback to update the value
14
17
  */
15
18
  update(callback: (value: Value) => Value): void;
16
19
  }
17
20
  /**
18
21
  * Create a reactive value
22
+ * @param value Initial value
23
+ * @param options Optional reactivity options
24
+ * @returns Reactive value
19
25
  */
20
26
  export declare function signal<Value>(value: Value, options?: ReactiveOptions<Value>): Signal<Value>;
@@ -1,6 +1,6 @@
1
1
  import type { Key, PlainObject } from '@oscarpalmer/atoms/models';
2
- import { type Unsubscribe } from '../subscription';
3
- import { Reactive, type ReactiveOptions } from './reactive';
2
+ import type { ReactiveOptions, Unsubscribe } from '../models';
3
+ import { Reactive } from './reactive';
4
4
  export declare class Store<Value extends PlainObject> extends Reactive<Value, Value> {
5
5
  #private;
6
6
  constructor(value: Value, options?: ReactiveOptions<Value>);
@@ -9,35 +9,49 @@ export declare class Store<Value extends PlainObject> extends Reactive<Value, Va
9
9
  */
10
10
  get(): Value;
11
11
  /**
12
- * Get a value by key _(without reactivity)_
12
+ * Get a value by key
13
+ * @param key Key of the value to get
14
+ * @returns Value for the specified key, or `undefined` if it doesn't exist
13
15
  */
14
16
  get(key: keyof Value): Value[keyof Value];
15
17
  /**
16
- * Get a value by key _(without reactivity)_
18
+ * Get a value by key
19
+ * @param key Key of the value to get
20
+ * @returns Value for the specified key, or `undefined` if it doesn't exist
17
21
  */
18
22
  get(key: Key): unknown;
19
23
  /**
20
24
  * Get the value _(without reactivity)_
25
+ * @returns The current value
21
26
  */
22
27
  peek(): Value;
23
28
  /**
24
29
  * Get a value by key _(without reactivity)_
30
+ * @param key Key of the value to get
31
+ * @returns Value for the specified key, or `undefined` if it doesn't exist
25
32
  */
26
33
  peek(key: keyof Value): Value[keyof Value];
27
34
  /**
28
35
  * Get a value by key _(without reactivity)_
36
+ * @param key Key of the value to get
37
+ * @returns Value for the specified key, or `undefined` if it doesn't exist
29
38
  */
30
39
  peek(key: Key): unknown;
31
40
  /**
32
41
  * Set the value
42
+ * @param value New value _(defaults to an empty object)_
33
43
  */
34
44
  set(value?: Value): void;
35
45
  /**
36
46
  * Set a value by key
47
+ * @param key Key of the value to set
48
+ * @param value New value
37
49
  */
38
50
  set(key: keyof Value, value: Value[keyof Value]): void;
39
51
  /**
40
52
  * Set a value by key
53
+ * @param key Key of the value to set
54
+ * @param value New value
41
55
  */
42
56
  set(key: Key, value: unknown): void;
43
57
  /**
@@ -46,18 +60,28 @@ export declare class Store<Value extends PlainObject> extends Reactive<Value, Va
46
60
  subscribe(callback: (value: Value) => void): Unsubscribe;
47
61
  /**
48
62
  * Subscribe to changes for a specific key
63
+ * @param key Key of the value to subscribe to
64
+ * @param callback Callback for changes
65
+ * @returns Unsubscribe callback
49
66
  */
50
67
  subscribe<Key extends keyof Value>(key: Key, callback: (value: Value[Key] | undefined) => void): Unsubscribe;
51
68
  /**
52
69
  * Subscribe to changes for a specific key
70
+ * @param key Key of the value to subscribe to
71
+ * @param callback Callback for changes
72
+ * @returns Unsubscribe callback
53
73
  */
54
74
  subscribe(key: Key, callback: (value: unknown) => void): Unsubscribe;
55
75
  /**
56
76
  * Update the value _(based on the current value)_
77
+ * @param callback Callback to update the value
57
78
  */
58
79
  update(callback: (value: Value) => Value): void;
59
80
  }
60
81
  /**
61
82
  * Create a reactive store
83
+ * @param value Initial object value
84
+ * @param options Optional reactivity options
85
+ * @returns Reactive store
62
86
  */
63
87
  export declare function store<Value extends PlainObject>(value: Value, options?: ReactiveOptions<Value>): Store<Value>;
package/dist/batch.cjs DELETED
@@ -1,32 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const effect = require("./effect.cjs");
4
- const helpers_is = require("./helpers/is.cjs");
5
- function flushHandlers() {
6
- while (exports.batchDepth === 0 && batchedHandlers.size > 0) {
7
- const handlers = [...batchedHandlers];
8
- batchedHandlers.clear();
9
- for (const handler of handlers) {
10
- if (helpers_is.isEffect(handler)) {
11
- effect.runEffect(handler);
12
- } else {
13
- handler.callback(handler.state.value);
14
- }
15
- }
16
- }
17
- }
18
- function startBatch() {
19
- exports.batchDepth += 1;
20
- }
21
- function stopBatch() {
22
- if (exports.batchDepth > 0) {
23
- exports.batchDepth -= 1;
24
- }
25
- flushHandlers();
26
- }
27
- const batchedHandlers = /* @__PURE__ */ new Set();
28
- exports.batchDepth = 0;
29
- exports.batchedHandlers = batchedHandlers;
30
- exports.flushHandlers = flushHandlers;
31
- exports.startBatch = startBatch;
32
- exports.stopBatch = stopBatch;
package/dist/effect.cjs DELETED
@@ -1,30 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const helpers_is = require("./helpers/is.cjs");
4
- class Effect {
5
- constructor(callback) {
6
- Object.defineProperty(this, "$mora", {
7
- value: helpers_is.effectName
8
- });
9
- this.state = {
10
- callback
11
- };
12
- runEffect(this);
13
- }
14
- }
15
- function runEffect(effect2) {
16
- const previousEffect = exports.activeEffect;
17
- exports.activeEffect = effect2;
18
- try {
19
- effect2.state.callback();
20
- } finally {
21
- exports.activeEffect = previousEffect;
22
- }
23
- }
24
- function effect(callback) {
25
- return new Effect(callback);
26
- }
27
- exports.activeEffect = void 0;
28
- exports.Effect = Effect;
29
- exports.effect = effect;
30
- exports.runEffect = runEffect;
@@ -1,40 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- function isArray(value) {
4
- return isMora(value, arrayName);
5
- }
6
- function isComputed(value) {
7
- return isMora(value, computedName);
8
- }
9
- function isEffect(value) {
10
- return isMora(value, effectName);
11
- }
12
- function isMora(value, name) {
13
- return typeof value === "object" && value != null && "$mora" in value && (typeof name === "string" ? value.$mora === name : name.has(value.$mora));
14
- }
15
- function isReactive(value) {
16
- return isMora(value, reactiveNames);
17
- }
18
- function isSignal(value) {
19
- return isMora(value, signalName);
20
- }
21
- function isStore(value) {
22
- return isMora(value, storeName);
23
- }
24
- const arrayName = "array";
25
- const computedName = "computed";
26
- const effectName = "effect";
27
- const signalName = "signal";
28
- const storeName = "store";
29
- const reactiveNames = /* @__PURE__ */ new Set([arrayName, computedName, signalName, storeName]);
30
- exports.arrayName = arrayName;
31
- exports.computedName = computedName;
32
- exports.effectName = effectName;
33
- exports.isArray = isArray;
34
- exports.isComputed = isComputed;
35
- exports.isEffect = isEffect;
36
- exports.isReactive = isReactive;
37
- exports.isSignal = isSignal;
38
- exports.isStore = isStore;
39
- exports.signalName = signalName;
40
- exports.storeName = storeName;
@@ -1,56 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const batch = require("../batch.cjs");
4
- const value_computed = require("../value/computed.cjs");
5
- const helpers_value = require("./value.cjs");
6
- function getReactiveValueInProxy(reactive, mapped, key, isArray) {
7
- let item = mapped.get(key);
8
- if (item == null) {
9
- item = value_computed.computed(() => {
10
- const value = reactive.get();
11
- return isArray ? value.at(key) : value[key];
12
- });
13
- mapped.set(key, item);
14
- }
15
- return item;
16
- }
17
- function setProxyValue(proxy, value) {
18
- batch.startBatch();
19
- const proxyKeys = Object.keys(proxy);
20
- const valueKeys = Object.keys(value);
21
- let { length } = proxyKeys;
22
- for (let index = 0; index < length; index += 1) {
23
- const key = proxyKeys[index];
24
- proxy[key] = valueKeys.includes(key) ? value[key] : void 0;
25
- }
26
- length = valueKeys.length;
27
- for (let index = 0; index < length; index += 1) {
28
- const key = valueKeys[index];
29
- if (!proxyKeys.includes(key)) {
30
- const keyedValue = value[key];
31
- proxy[key] = keyedValue;
32
- }
33
- }
34
- batch.stopBatch();
35
- }
36
- function setValueInProxy(target, property, value, state, isArray, length) {
37
- if (isArray) {
38
- const isIndex = !Number.isNaN(Number(property));
39
- const isLength = property === "length";
40
- if (!isIndex && !isLength) {
41
- return Reflect.set(target, property, value);
42
- }
43
- }
44
- const previous = Reflect.get(target, property);
45
- if (!state.equal(previous, value)) {
46
- Reflect.set(target, property, value);
47
- helpers_value.emitValue(state);
48
- if (isArray) {
49
- length == null ? void 0 : length.set(target.length);
50
- }
51
- }
52
- return true;
53
- }
54
- exports.getReactiveValueInProxy = getReactiveValueInProxy;
55
- exports.setProxyValue = setProxyValue;
56
- exports.setValueInProxy = setValueInProxy;