@oscarpalmer/mora 0.19.0 → 0.21.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.
@@ -1,5 +1,9 @@
1
1
  import {isKey, isPlainObject} from '@oscarpalmer/atoms/is';
2
- import type {Key, PlainObject} from '@oscarpalmer/atoms/models';
2
+ import type {
3
+ GenericCallback,
4
+ Key,
5
+ PlainObject,
6
+ } from '@oscarpalmer/atoms/models';
3
7
  import {storeName} from '../helpers/is';
4
8
  import {
5
9
  getReactiveValueInProxy,
@@ -7,6 +11,7 @@ import {
7
11
  setValueInProxy,
8
12
  } from '../helpers/proxy';
9
13
  import {getValue} from '../helpers/value';
14
+ import {type Unsubscribe, noop, subscribe} from '../subscription';
10
15
  import type {Computed} from './computed';
11
16
  import {Reactive, type ReactiveOptions} from './reactive';
12
17
 
@@ -41,7 +46,7 @@ export class Store<Value extends PlainObject> extends Reactive<Value, Value> {
41
46
 
42
47
  get(key?: unknown): unknown {
43
48
  return isKey(key)
44
- ? getReactiveValueInProxy(this, this.#keyed, key, false)
49
+ ? getReactiveValueInProxy(this, this.#keyed, key, false).get()
45
50
  : getValue(this.state);
46
51
  }
47
52
 
@@ -86,6 +91,48 @@ export class Store<Value extends PlainObject> extends Reactive<Value, Value> {
86
91
  setProxyValue(this.state.value, first ?? {});
87
92
  }
88
93
  }
94
+
95
+ /**
96
+ * @inheritdoc
97
+ */
98
+ subscribe(callback: (value: Value) => void): Unsubscribe;
99
+
100
+ /**
101
+ * Subscribe to changes for a specific key
102
+ */
103
+ subscribe<Key extends keyof Value>(
104
+ key: Key,
105
+ callback: (value: Value[Key] | undefined) => void,
106
+ ): Unsubscribe;
107
+
108
+ /**
109
+ * Subscribe to changes for a specific key
110
+ */
111
+ subscribe(key: Key, callback: (value: unknown) => void): Unsubscribe;
112
+
113
+ subscribe(
114
+ first: Key | GenericCallback,
115
+ second?: GenericCallback,
116
+ ): Unsubscribe {
117
+ if (isKey(first) && typeof second === 'function') {
118
+ return getReactiveValueInProxy(this, this.#keyed, first, false).subscribe(
119
+ second,
120
+ );
121
+ }
122
+
123
+ return typeof first === 'function' ? subscribe(this.state, first) : noop;
124
+ }
125
+
126
+ /**
127
+ * Update the value _(based on the current value)_
128
+ */
129
+ update(callback: (value: Value) => Value): void {
130
+ const updated = callback({...this.state.value});
131
+
132
+ if (updated == null || isPlainObject(updated)) {
133
+ setProxyValue(this.state.value, updated ?? {});
134
+ }
135
+ }
89
136
  }
90
137
 
91
138
  /**
package/types/batch.d.cts CHANGED
@@ -52,13 +52,13 @@ export type ReactiveState<Value, Equal> = {
52
52
  computeds: Set<Computed<unknown>>;
53
53
  effects: Set<Effect>;
54
54
  equal: (first: Equal, second: Equal) => boolean;
55
- subscriptions: Map<(value: Value) => void, Subscription<Value>>;
55
+ subscriptions: Map<GenericCallback, Subscription>;
56
56
  value: Value;
57
57
  };
58
- declare class Subscription<Value> {
59
- state: ReactiveState<Value, never>;
58
+ declare class Subscription {
59
+ state: ReactiveState<unknown, never>;
60
60
  callback: GenericCallback;
61
- constructor(state: ReactiveState<Value, never>, callback: GenericCallback);
61
+ constructor(state: ReactiveState<unknown, never>, callback: GenericCallback);
62
62
  }
63
63
  /**
64
64
  * Unsubscribe from changes
@@ -73,7 +73,7 @@ export declare function startBatch(): void;
73
73
  * Stop batching effects and flush _(run)_ them
74
74
  */
75
75
  export declare function stopBatch(): void;
76
- export declare const batchedHandlers: Set<Effect | Subscription<unknown>>;
76
+ export declare const batchedHandlers: Set<Effect | Subscription>;
77
77
  export declare let batchDepth: number;
78
78
 
79
79
  export {};
package/types/batch.d.ts CHANGED
@@ -9,5 +9,5 @@ export declare function startBatch(): void;
9
9
  * Stop batching effects and flush _(run)_ them
10
10
  */
11
11
  export declare function stopBatch(): void;
12
- export declare const batchedHandlers: Set<Effect | Subscription<unknown>>;
12
+ export declare const batchedHandlers: Set<Subscription | Effect>;
13
13
  export declare let batchDepth: number;
@@ -60,13 +60,13 @@ export type ReactiveState<Value, Equal> = {
60
60
  computeds: Set<Computed<unknown>>;
61
61
  effects: Set<Effect>;
62
62
  equal: (first: Equal, second: Equal) => boolean;
63
- subscriptions: Map<(value: Value) => void, Subscription<Value>>;
63
+ subscriptions: Map<GenericCallback, Subscription>;
64
64
  value: Value;
65
65
  };
66
- declare class Subscription<Value> {
67
- state: ReactiveState<Value, never>;
66
+ declare class Subscription {
67
+ state: ReactiveState<unknown, never>;
68
68
  callback: GenericCallback;
69
- constructor(state: ReactiveState<Value, never>, callback: GenericCallback);
69
+ constructor(state: ReactiveState<unknown, never>, callback: GenericCallback);
70
70
  }
71
71
  /**
72
72
  * Unsubscribe from changes
@@ -91,7 +91,13 @@ declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
91
91
  * @inheritdoc
92
92
  */
93
93
  get(): Item[];
94
+ /**
95
+ * Get the value at an index
96
+ */
94
97
  get(index: number): Item | undefined;
98
+ /**
99
+ * Get the length of the array
100
+ */
95
101
  get(property: "length"): number;
96
102
  /**
97
103
  * Create a computed, filtered array
@@ -105,6 +111,7 @@ declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
105
111
  * @inheritdoc
106
112
  */
107
113
  peek(): Item[];
114
+ peek(index: number): Item | undefined;
108
115
  /**
109
116
  * Get the length of the array _(without reactivity)_
110
117
  */
@@ -120,7 +127,7 @@ declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
120
127
  /**
121
128
  * Set the value
122
129
  */
123
- set(value: Item[]): void;
130
+ set(value?: Item[]): void;
124
131
  /**
125
132
  * Set the value at an index
126
133
  */
@@ -137,10 +144,22 @@ declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
137
144
  * Remove and return items from the array _(and optionally add new items)_
138
145
  */
139
146
  splice(from: number, to?: number, ...items: Item[]): Item[];
147
+ /**
148
+ * @inheritdoc
149
+ */
150
+ subscribe(callback: (value: Item[]) => void): Unsubscribe;
151
+ /**
152
+ * Subscribe to changes at a specific index
153
+ */
154
+ subscribe(index: number, callback: (value: Item | undefined) => void): Unsubscribe;
140
155
  /**
141
156
  * Add items to the beginning of the array
142
157
  */
143
158
  unshift(...items: Item[]): number;
159
+ /**
160
+ * Update the value _(based on the current value)_
161
+ */
162
+ update(callback: (value: Item[]) => Item[]): void;
144
163
  }
145
164
  declare class Signal<Value> extends Reactive<Value> {
146
165
  constructor(value: Value, options?: ReactiveOptions<Value>);
@@ -153,7 +172,7 @@ declare class Signal<Value> extends Reactive<Value> {
153
172
  */
154
173
  set(value: Value): void;
155
174
  /**
156
- * Update the value
175
+ * Update the value _(based on the current value)_
157
176
  */
158
177
  update(callback: (value: Value) => Value): void;
159
178
  }
@@ -196,6 +215,22 @@ declare class Store<Value extends PlainObject> extends Reactive<Value, Value> {
196
215
  * Set a value by key
197
216
  */
198
217
  set(key: Key, value: unknown): void;
218
+ /**
219
+ * @inheritdoc
220
+ */
221
+ subscribe(callback: (value: Value) => void): Unsubscribe;
222
+ /**
223
+ * Subscribe to changes for a specific key
224
+ */
225
+ subscribe<Key extends keyof Value>(key: Key, callback: (value: Value[Key] | undefined) => void): Unsubscribe;
226
+ /**
227
+ * Subscribe to changes for a specific key
228
+ */
229
+ subscribe(key: Key, callback: (value: unknown) => void): Unsubscribe;
230
+ /**
231
+ * Update the value _(based on the current value)_
232
+ */
233
+ update(callback: (value: Value) => Value): void;
199
234
  }
200
235
  /**
201
236
  * Is the value a reactive array?
@@ -64,13 +64,13 @@ export type ReactiveState<Value, Equal> = {
64
64
  computeds: Set<Computed<unknown>>;
65
65
  effects: Set<Effect>;
66
66
  equal: (first: Equal, second: Equal) => boolean;
67
- subscriptions: Map<(value: Value) => void, Subscription<Value>>;
67
+ subscriptions: Map<GenericCallback, Subscription>;
68
68
  value: Value;
69
69
  };
70
- declare class Subscription<Value> {
71
- state: ReactiveState<Value, never>;
70
+ declare class Subscription {
71
+ state: ReactiveState<unknown, never>;
72
72
  callback: GenericCallback;
73
- constructor(state: ReactiveState<Value, never>, callback: GenericCallback);
73
+ constructor(state: ReactiveState<unknown, never>, callback: GenericCallback);
74
74
  }
75
75
  /**
76
76
  * Unsubscribe from changes
@@ -95,7 +95,13 @@ declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
95
95
  * @inheritdoc
96
96
  */
97
97
  get(): Item[];
98
+ /**
99
+ * Get the value at an index
100
+ */
98
101
  get(index: number): Item | undefined;
102
+ /**
103
+ * Get the length of the array
104
+ */
99
105
  get(property: "length"): number;
100
106
  /**
101
107
  * Create a computed, filtered array
@@ -109,6 +115,7 @@ declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
109
115
  * @inheritdoc
110
116
  */
111
117
  peek(): Item[];
118
+ peek(index: number): Item | undefined;
112
119
  /**
113
120
  * Get the length of the array _(without reactivity)_
114
121
  */
@@ -124,7 +131,7 @@ declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
124
131
  /**
125
132
  * Set the value
126
133
  */
127
- set(value: Item[]): void;
134
+ set(value?: Item[]): void;
128
135
  /**
129
136
  * Set the value at an index
130
137
  */
@@ -141,10 +148,22 @@ declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
141
148
  * Remove and return items from the array _(and optionally add new items)_
142
149
  */
143
150
  splice(from: number, to?: number, ...items: Item[]): Item[];
151
+ /**
152
+ * @inheritdoc
153
+ */
154
+ subscribe(callback: (value: Item[]) => void): Unsubscribe;
155
+ /**
156
+ * Subscribe to changes at a specific index
157
+ */
158
+ subscribe(index: number, callback: (value: Item | undefined) => void): Unsubscribe;
144
159
  /**
145
160
  * Add items to the beginning of the array
146
161
  */
147
162
  unshift(...items: Item[]): number;
163
+ /**
164
+ * Update the value _(based on the current value)_
165
+ */
166
+ update(callback: (value: Item[]) => Item[]): void;
148
167
  }
149
168
  declare class Signal<Value> extends Reactive<Value> {
150
169
  constructor(value: Value, options?: ReactiveOptions<Value>);
@@ -157,7 +176,7 @@ declare class Signal<Value> extends Reactive<Value> {
157
176
  */
158
177
  set(value: Value): void;
159
178
  /**
160
- * Update the value
179
+ * Update the value _(based on the current value)_
161
180
  */
162
181
  update(callback: (value: Value) => Value): void;
163
182
  }
@@ -200,9 +219,25 @@ declare class Store<Value extends PlainObject> extends Reactive<Value, Value> {
200
219
  * Set a value by key
201
220
  */
202
221
  set(key: Key, value: unknown): void;
222
+ /**
223
+ * @inheritdoc
224
+ */
225
+ subscribe(callback: (value: Value) => void): Unsubscribe;
226
+ /**
227
+ * Subscribe to changes for a specific key
228
+ */
229
+ subscribe<Key extends keyof Value>(key: Key, callback: (value: Value[Key] | undefined) => void): Unsubscribe;
230
+ /**
231
+ * Subscribe to changes for a specific key
232
+ */
233
+ subscribe(key: Key, callback: (value: unknown) => void): Unsubscribe;
234
+ /**
235
+ * Update the value _(based on the current value)_
236
+ */
237
+ update(callback: (value: Value) => Value): void;
203
238
  }
204
- export declare function getReactiveValueInProxy<Value>(array: ReactiveArray<Value>, mapped: Map<Key, Computed<unknown>>, index: number, isArray: true): unknown;
205
- export declare function getReactiveValueInProxy<Value extends PlainObject>(store: Store<Value>, mapped: Map<Key, Computed<unknown>>, key: Key, isArray: false): unknown;
239
+ export declare function getReactiveValueInProxy<Value>(array: ReactiveArray<Value>, mapped: Map<Key, Computed<unknown>>, index: number, isArray: true): Computed<unknown>;
240
+ export declare function getReactiveValueInProxy<Value extends PlainObject>(store: Store<Value>, mapped: Map<Key, Computed<unknown>>, key: Key, isArray: false): Computed<unknown>;
206
241
  export declare function setProxyValue(proxy: ArrayOrPlainObject, value: ArrayOrPlainObject): void;
207
242
  export declare function setValueInProxy<Value extends ArrayOrPlainObject, Equal>(target: Value, property: PropertyKey, value: unknown, state: ReactiveState<Value, Equal>, isArray: boolean, length?: Signal<number>): boolean;
208
243
 
@@ -4,7 +4,7 @@ import { type Computed } from '../value/computed';
4
4
  import type { ReactiveState } from '../value/reactive';
5
5
  import type { Signal } from '../value/signal';
6
6
  import type { Store } from '../value/store';
7
- export declare function getReactiveValueInProxy<Value>(array: ReactiveArray<Value>, mapped: Map<Key, Computed<unknown>>, index: number, isArray: true): unknown;
8
- export declare function getReactiveValueInProxy<Value extends PlainObject>(store: Store<Value>, mapped: Map<Key, Computed<unknown>>, key: Key, isArray: false): unknown;
7
+ export declare function getReactiveValueInProxy<Value>(array: ReactiveArray<Value>, mapped: Map<Key, Computed<unknown>>, index: number, isArray: true): Computed<unknown>;
8
+ export declare function getReactiveValueInProxy<Value extends PlainObject>(store: Store<Value>, mapped: Map<Key, Computed<unknown>>, key: Key, isArray: false): Computed<unknown>;
9
9
  export declare function setProxyValue(proxy: ArrayOrPlainObject, value: ArrayOrPlainObject): void;
10
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;
@@ -52,13 +52,13 @@ export type ReactiveState<Value, Equal> = {
52
52
  computeds: Set<Computed<unknown>>;
53
53
  effects: Set<Effect>;
54
54
  equal: (first: Equal, second: Equal) => boolean;
55
- subscriptions: Map<(value: Value) => void, Subscription<Value>>;
55
+ subscriptions: Map<GenericCallback, Subscription>;
56
56
  value: Value;
57
57
  };
58
- declare class Subscription<Value> {
59
- state: ReactiveState<Value, never>;
58
+ declare class Subscription {
59
+ state: ReactiveState<unknown, never>;
60
60
  callback: GenericCallback;
61
- constructor(state: ReactiveState<Value, never>, callback: GenericCallback);
61
+ constructor(state: ReactiveState<unknown, never>, callback: GenericCallback);
62
62
  }
63
63
  /**
64
64
  * Unsubscribe from changes
package/types/index.d.cts CHANGED
@@ -68,13 +68,13 @@ export type ReactiveState<Value, Equal> = {
68
68
  computeds: Set<Computed<unknown>>;
69
69
  effects: Set<Effect>;
70
70
  equal: (first: Equal, second: Equal) => boolean;
71
- subscriptions: Map<(value: Value) => void, Subscription<Value>>;
71
+ subscriptions: Map<GenericCallback, Subscription>;
72
72
  value: Value;
73
73
  };
74
- declare class Subscription<Value> {
75
- state: ReactiveState<Value, never>;
74
+ declare class Subscription {
75
+ state: ReactiveState<unknown, never>;
76
76
  callback: GenericCallback;
77
- constructor(state: ReactiveState<Value, never>, callback: GenericCallback);
77
+ constructor(state: ReactiveState<unknown, never>, callback: GenericCallback);
78
78
  }
79
79
  /**
80
80
  * Unsubscribe from changes
@@ -107,7 +107,13 @@ export declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
107
107
  * @inheritdoc
108
108
  */
109
109
  get(): Item[];
110
+ /**
111
+ * Get the value at an index
112
+ */
110
113
  get(index: number): Item | undefined;
114
+ /**
115
+ * Get the length of the array
116
+ */
111
117
  get(property: "length"): number;
112
118
  /**
113
119
  * Create a computed, filtered array
@@ -121,6 +127,7 @@ export declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
121
127
  * @inheritdoc
122
128
  */
123
129
  peek(): Item[];
130
+ peek(index: number): Item | undefined;
124
131
  /**
125
132
  * Get the length of the array _(without reactivity)_
126
133
  */
@@ -136,7 +143,7 @@ export declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
136
143
  /**
137
144
  * Set the value
138
145
  */
139
- set(value: Item[]): void;
146
+ set(value?: Item[]): void;
140
147
  /**
141
148
  * Set the value at an index
142
149
  */
@@ -153,10 +160,22 @@ export declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
153
160
  * Remove and return items from the array _(and optionally add new items)_
154
161
  */
155
162
  splice(from: number, to?: number, ...items: Item[]): Item[];
163
+ /**
164
+ * @inheritdoc
165
+ */
166
+ subscribe(callback: (value: Item[]) => void): Unsubscribe;
167
+ /**
168
+ * Subscribe to changes at a specific index
169
+ */
170
+ subscribe(index: number, callback: (value: Item | undefined) => void): Unsubscribe;
156
171
  /**
157
172
  * Add items to the beginning of the array
158
173
  */
159
174
  unshift(...items: Item[]): number;
175
+ /**
176
+ * Update the value _(based on the current value)_
177
+ */
178
+ update(callback: (value: Item[]) => Item[]): void;
160
179
  }
161
180
  /**
162
181
  * Create a reactive array
@@ -173,7 +192,7 @@ export declare class Signal<Value> extends Reactive<Value> {
173
192
  */
174
193
  set(value: Value): void;
175
194
  /**
176
- * Update the value
195
+ * Update the value _(based on the current value)_
177
196
  */
178
197
  update(callback: (value: Value) => Value): void;
179
198
  }
@@ -220,6 +239,22 @@ export declare class Store<Value extends PlainObject> extends Reactive<Value, Va
220
239
  * Set a value by key
221
240
  */
222
241
  set(key: Key, value: unknown): void;
242
+ /**
243
+ * @inheritdoc
244
+ */
245
+ subscribe(callback: (value: Value) => void): Unsubscribe;
246
+ /**
247
+ * Subscribe to changes for a specific key
248
+ */
249
+ subscribe<Key extends keyof Value>(key: Key, callback: (value: Value[Key] | undefined) => void): Unsubscribe;
250
+ /**
251
+ * Subscribe to changes for a specific key
252
+ */
253
+ subscribe(key: Key, callback: (value: unknown) => void): Unsubscribe;
254
+ /**
255
+ * Update the value _(based on the current value)_
256
+ */
257
+ update(callback: (value: Value) => Value): void;
223
258
  }
224
259
  /**
225
260
  * Create a reactive store
@@ -52,13 +52,13 @@ export type ReactiveState<Value, Equal> = {
52
52
  computeds: Set<Computed<unknown>>;
53
53
  effects: Set<Effect>;
54
54
  equal: (first: Equal, second: Equal) => boolean;
55
- subscriptions: Map<(value: Value) => void, Subscription<Value>>;
55
+ subscriptions: Map<GenericCallback, Subscription>;
56
56
  value: Value;
57
57
  };
58
- export declare class Subscription<Value> {
59
- state: ReactiveState<Value, never>;
58
+ export declare class Subscription {
59
+ state: ReactiveState<unknown, never>;
60
60
  callback: GenericCallback;
61
- constructor(state: ReactiveState<Value, never>, callback: GenericCallback);
61
+ constructor(state: ReactiveState<unknown, never>, callback: GenericCallback);
62
62
  }
63
63
  /**
64
64
  * Unsubscribe from changes
@@ -1,9 +1,9 @@
1
1
  import type { GenericCallback } from '@oscarpalmer/atoms/models';
2
2
  import type { ReactiveState } from './value/reactive';
3
- export declare class Subscription<Value> {
4
- state: ReactiveState<Value, never>;
3
+ export declare class Subscription {
4
+ state: ReactiveState<unknown, never>;
5
5
  callback: GenericCallback;
6
- constructor(state: ReactiveState<Value, never>, callback: GenericCallback);
6
+ constructor(state: ReactiveState<unknown, never>, callback: GenericCallback);
7
7
  }
8
8
  /**
9
9
  * Unsubscribe from changes
@@ -52,13 +52,13 @@ export type ReactiveState<Value, Equal> = {
52
52
  computeds: Set<Computed<unknown>>;
53
53
  effects: Set<Effect>;
54
54
  equal: (first: Equal, second: Equal) => boolean;
55
- subscriptions: Map<(value: Value) => void, Subscription<Value>>;
55
+ subscriptions: Map<GenericCallback, Subscription>;
56
56
  value: Value;
57
57
  };
58
- declare class Subscription<Value> {
59
- state: ReactiveState<Value, never>;
58
+ declare class Subscription {
59
+ state: ReactiveState<unknown, never>;
60
60
  callback: GenericCallback;
61
- constructor(state: ReactiveState<Value, never>, callback: GenericCallback);
61
+ constructor(state: ReactiveState<unknown, never>, callback: GenericCallback);
62
62
  }
63
63
  /**
64
64
  * Unsubscribe from changes
@@ -83,7 +83,13 @@ export declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
83
83
  * @inheritdoc
84
84
  */
85
85
  get(): Item[];
86
+ /**
87
+ * Get the value at an index
88
+ */
86
89
  get(index: number): Item | undefined;
90
+ /**
91
+ * Get the length of the array
92
+ */
87
93
  get(property: "length"): number;
88
94
  /**
89
95
  * Create a computed, filtered array
@@ -97,6 +103,7 @@ export declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
97
103
  * @inheritdoc
98
104
  */
99
105
  peek(): Item[];
106
+ peek(index: number): Item | undefined;
100
107
  /**
101
108
  * Get the length of the array _(without reactivity)_
102
109
  */
@@ -112,7 +119,7 @@ export declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
112
119
  /**
113
120
  * Set the value
114
121
  */
115
- set(value: Item[]): void;
122
+ set(value?: Item[]): void;
116
123
  /**
117
124
  * Set the value at an index
118
125
  */
@@ -129,10 +136,22 @@ export declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
129
136
  * Remove and return items from the array _(and optionally add new items)_
130
137
  */
131
138
  splice(from: number, to?: number, ...items: Item[]): Item[];
139
+ /**
140
+ * @inheritdoc
141
+ */
142
+ subscribe(callback: (value: Item[]) => void): Unsubscribe;
143
+ /**
144
+ * Subscribe to changes at a specific index
145
+ */
146
+ subscribe(index: number, callback: (value: Item | undefined) => void): Unsubscribe;
132
147
  /**
133
148
  * Add items to the beginning of the array
134
149
  */
135
150
  unshift(...items: Item[]): number;
151
+ /**
152
+ * Update the value _(based on the current value)_
153
+ */
154
+ update(callback: (value: Item[]) => Item[]): void;
136
155
  }
