pocket-state 0.1.22 → 0.1.24
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 +21 -17
- package/src/globalState/type.d.ts +3 -0
package/package.json
CHANGED
package/src/globalState/store.ts
CHANGED
|
@@ -126,27 +126,31 @@ export function createStore<T>(
|
|
|
126
126
|
}
|
|
127
127
|
|
|
128
128
|
function reset(initialValue?: T | Partial<T>) {
|
|
129
|
-
const isObj = (
|
|
130
|
-
v: unknown,
|
|
131
|
-
): v is Record<string | symbol | number, unknown> =>
|
|
129
|
+
const isObj = (v: unknown): v is Record<any, any> =>
|
|
132
130
|
typeof v === 'object' && v !== null;
|
|
133
131
|
|
|
134
|
-
|
|
135
|
-
if (initialValue
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
132
|
+
// 1️⃣ reset() → force về _initialState + force emit
|
|
133
|
+
if (initialValue === undefined) {
|
|
134
|
+
const cloned = cloneObject(_initialState) as T;
|
|
135
|
+
state = cloned;
|
|
136
|
+
emitState();
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// 2️⃣ reset(value) → dựa trên _initialState
|
|
141
|
+
let next = cloneObject(_initialState) as T;
|
|
142
|
+
|
|
143
|
+
if (Array.isArray(initialValue)) {
|
|
144
|
+
next = initialValue.slice() as T;
|
|
145
|
+
} else if (isObj(initialValue)) {
|
|
146
|
+
Object.assign(next as any, initialValue);
|
|
147
|
+
} else {
|
|
148
|
+
if (!Object.is(getValue(), initialValue)) {
|
|
149
|
+
setFn(initialValue as unknown as Partial<T>);
|
|
146
150
|
}
|
|
151
|
+
return;
|
|
147
152
|
}
|
|
148
|
-
|
|
149
|
-
if (!areEqual(current, next)) {
|
|
153
|
+
if (!areEqual(getValue(), next)) {
|
|
150
154
|
setFn(next as unknown as Partial<T>);
|
|
151
155
|
}
|
|
152
156
|
}
|