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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pocket-state",
3
- "version": "0.1.25",
3
+ "version": "0.1.26",
4
4
  "description": "tiny global store",
5
5
  "main": "src/index",
6
6
  "codegenConfig": {
@@ -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
- let resolved = typeof patch === 'function' ? patch(state) : patch;
82
- if (isPromise(resolved)) {
83
- resolved
84
- .then(res => {
85
- if (res && typeof res === 'object') {
86
- setFn(res as Partial<T>);
87
- }
88
- })
89
- .catch(error => console.warn('[store.setValue] patch error:', error));
90
- } else {
91
- if (resolved && typeof resolved === 'object') {
92
- setFn(resolved as Partial<T>);
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 (Array.isArray(state)) {
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 (Array.isArray(initialValue)) {
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 (Array.isArray(_initialState)) {
173
+ if (isArray(_initialState)) {
160
174
  return (_initialState as any).slice();
161
175
  }
162
176
  if (_initialState && typeof _initialState === 'object') {
@@ -137,6 +137,9 @@ export interface Store<T> {
137
137
  */
138
138
  setValue(
139
139
  patch: Partial<T> | ((state: T) => Partial<T> | Promise<Partial<T>>),
140
+ patchOptions?: {
141
+ forced: boolean;
142
+ },
140
143
  ): void;
141
144
 
142
145
  /**