@oscarpalmer/mora 0.23.0 → 0.25.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,12 +1,9 @@
1
1
  import {isKey, isPlainObject} from '@oscarpalmer/atoms/is';
2
- import type {
3
- GenericCallback,
4
- Key,
5
- PlainObject,
6
- } from '@oscarpalmer/atoms/models';
2
+ import type {GenericCallback, Key, PlainObject} from '@oscarpalmer/atoms/models';
7
3
  import {NAME_STORE} from '../constants';
8
4
  import {
9
5
  getReactiveValueInProxy,
6
+ emityProxyValues,
10
7
  setProxyValue,
11
8
  setValueInProxy,
12
9
  } from '../helpers/proxy';
@@ -46,7 +43,7 @@ export class Store<Value extends PlainObject> extends Reactive<Value, Value> {
46
43
  * @param key Key of the value to get
47
44
  * @returns Value for the specified key, or `undefined` if it doesn't exist
48
45
  */
49
- get(key: keyof Value): Value[keyof Value];
46
+ get<Key extends keyof Value>(key: Key): Value[Key];
50
47
 
51
48
  /**
52
49
  * Get a value by key
@@ -61,27 +58,37 @@ export class Store<Value extends PlainObject> extends Reactive<Value, Value> {
61
58
  : getValue(this.state);
62
59
  }
63
60
 
61
+ /**
62
+ * Notify dependents of changes
63
+ *
64
+ * _This bypasses equality checks and will immediately notify dependents.
65
+ * Use this only if you're modifying nested data that would be ignored by equality checks._
66
+ */
67
+ notify(): void {
68
+ emityProxyValues(this.state, this.#keyed);
69
+ }
70
+
64
71
  /**
65
72
  * Get the value _(without reactivity)_
66
73
  * @returns The current value
67
74
  */
68
- peek(): Value;
75
+ override peek(): Value;
69
76
 
70
77
  /**
71
78
  * Get a value by key _(without reactivity)_
72
79
  * @param key Key of the value to get
73
80
  * @returns Value for the specified key, or `undefined` if it doesn't exist
74
81
  */
75
- peek(key: keyof Value): Value[keyof Value];
82
+ override peek<Key extends keyof Value>(key: Key): Value[Key];
76
83
 
77
84
  /**
78
85
  * Get a value by key _(without reactivity)_
79
86
  * @param key Key of the value to get
80
87
  * @returns Value for the specified key, or `undefined` if it doesn't exist
81
88
  */
82
- peek(key: Key): unknown;
89
+ override peek(key: Key): unknown;
83
90
 
84
- peek(key?: unknown): unknown {
91
+ override peek(key?: unknown): unknown {
85
92
  return isKey(key) ? this.state.value[key] : {...this.state.value};
86
93
  }
87
94
 
@@ -96,7 +103,7 @@ export class Store<Value extends PlainObject> extends Reactive<Value, Value> {
96
103
  * @param key Key of the value to set
97
104
  * @param value New value
98
105
  */
99
- set(key: keyof Value, value: Value[keyof Value]): void;
106
+ set<Key extends keyof Value>(key: Key, value: Value[Key]): void;
100
107
 
101
108
  /**
102
109
  * Set a value by key
@@ -116,7 +123,7 @@ export class Store<Value extends PlainObject> extends Reactive<Value, Value> {
116
123
  /**
117
124
  * @inheritdoc
118
125
  */
119
- subscribe(callback: (value: Value) => void): Unsubscribe;
126
+ override subscribe(callback: (value: Value) => void): Unsubscribe;
120
127
 
121
128
  /**
122
129
  * Subscribe to changes for a specific key
@@ -124,7 +131,7 @@ export class Store<Value extends PlainObject> extends Reactive<Value, Value> {
124
131
  * @param callback Callback for changes
125
132
  * @returns Unsubscribe callback
126
133
  */
127
- subscribe<Key extends keyof Value>(
134
+ override subscribe<Key extends keyof Value>(
128
135
  key: Key,
129
136
  callback: (value: Value[Key] | undefined) => void,
130
137
  ): Unsubscribe;
@@ -135,16 +142,11 @@ export class Store<Value extends PlainObject> extends Reactive<Value, Value> {
135
142
  * @param callback Callback for changes
136
143
  * @returns Unsubscribe callback
137
144
  */
138
- subscribe(key: Key, callback: (value: unknown) => void): Unsubscribe;
145
+ override subscribe(key: Key, callback: (value: unknown) => void): Unsubscribe;
139
146
 
140
- subscribe(
141
- first: Key | GenericCallback,
142
- second?: GenericCallback,
143
- ): Unsubscribe {
147
+ override subscribe(first: Key | GenericCallback, second?: GenericCallback): Unsubscribe {
144
148
  if (isKey(first) && typeof second === 'function') {
145
- return getReactiveValueInProxy(this, this.#keyed, first, false).subscribe(
146
- second,
147
- );
149
+ return getReactiveValueInProxy(this, this.#keyed, first, false).subscribe(second);
148
150
  }
149
151
 
150
152
  return typeof first === 'function' ? subscribe(this.state, first) : noop;
@@ -166,7 +168,7 @@ export class Store<Value extends PlainObject> extends Reactive<Value, Value> {
166
168
  /**
167
169
  * Create a reactive store
168
170
  * @param value Initial object value
169
- * @param options Optional reactivity options
171
+ * @param options Reactivity options
170
172
  * @returns Reactive store
171
173
  */
172
174
  export function store<Value extends PlainObject>(
@@ -9,6 +9,8 @@ export declare const METHODS_UPDATE: Set<string>;
9
9
  export declare const NAME_ARRAY = "array";
10
10
  export declare const NAME_COMPUTED = "computed";
11
11
  export declare const NAME_EFFECT = "effect";
12
+ export declare const NAME_MORA = "$mora";
12
13
  export declare const NAME_SIGNAL = "signal";
13
14
  export declare const NAME_STORE = "store";
14
- export declare const NAMES: Set<string>;
15
+ export declare const NAME_ALL: Set<string>;
16
+ export declare const PROPERTY_LENGTH = "length";
@@ -1,8 +1,10 @@
1
1
  import type { ArrayOrPlainObject, Key, PlainObject } from '@oscarpalmer/atoms/models';
2
- import type { SetValueInProxyParameters } from '../models';
2
+ import type { ReactiveState, SetValueInProxyParameters } from '../models';
3
3
  import type { ReactiveArray } from '../value/array';
4
4
  import { type Computed } from '../value/computed';
5
5
  import type { Store } from '../value/store';
6
+ export declare function emityProxyValues<Value>(state: ReactiveState<Value, Value>, mapped: Map<Key, Computed<unknown>>): void;
7
+ export declare function emityProxyValues<Value>(state: ReactiveState<Value[], Value>, mapped: Map<Key, Computed<unknown>>): void;
6
8
  export declare function getReactiveValueInProxy<Value>(array: ReactiveArray<Value>, mapped: Map<Key, Computed<unknown>>, index: number, isArray: true): Computed<unknown>;
7
9
  export declare function getReactiveValueInProxy<Value extends PlainObject>(store: Store<Value>, mapped: Map<Key, Computed<unknown>>, key: Key, isArray: false): Computed<unknown>;
8
10
  export declare function setProxyValue(proxy: ArrayOrPlainObject, value: ArrayOrPlainObject): void;
package/types/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export { startBatch, stopBatch } from './batch';
2
2
  export { effect, type Effect } from './effect';
3
- export { isArray, isComputed, isEffect, isReactive, isSignal, } from './helpers/is';
3
+ export { isArray, isComputed, isEffect, isReactive, isSignal } from './helpers/is';
4
4
  export type { Unsubscribe } from './models';
5
5
  export { array, type ReactiveArray } from './value/array';
6
6
  export { computed, type Computed } from './value/computed';
@@ -131,7 +131,7 @@ export declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
131
131
  /**
132
132
  * Create a reactive array
133
133
  * @param value Initial array of items
134
- * @param options Optional reactivity options
134
+ * @param options Reactivity options
135
135
  * @returns Reactive array
136
136
  */
137
137
  export declare function array<Item>(value: Item[], options?: ReactiveOptions<Item>): ReactiveArray<Item>;
@@ -10,5 +10,8 @@ export declare class Computed<Value> extends Reactive<Value> {
10
10
  }
11
11
  /**
12
12
  * Create a computed value
13
+ * @param callback Callback to compute the value
14
+ * @param options Reactivity options
15
+ * @returns Computed value
13
16
  */
14
17
  export declare function computed<Value>(callback: () => Value, options?: ReactiveOptions<Value>): Computed<Value>;
@@ -20,7 +20,7 @@ export declare class Signal<Value> extends Reactive<Value> {
20
20
  /**
21
21
  * Create a reactive value
22
22
  * @param value Initial value
23
- * @param options Optional reactivity options
23
+ * @param options Reactivity options
24
24
  * @returns Reactive value
25
25
  */
26
26
  export declare function signal<Value>(value: Value, options?: ReactiveOptions<Value>): Signal<Value>;
@@ -13,13 +13,20 @@ export declare class Store<Value extends PlainObject> extends Reactive<Value, Va
13
13
  * @param key Key of the value to get
14
14
  * @returns Value for the specified key, or `undefined` if it doesn't exist
15
15
  */
16
- get(key: keyof Value): Value[keyof Value];
16
+ get<Key extends keyof Value>(key: Key): Value[Key];
17
17
  /**
18
18
  * Get a value by key
19
19
  * @param key Key of the value to get
20
20
  * @returns Value for the specified key, or `undefined` if it doesn't exist
21
21
  */
22
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;
23
30
  /**
24
31
  * Get the value _(without reactivity)_
25
32
  * @returns The current value
@@ -30,7 +37,7 @@ export declare class Store<Value extends PlainObject> extends Reactive<Value, Va
30
37
  * @param key Key of the value to get
31
38
  * @returns Value for the specified key, or `undefined` if it doesn't exist
32
39
  */
33
- peek(key: keyof Value): Value[keyof Value];
40
+ peek<Key extends keyof Value>(key: Key): Value[Key];
34
41
  /**
35
42
  * Get a value by key _(without reactivity)_
36
43
  * @param key Key of the value to get
@@ -47,7 +54,7 @@ export declare class Store<Value extends PlainObject> extends Reactive<Value, Va
47
54
  * @param key Key of the value to set
48
55
  * @param value New value
49
56
  */
50
- set(key: keyof Value, value: Value[keyof Value]): void;
57
+ set<Key extends keyof Value>(key: Key, value: Value[Key]): void;
51
58
  /**
52
59
  * Set a value by key
53
60
  * @param key Key of the value to set
@@ -81,7 +88,7 @@ export declare class Store<Value extends PlainObject> extends Reactive<Value, Va
81
88
  /**
82
89
  * Create a reactive store
83
90
  * @param value Initial object value
84
- * @param options Optional reactivity options
91
+ * @param options Reactivity options
85
92
  * @returns Reactive store
86
93
  */
87
94
  export declare function store<Value extends PlainObject>(value: Value, options?: ReactiveOptions<Value>): Store<Value>;