pocket-state 0.1.8 → 0.1.10

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.8",
3
+ "version": "0.1.10",
4
4
  "description": "tiny global store",
5
5
  "main": "src/index",
6
6
  "codegenConfig": {
@@ -10,18 +10,17 @@ export function createStore<T>(
10
10
  equalityFn?: (a: any, b: any) => boolean,
11
11
  ): Store<T> {
12
12
  const emitter: IEventEmitter = new EventEmitter();
13
- let state = initialState;
13
+ const _initialState = Array.isArray(initialState)
14
+ ? (initialState as any).slice()
15
+ : initialState && typeof initialState === 'object'
16
+ ? {...initialState}
17
+ : initialState;
18
+ let state = _initialState;
14
19
 
15
20
  const areEqual = equalityFn ?? shallow;
16
21
 
17
- let emitScheduled = false;
18
22
  const emitState = () => {
19
- if (emitScheduled) return;
20
- emitScheduled = true;
21
- queueMicrotask(() => {
22
- emitScheduled = false;
23
- emitter.emit('state', state);
24
- });
23
+ emitter.emit('state', state);
25
24
  };
26
25
 
27
26
  function baseSet(delta: Partial<T>) {
@@ -31,13 +30,6 @@ export function createStore<T>(
31
30
 
32
31
  if (!areEqual(state, nextState)) {
33
32
  state = nextState;
34
-
35
- if (process.env.NODE_ENV !== 'production') {
36
- try {
37
- Object.freeze(state as any);
38
- } catch {}
39
- }
40
-
41
33
  emitState();
42
34
  }
43
35
  }
@@ -47,14 +39,9 @@ export function createStore<T>(
47
39
  baseSet as (patch: Partial<T>) => void,
48
40
  );
49
41
 
50
- const getValue = ((key?: keyof T | (keyof T)[]) => {
42
+ const getValue = ((key?: keyof T) => {
51
43
  if (key === undefined) return state;
52
- if (Array.isArray(key)) {
53
- const out = {} as Pick<T, (typeof key)[number]>;
54
- for (const k of key) (out as any)[k] = (state as any)[k];
55
- return out;
56
- }
57
- return (state as any)[key];
44
+ return state[key];
58
45
  }) as UseStoreGet<T>;
59
46
 
60
47
  function subscribe(selectorOrListener: any, maybeListener?: any) {
@@ -88,18 +75,20 @@ export function createStore<T>(
88
75
  function setValue(
89
76
  patch: Partial<T> | ((state: T) => Partial<T> | Promise<Partial<T>>),
90
77
  ): void {
91
- (async () => {
92
- try {
93
- const resolved =
94
- typeof patch === 'function' ? await (patch as any)(state) : patch;
95
-
96
- if (resolved && typeof resolved === 'object') {
97
- setFn(resolved as Partial<T>);
98
- }
99
- } catch (error) {
100
- console.warn('[store.setValue] patch error:', error);
78
+ let resolved = typeof patch === 'function' ? patch(state) : patch;
79
+ if (resolved instanceof Promise) {
80
+ resolved
81
+ .then(res => {
82
+ if (res && typeof res === 'object') {
83
+ setFn(res as Partial<T>);
84
+ }
85
+ })
86
+ .catch(error => console.warn('[store.setValue] patch error:', error));
87
+ } else {
88
+ if (resolved && typeof resolved === 'object') {
89
+ setFn(resolved as Partial<T>);
101
90
  }
102
- })();
91
+ }
103
92
  }
104
93
 
105
94
  function setImmer(updater: (draft: Draft<T>) => void): void {
@@ -168,22 +157,23 @@ export function createStore<T>(
168
157
  }
169
158
 
170
159
  function getInitialValue(): T {
171
- if (Array.isArray(state)) {
172
- return (state as any).slice();
160
+ if (Array.isArray(_initialState)) {
161
+ return (_initialState as any).slice();
173
162
  }
174
- if (state && typeof state === 'object') {
175
- return {...(state as any)};
163
+ if (_initialState && typeof _initialState === 'object') {
164
+ return {..._initialState};
176
165
  }
177
- return state;
166
+ return _initialState;
178
167
  }
179
168
 
180
169
  function isDirty() {
181
- return !areEqual(state, initialState);
170
+ return !areEqual(state, _initialState);
182
171
  }
183
172
 
184
173
  function getNumberOfSubscriber() {
185
174
  return emitter.getNumberOfSubscriber();
186
175
  }
176
+
187
177
  return {
188
178
  getValue,
189
179
  getInitialValue,