pocket-state 0.1.9 → 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.9",
3
+ "version": "0.1.10",
4
4
  "description": "tiny global store",
5
5
  "main": "src/index",
6
6
  "codegenConfig": {
@@ -10,7 +10,12 @@ 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
 
@@ -35,7 +40,7 @@ export function createStore<T>(
35
40
  );
36
41
 
37
42
  const getValue = ((key?: keyof T) => {
38
- if (!key) return state;
43
+ if (key === undefined) return state;
39
44
  return state[key];
40
45
  }) as UseStoreGet<T>;
41
46
 
@@ -70,18 +75,20 @@ export function createStore<T>(
70
75
  function setValue(
71
76
  patch: Partial<T> | ((state: T) => Partial<T> | Promise<Partial<T>>),
72
77
  ): void {
73
- (async () => {
74
- try {
75
- const resolved =
76
- typeof patch === 'function' ? await patch(state) : patch;
77
-
78
- if (resolved && typeof resolved === 'object') {
79
- setFn(resolved as Partial<T>);
80
- }
81
- } catch (error) {
82
- 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>);
83
90
  }
84
- })();
91
+ }
85
92
  }
86
93
 
87
94
  function setImmer(updater: (draft: Draft<T>) => void): void {
@@ -95,17 +102,17 @@ export function createStore<T>(
95
102
  }
96
103
  const delta = {} as Partial<T>;
97
104
  let changed = false;
98
- for (const k in nextState) {
99
- const nv = nextState[k];
100
- const ov = state[k];
105
+ for (const k in nextState as any) {
106
+ const nv = (nextState as any)[k];
107
+ const ov = (state as any)[k];
101
108
  if (nv !== ov) {
102
- delta[k] = nv;
109
+ (delta as any)[k] = nv;
103
110
  changed = true;
104
111
  }
105
112
  }
106
- for (const k in state) {
113
+ for (const k in state as any) {
107
114
  if (!(k in (nextState as any))) {
108
- delta[k] = undefined;
115
+ (delta as any)[k] = undefined;
109
116
  changed = true;
110
117
  }
111
118
  }
@@ -150,22 +157,23 @@ export function createStore<T>(
150
157
  }
151
158
 
152
159
  function getInitialValue(): T {
153
- if (Array.isArray(state)) {
154
- return (state as any).slice();
160
+ if (Array.isArray(_initialState)) {
161
+ return (_initialState as any).slice();
155
162
  }
156
- if (state && typeof state === 'object') {
157
- return {...state};
163
+ if (_initialState && typeof _initialState === 'object') {
164
+ return {..._initialState};
158
165
  }
159
- return state;
166
+ return _initialState;
160
167
  }
161
168
 
162
169
  function isDirty() {
163
- return !areEqual(state, initialState);
170
+ return !areEqual(state, _initialState);
164
171
  }
165
172
 
166
173
  function getNumberOfSubscriber() {
167
174
  return emitter.getNumberOfSubscriber();
168
175
  }
176
+
169
177
  return {
170
178
  getValue,
171
179
  getInitialValue,