pocket-state 0.1.25 → 0.1.26
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 -15
- package/src/globalState/type.d.ts +3 -0
package/package.json
CHANGED
package/src/globalState/store.ts
CHANGED
|
@@ -77,20 +77,34 @@ export function createStore<T>(
|
|
|
77
77
|
|
|
78
78
|
function setValue(
|
|
79
79
|
patch: Partial<T> | ((state: T) => Partial<T> | Promise<Partial<T>>),
|
|
80
|
+
patchOptions?: {
|
|
81
|
+
forced?: boolean;
|
|
82
|
+
},
|
|
80
83
|
): void {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
84
|
+
const apply = (res: Partial<T>) => {
|
|
85
|
+
if (!res || typeof res !== 'object') return;
|
|
86
|
+
if (patchOptions?.forced) {
|
|
87
|
+
state = isArray(state) ? (res as unknown as T) : {...state, ...res};
|
|
88
|
+
emitState();
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
setFn(res);
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
try {
|
|
95
|
+
const resolved = typeof patch === 'function' ? patch(state) : patch;
|
|
96
|
+
|
|
97
|
+
if (isPromise(resolved)) {
|
|
98
|
+
resolved
|
|
99
|
+
.then(apply)
|
|
100
|
+
.catch(error =>
|
|
101
|
+
console.warn('[store.setValue] patch async error:', error),
|
|
102
|
+
);
|
|
103
|
+
} else {
|
|
104
|
+
apply(resolved);
|
|
93
105
|
}
|
|
106
|
+
} catch (error) {
|
|
107
|
+
console.warn('[store.setValue] patch error:', error);
|
|
94
108
|
}
|
|
95
109
|
}
|
|
96
110
|
|
|
@@ -99,7 +113,7 @@ export function createStore<T>(
|
|
|
99
113
|
const nextState = produce(state, updater);
|
|
100
114
|
if (areEqual(state, nextState)) return;
|
|
101
115
|
|
|
102
|
-
if (
|
|
116
|
+
if (isArray(state)) {
|
|
103
117
|
setFn(nextState as unknown as Partial<T>);
|
|
104
118
|
return;
|
|
105
119
|
}
|
|
@@ -140,7 +154,7 @@ export function createStore<T>(
|
|
|
140
154
|
// 2️⃣ reset(value) → dựa trên _initialState
|
|
141
155
|
let next = cloneObject(_initialState) as T;
|
|
142
156
|
|
|
143
|
-
if (
|
|
157
|
+
if (isArray(initialValue)) {
|
|
144
158
|
next = initialValue.slice() as T;
|
|
145
159
|
} else if (isObj(initialValue)) {
|
|
146
160
|
Object.assign(next as any, initialValue);
|
|
@@ -156,7 +170,7 @@ export function createStore<T>(
|
|
|
156
170
|
}
|
|
157
171
|
|
|
158
172
|
function getInitialValue(): T {
|
|
159
|
-
if (
|
|
173
|
+
if (isArray(_initialState)) {
|
|
160
174
|
return (_initialState as any).slice();
|
|
161
175
|
}
|
|
162
176
|
if (_initialState && typeof _initialState === 'object') {
|