pocket-state 0.1.24 → 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.24",
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') {
@@ -14,10 +14,6 @@ interface FileList {
14
14
  export interface Listener<T = unknown> {
15
15
  (prev: T, next: T): void;
16
16
  }
17
- export interface Listener<T = unknown> {
18
- (next: T): void;
19
- }
20
-
21
17
  /**
22
18
  * Immer-style mutation function used for "mutable-looking" updates.
23
19
  *
@@ -141,6 +137,9 @@ export interface Store<T> {
141
137
  */
142
138
  setValue(
143
139
  patch: Partial<T> | ((state: T) => Partial<T> | Promise<Partial<T>>),
140
+ patchOptions?: {
141
+ forced: boolean;
142
+ },
144
143
  ): void;
145
144
 
146
145
  /**
@@ -20,7 +20,7 @@ export class EventEmitter implements IEventEmitter {
20
20
  this.onceWrappers
21
21
  .get(event)
22
22
  ?.delete(listener as unknown as Listener<any>);
23
- listener(payload);
23
+ listener(payload, payload);
24
24
  };
25
25
  if (!this.onceWrappers.has(event)) {
26
26
  this.onceWrappers.set(event, new Map());
@@ -39,7 +39,7 @@ export class EventEmitter implements IEventEmitter {
39
39
  if (!listeners || listeners.size === 0) return;
40
40
  for (const l of listeners) {
41
41
  try {
42
- l(payload);
42
+ l(payload, payload);
43
43
  } catch (error) {
44
44
  console.warn(`Error in listener for '${event}':`, error);
45
45
  }