pocket-state 0.1.8 → 0.1.9
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 +12 -30
package/package.json
CHANGED
package/src/globalState/store.ts
CHANGED
|
@@ -14,14 +14,8 @@ export function createStore<T>(
|
|
|
14
14
|
|
|
15
15
|
const areEqual = equalityFn ?? shallow;
|
|
16
16
|
|
|
17
|
-
let emitScheduled = false;
|
|
18
17
|
const emitState = () => {
|
|
19
|
-
|
|
20
|
-
emitScheduled = true;
|
|
21
|
-
queueMicrotask(() => {
|
|
22
|
-
emitScheduled = false;
|
|
23
|
-
emitter.emit('state', state);
|
|
24
|
-
});
|
|
18
|
+
emitter.emit('state', state);
|
|
25
19
|
};
|
|
26
20
|
|
|
27
21
|
function baseSet(delta: Partial<T>) {
|
|
@@ -31,13 +25,6 @@ export function createStore<T>(
|
|
|
31
25
|
|
|
32
26
|
if (!areEqual(state, nextState)) {
|
|
33
27
|
state = nextState;
|
|
34
|
-
|
|
35
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
36
|
-
try {
|
|
37
|
-
Object.freeze(state as any);
|
|
38
|
-
} catch {}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
28
|
emitState();
|
|
42
29
|
}
|
|
43
30
|
}
|
|
@@ -47,14 +34,9 @@ export function createStore<T>(
|
|
|
47
34
|
baseSet as (patch: Partial<T>) => void,
|
|
48
35
|
);
|
|
49
36
|
|
|
50
|
-
const getValue = ((key?: keyof T
|
|
51
|
-
if (key
|
|
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];
|
|
37
|
+
const getValue = ((key?: keyof T) => {
|
|
38
|
+
if (!key) return state;
|
|
39
|
+
return state[key];
|
|
58
40
|
}) as UseStoreGet<T>;
|
|
59
41
|
|
|
60
42
|
function subscribe(selectorOrListener: any, maybeListener?: any) {
|
|
@@ -91,7 +73,7 @@ export function createStore<T>(
|
|
|
91
73
|
(async () => {
|
|
92
74
|
try {
|
|
93
75
|
const resolved =
|
|
94
|
-
typeof patch === 'function' ? await
|
|
76
|
+
typeof patch === 'function' ? await patch(state) : patch;
|
|
95
77
|
|
|
96
78
|
if (resolved && typeof resolved === 'object') {
|
|
97
79
|
setFn(resolved as Partial<T>);
|
|
@@ -113,17 +95,17 @@ export function createStore<T>(
|
|
|
113
95
|
}
|
|
114
96
|
const delta = {} as Partial<T>;
|
|
115
97
|
let changed = false;
|
|
116
|
-
for (const k in nextState
|
|
117
|
-
const nv =
|
|
118
|
-
const ov =
|
|
98
|
+
for (const k in nextState) {
|
|
99
|
+
const nv = nextState[k];
|
|
100
|
+
const ov = state[k];
|
|
119
101
|
if (nv !== ov) {
|
|
120
|
-
|
|
102
|
+
delta[k] = nv;
|
|
121
103
|
changed = true;
|
|
122
104
|
}
|
|
123
105
|
}
|
|
124
|
-
for (const k in state
|
|
106
|
+
for (const k in state) {
|
|
125
107
|
if (!(k in (nextState as any))) {
|
|
126
|
-
|
|
108
|
+
delta[k] = undefined;
|
|
127
109
|
changed = true;
|
|
128
110
|
}
|
|
129
111
|
}
|
|
@@ -172,7 +154,7 @@ export function createStore<T>(
|
|
|
172
154
|
return (state as any).slice();
|
|
173
155
|
}
|
|
174
156
|
if (state && typeof state === 'object') {
|
|
175
|
-
return {...
|
|
157
|
+
return {...state};
|
|
176
158
|
}
|
|
177
159
|
return state;
|
|
178
160
|
}
|