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