137
156
  /**
138
157
  * Create a reactive array
@@ -1,3 +1,4 @@
1
+ import { type Unsubscribe } from '../subscription';
1
2
  import { type Computed } from './computed';
2
3
  import { Reactive, type ReactiveOptions } from './reactive';
3
4
  export declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
@@ -19,7 +20,13 @@ export declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
19
20
  * @inheritdoc
20
21
  */
21
22
  get(): Item[];
23
+ /**
24
+ * Get the value at an index
25
+ */
22
26
  get(index: number): Item | undefined;
27
+ /**
28
+ * Get the length of the array
29
+ */
23
30
  get(property: 'length'): number;
24
31
  /**
25
32
  * Create a computed, filtered array
@@ -33,6 +40,7 @@ export declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
33
40
  * @inheritdoc
34
41
  */
35
42
  peek(): Item[];
43
+ peek(index: number): Item | undefined;
36
44
  /**
37
45
  * Get the length of the array _(without reactivity)_
38
46
  */
@@ -48,7 +56,7 @@ export declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
48
56
  /**
49
57
  * Set the value
50
58
  */
51
- set(value: Item[]): void;
59
+ set(value?: Item[]): void;
52
60
  /**
53
61
  * Set the value at an index
54
62
  */
@@ -65,10 +73,22 @@ export declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
65
73
  * Remove and return items from the array _(and optionally add new items)_
