pocket-state 0.1.8 → 0.1.10
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.
- package/package.json +1 -1
- package/src/globalState/store.ts +29 -39
package/package.json
CHANGED
package/src/globalState/store.ts
CHANGED
|
@@ -10,18 +10,17 @@ export function createStore<T>(
|
|
|
10
10
|
equalityFn?: (a: any, b: any) => boolean,
|
|
11
11
|
): Store<T> {
|
|
12
12
|
const emitter: IEventEmitter = new EventEmitter();
|
|
13
|
-
|
|
13
|
+
const _initialState = Array.isArray(initialState)
|
|
14
|
+
? (initialState as any).slice()
|
|
15
|
+
: initialState && typeof initialState === 'object'
|
|
16
|
+
? {...initialState}
|
|
17
|
+
: initialState;
|
|
18
|
+
let state = _initialState;
|
|
14
19
|
|
|
15
20
|
const areEqual = equalityFn ?? shallow;
|
|
16
21
|
|
|
17
|
-
let emitScheduled = false;
|
|
18
22
|
const emitState = () => {
|
|
19
|
-
|
|
20
|
-
emitScheduled = true;
|
|
21
|
-
queueMicrotask(() => {
|
|
22
|
-
emitScheduled = false;
|
|
23
|
-
emitter.emit('state', state);
|
|
24
|
-
});
|
|
23
|
+
emitter.emit('state', state);
|
|
25
24
|
};
|
|
26
25
|
|
|
27
26
|
function baseSet(delta: Partial<T>) {
|
|
@@ -31,13 +30,6 @@ export function createStore<T>(
|
|
|
31
30
|
|
|
32
31
|
if (!areEqual(state, nextState)) {
|
|
33
32
|
state = nextState;
|
|
34
|
-
|
|
35
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
36
|
-
try {
|
|
37
|
-
Object.freeze(state as any);
|
|
38
|
-
} catch {}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
33
|
emitState();
|
|
42
34
|
}
|
|
43
35
|
}
|
|
@@ -47,14 +39,9 @@ export function createStore<T>(
|
|
|
47
39
|
baseSet as (patch: Partial<T>) => void,
|
|
48
40
|
);
|
|
49
41
|
|
|
50
|
-
const getValue = ((key?: keyof T
|
|
42
|
+
const getValue = ((key?: keyof T) => {
|
|
51
43
|
if (key === undefined) return state;
|
|
52
|
-
|
|
53
|
-
const out = {} as Pick<T, (typeof key)[number]>;
|
|
54
|
-
for (const k of key) (out as any)[k] = (state as any)[k];
|
|
55
|
-
return out;
|
|
56
|
-
}
|
|
57
|
-
return (state as any)[key];
|
|
44
|
+
return state[key];
|
|
58
45
|
}) as UseStoreGet<T>;
|
|
59
46
|
|
|
60
47
|
function subscribe(selectorOrListener: any, maybeListener?: any) {
|
|
@@ -88,18 +75,20 @@ export function createStore<T>(
|
|
|
88
75
|
function setValue(
|
|
89
76
|
patch: Partial<T> | ((state: T) => Partial<T> | Promise<Partial<T>>),
|
|
90
77
|
): void {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
|
|
78
|
+
let resolved = typeof patch === 'function' ? patch(state) : patch;
|
|
79
|
+
if (resolved instanceof Promise) {
|
|
80
|
+
resolved
|
|
81
|
+
.then(res => {
|
|
82
|
+
if (res && typeof res === 'object') {
|
|
83
|
+
setFn(res as Partial<T>);
|
|
84
|
+
}
|
|
85
|
+
})
|
|
86
|
+
.catch(error => console.warn('[store.setValue] patch error:', error));
|
|
87
|
+
} else {
|
|
88
|
+
if (resolved && typeof resolved === 'object') {
|
|
89
|
+
setFn(resolved as Partial<T>);
|
|
101
90
|
}
|
|
102
|
-
}
|
|
91
|
+
}
|
|
103
92
|
}
|
|
104
93
|
|
|
105
94
|
function setImmer(updater: (draft: Draft<T>) => void): void {
|
|
@@ -168,22 +157,23 @@ export function createStore<T>(
|
|
|
168
157
|
}
|
|
169
158
|
|
|
170
159
|
function getInitialValue(): T {
|
|
171
|
-
if (Array.isArray(
|
|
172
|
-
return (
|
|
160
|
+
if (Array.isArray(_initialState)) {
|
|
161
|
+
return (_initialState as any).slice();
|
|
173
162
|
}
|
|
174
|
-
if (
|
|
175
|
-
return {...
|
|
163
|
+
if (_initialState && typeof _initialState === 'object') {
|
|
164
|
+
return {..._initialState};
|
|
176
165
|
}
|
|
177
|
-
return
|
|
166
|
+
return _initialState;
|
|
178
167
|
}
|
|
179
168
|
|
|
180
169
|
function isDirty() {
|
|
181
|
-
return !areEqual(state,
|
|
170
|
+
return !areEqual(state, _initialState);
|
|
182
171
|
}
|
|
183
172
|
|
|
184
173
|
function getNumberOfSubscriber() {
|
|
185
174
|
return emitter.getNumberOfSubscriber();
|
|
186
175
|
}
|
|
176
|
+
|
|
187
177
|
return {
|
|
188
178
|
getValue,
|
|
189
179
|
getInitialValue,
|