pocket-state 0.1.10 → 0.1.12
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/README.md +25 -8
- package/package.json +1 -1
- package/src/globalState/store.ts +3 -3
- package/src/index.tsx +5 -23
package/README.md
CHANGED
|
@@ -14,6 +14,7 @@ Works seamlessly **inside React** with hooks or **outside React** with a simple
|
|
|
14
14
|
- 🔌 Framework-agnostic – Works in plain TS/JS and React.
|
|
15
15
|
- 🛠 Middleware – Logging, persistence, batching, devtools bridges, etc.
|
|
16
16
|
- 🔔 Event Emitter – Subscribe to store and custom events.
|
|
17
|
+
- ⚖️ EqualityFn – Custom comparator (shallow by default, can use deep/custom).
|
|
17
18
|
- ✅ TypeScript-first – Fully type-safe.
|
|
18
19
|
|
|
19
20
|
---
|
|
@@ -183,17 +184,33 @@ export function CounterComponent() {
|
|
|
183
184
|
|
|
184
185
|
---
|
|
185
186
|
|
|
186
|
-
##
|
|
187
|
+
## ⚖️ EqualityFn
|
|
187
188
|
|
|
188
|
-
|
|
189
|
+
By default, `createStore` uses a shallow equality function to detect changes.
|
|
190
|
+
You can override this by providing a custom comparator as the 3rd argument:
|
|
189
191
|
|
|
190
|
-
```
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
192
|
+
```ts
|
|
193
|
+
import {createStore} from 'pocket-state';
|
|
194
|
+
import deepEqual from 'fast-deep-equal';
|
|
195
|
+
|
|
196
|
+
interface State {
|
|
197
|
+
count: number;
|
|
198
|
+
nested: {a: number; b: number};
|
|
194
199
|
}
|
|
200
|
+
|
|
201
|
+
// shallow equality (default)
|
|
202
|
+
const store1 = createStore<State>({count: 0, nested: {a: 1, b: 2}});
|
|
203
|
+
|
|
204
|
+
// deep equality
|
|
205
|
+
const store2 = createStore<State>(
|
|
206
|
+
{count: 0, nested: {a: 1, b: 2}},
|
|
207
|
+
[],
|
|
208
|
+
deepEqual,
|
|
209
|
+
);
|
|
195
210
|
```
|
|
196
211
|
|
|
212
|
+
This is useful when working with nested state objects and you want to avoid unnecessary re-renders.
|
|
213
|
+
|
|
197
214
|
---
|
|
198
215
|
|
|
199
216
|
## 🧩 API Reference
|
|
@@ -224,6 +241,6 @@ Generates a custom hook for your store.
|
|
|
224
241
|
|
|
225
242
|
## 📜 License
|
|
226
243
|
|
|
227
|
-
MIT
|
|
244
|
+
MIT
|
|
228
245
|
|
|
229
|
-
keywords: pocket-state, state-management, react, react-native, typescript, hooks, store
|
|
246
|
+
keywords: pocket-state, state-management, react, react-native, typescript, hooks, store, equalityFn
|
package/package.json
CHANGED
package/src/globalState/store.ts
CHANGED
|
@@ -104,14 +104,14 @@ export function createStore<T>(
|
|
|
104
104
|
let changed = false;
|
|
105
105
|
for (const k in nextState as any) {
|
|
106
106
|
const nv = (nextState as any)[k];
|
|
107
|
-
const ov =
|
|
107
|
+
const ov = state[k];
|
|
108
108
|
if (nv !== ov) {
|
|
109
109
|
(delta as any)[k] = nv;
|
|
110
110
|
changed = true;
|
|
111
111
|
}
|
|
112
112
|
}
|
|
113
|
-
for (const k in state
|
|
114
|
-
if (!(k in
|
|
113
|
+
for (const k in state) {
|
|
114
|
+
if (!(k in nextState)) {
|
|
115
115
|
(delta as any)[k] = undefined;
|
|
116
116
|
changed = true;
|
|
117
117
|
}
|
package/src/index.tsx
CHANGED
|
@@ -1,23 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
Listener,
|
|
7
|
-
Middleware,
|
|
8
|
-
MutateFn,
|
|
9
|
-
UseStoreGet,
|
|
10
|
-
UseStoreSet,
|
|
11
|
-
Store,
|
|
12
|
-
} from './globalState/type';
|
|
13
|
-
|
|
14
|
-
export {createStore, useStore, createHook};
|
|
15
|
-
export type {
|
|
16
|
-
IEventEmitter,
|
|
17
|
-
Listener,
|
|
18
|
-
Middleware,
|
|
19
|
-
MutateFn,
|
|
20
|
-
UseStoreGet,
|
|
21
|
-
UseStoreSet,
|
|
22
|
-
Store,
|
|
23
|
-
};
|
|
1
|
+
export * from './globalState/store';
|
|
2
|
+
export * from './globalState/hooks';
|
|
3
|
+
export * from './globalState/create';
|
|
4
|
+
export type * from './globalState/type';
|
|
5
|
+
export * from './globalState/event';
|