66
74
  */
67
75
  splice(from: number, to?: number, ...items: Item[]): Item[];
76
+ /**
77
+ * @inheritdoc
78
+ */
79
+ subscribe(callback: (value: Item[]) => void): Unsubscribe;
80
+ /**
81
+ * Subscribe to changes at a specific index
82
+ */
83
+ subscribe(index: number, callback: (value: Item | undefined) => void): Unsubscribe;
68
84
  /**
69
85
  * Add items to the beginning of the array
70
86
  */
71
87
  unshift(...items: Item[]): number;
88
+ /**
89
+ * Update the value _(based on the current value)_
90
+ */
91
+ update(callback: (value: Item[]) => Item[]): void;
72
92
  }
73
93
  /**
74
94
  * Create a reactive array
@@ -65,13 +65,13 @@ export type ReactiveState<Value, Equal> = {
65
65
  computeds: Set<Computed<unknown>>;
66
66
  effects: Set<Effect>;
67
67
  equal: (first: Equal, second: Equal) => boolean;
68
- subscriptions: Map<(value: Value) => void, Subscription<Value>>;
68
+ subscriptions: Map<GenericCallback, Subscription>;
69
69
  value: Value;
70
70
  };
71
- declare class Subscription<Value> {
72
- state: ReactiveState<Value, never>;
71
+ declare class Subscription {
72
+ state: ReactiveState<unknown, never>;
73
73
  callback: GenericCallback;
74
- constructor(state: ReactiveState<Value, never>, callback: GenericCallback);
74
+ constructor(state: ReactiveState<unknown, never>, callback: GenericCallback);
75
75
  }
76
76
  /**
77
77
  * Unsubscribe from changes
@@ -52,13 +52,13 @@ export type ReactiveState<Value, Equal> = {
52
52
  computeds: Set<Computed<unknown>>;
53
53
  effects: Set<Effect>;
54
54
  equal: (first: Equal, second: Equal) => boolean;
55
- subscriptions: Map<(value: Value) => void, Subscription<Value>>;
55
+ subscriptions: Map<GenericCallback, Subscription>;
56
56
  value: Value;
57
57
  };
58
- declare class Subscription<Value> {
59
- state: ReactiveState<Value, never>;
58
+ declare class Subscription {
59
+ state: ReactiveState<unknown, never>;
60
60
  callback: GenericCallback;
61
- constructor(state: ReactiveState<Value, never>, callback: GenericCallback);
61
+ constructor(state: ReactiveState<unknown, never>, callback: GenericCallback);
62
62
  }
63
63
  /**
64
64
  * Unsubscribe from changes
@@ -1,3 +1,4 @@
1
+ import type { GenericCallback } from '@oscarpalmer/atoms/models';
1
2
  import type { Effect } from '../effect';
2
3
  import { type Subscription, type Unsubscribe } from '../subscription';
3
4
  import type { Computed } from './computed';
@@ -39,6 +40,6 @@ export type ReactiveState<Value, Equal> = {
39
40
  computeds: Set<Computed<unknown>>;
40
41
  effects: Set<Effect>;
41
42
  equal: (first: Equal, second: Equal) => boolean;
42
- subscriptions: Map<(value: Value) => void, Subscription<Value>>;
43
+ subscriptions: Map<GenericCallback, Subscription>;
43
44
  value: Value;
44
45
  };