pocket-state 0.1.22 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pocket-state",
3
- "version": "0.1.22",
3
+ "version": "0.1.24",
4
4
  "description": "tiny global store",
5
5
  "main": "src/index",
6
6
  "codegenConfig": {
@@ -126,27 +126,31 @@ export function createStore<T>(
126
126
  }
127
127
 
128
128
  function reset(initialValue?: T | Partial<T>) {
129
- const isObj = (
130
- v: unknown,
131
- ): v is Record<string | symbol | number, unknown> =>
129
+ const isObj = (v: unknown): v is Record<any, any> =>
132
130
  typeof v === 'object' && v !== null;
133
131
 
134
- let next = cloneObject(initialState) as T;
135
- if (initialValue !== undefined) {
136
- if (Array.isArray(initialValue)) {
137
- next = (initialValue as any).slice();
138
- } else if (isObj(initialValue)) {
139
- Object.assign(next as any, initialValue);
140
- } else {
141
- const current = getValue();
142
- if (!Object.is(current as any, initialValue as any)) {
143
- setFn(initialValue as unknown as Partial<T>);
144
- }
145
- return;
132
+ // 1️⃣ reset() force về _initialState + force emit
133
+ if (initialValue === undefined) {
134
+ const cloned = cloneObject(_initialState) as T;
135
+ state = cloned;
136
+ emitState();
137
+ return;
138
+ }
139
+
140
+ // 2️⃣ reset(value) dựa trên _initialState
141
+ let next = cloneObject(_initialState) as T;
142
+
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>);
146
150
  }
151
+ return;
147
152
  }
148
- const current = getValue();
149
- if (!areEqual(current, next)) {
153
+ if (!areEqual(getValue(), next)) {
150
154
  setFn(next as unknown as Partial<T>);
151
155
  }
152
156
  }
@@ -14,6 +14,9 @@ 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
+ }
17
20
 
18
21
  /**
19
22
  * Immer-style mutation function used for "mutable-looking" updates.