@oscarpalmer/mora 0.22.0 → 0.24.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 (44) hide show
  1. package/dist/batch.js +8 -9
  2. package/dist/constants.js +36 -0
  3. package/dist/effect.js +7 -8
  4. package/dist/helpers/is.js +9 -19
  5. package/dist/helpers/proxy.js +12 -7
  6. package/dist/helpers/value.js +7 -8
  7. package/dist/index.js +1 -1
  8. package/dist/models.js +0 -0
  9. package/dist/mora.full.js +248 -148
  10. package/dist/subscription.js +7 -5
  11. package/dist/value/array.js +27 -29
  12. package/dist/value/computed.js +18 -21
  13. package/dist/value/reactive.js +2 -1
  14. package/dist/value/signal.js +2 -2
  15. package/dist/value/store.js +12 -3
  16. package/package.json +9 -11
  17. package/src/batch.ts +11 -13
  18. package/src/constants.ts +53 -0
  19. package/src/effect.ts +12 -17
  20. package/src/helpers/is.ts +45 -22
  21. package/src/helpers/proxy.ts +62 -39
  22. package/src/helpers/value.ts +19 -14
  23. package/src/index.ts +1 -0
  24. package/src/models.ts +66 -0
  25. package/src/subscription.ts +14 -16
  26. package/src/value/array.ts +92 -55
  27. package/src/value/computed.ts +29 -39
  28. package/src/value/reactive.ts +11 -25
  29. package/src/value/signal.ts +9 -3
  30. package/src/value/store.ts +55 -13
  31. package/types/batch.d.ts +3 -5
  32. package/types/constants.d.ts +16 -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 +4 -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 +38 -7
@@ -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,56 @@ 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
- get(key: keyof Value): Value[keyof Value];
16
+ get<Key extends keyof Value>(key: Key): Value[Key];
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;
23
+ /**
24
+ * Notify dependents of changes
25
+ *
26
+ * _This bypasses equality checks and will immediately notify dependents.
27
+ * Use this only if you're modifying nested data that would be ignored by equality checks._
28
+ */
29
+ notify(): void;
19
30
  /**
20
31
  * Get the value _(without reactivity)_
32
+ * @returns The current value
21
33
  */
22
34
  peek(): Value;
23
35
  /**
24
36
  * Get a value by key _(without reactivity)_
37
+ * @param key Key of the value to get
38
+ * @returns Value for the specified key, or `undefined` if it doesn't exist
25
39
  */
26
- peek(key: keyof Value): Value[keyof Value];
40
+ peek<Key extends keyof Value>(key: Key): Value[Key];
27
41
  /**
28
42
  * Get a value by key _(without reactivity)_
43
+ * @param key Key of the value to get
44
+ * @returns Value for the specified key, or `undefined` if it doesn't exist
29
45
  */
30
46
  peek(key: Key): unknown;
31
47
  /**
32
48
  * Set the value
49
+ * @param value New value _(defaults to an empty object)_
33
50
  */
34
51
  set(value?: Value): void;
35
52
  /**
36
53
  * Set a value by key
54
+ * @param key Key of the value to set
55
+ * @param value New value
37
56
  */
38
- set(key: keyof Value, value: Value[keyof Value]): void;
57
+ set<Key extends keyof Value>(key: Key, value: Value[Key]): void;
39
58
  /**
40
59
  * Set a value by key
60
+ * @param key Key of the value to set
61
+ * @param value New value
41
62
  */
42
63
  set(key: Key, value: unknown): void;
43
64
  /**
@@ -46,18 +67,28 @@ export declare class Store<Value extends PlainObject> extends Reactive<Value, Va
46
67
  subscribe(callback: (value: Value) => void): Unsubscribe;
47
68
  /**
48
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
49
73
  */
50
74
  subscribe<Key extends keyof Value>(key: Key, callback: (value: Value[Key] | undefined) => void): Unsubscribe;
51
75
  /**
52
76
  * Subscribe to changes for a specific key
77
+ * @param key Key of the value to subscribe to
78
+ * @param callback Callback for changes
79
+ * @returns Unsubscribe callback
53
80
  */
54
81
  subscribe(key: Key, callback: (value: unknown) => void): Unsubscribe;
55
82
  /**
56
83
  * Update the value _(based on the current value)_
84
+ * @param callback Callback to update the value
57
85
  */
58
86
  update(callback: (value: Value) => Value): void;
59
87
  }
60
88
  /**
61
89
  * Create a reactive store
90
+ * @param value Initial object value
91
+ * @param options Optional reactivity options
92
+ * @returns Reactive store
62
93
  */
63
94
  export declare function store<Value extends PlainObject>(value: Value, options?: ReactiveOptions<Value>): Store<Value>;