pocket-state 0.1.23 → 0.1.25

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.23",
3
+ "version": "0.1.25",
4
4
  "description": "tiny global store",
5
5
  "main": "src/index",
6
6
  "codegenConfig": {
@@ -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
- if (initialState === undefined) {
140
- resetToInitialState();
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
- const isObj = (
145
- v: unknown,
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
- let next = cloneObject(initialState) as T;
150
- if (initialValue !== undefined) {
151
- if (Array.isArray(initialValue)) {
152
- next = (initialValue as any).slice();
153
- } else if (isObj(initialValue)) {
154
- Object.assign(next as any, initialValue);
155
- } else {
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
- const current = getValue();
164
- if (!areEqual(current, next)) {
153
+ if (!areEqual(getValue(), next)) {
165
154
  setFn(next as unknown as Partial<T>);
166
155
  }
167
156
  }
@@ -14,7 +14,6 @@ interface FileList {
14
14
  export interface Listener<T = unknown> {
15
15
  (prev: T, next: T): void;
16
16
  }
17
-
18
17
  /**
19
18
  * Immer-style mutation function used for "mutable-looking" updates.
20
19
  *
@@ -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
  }