pocket-state 0.1.13 → 0.1.15

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.13",
3
+ "version": "0.1.15",
4
4
  "description": "tiny global store",
5
5
  "main": "src/index",
6
6
  "codegenConfig": {
@@ -1,29 +1,31 @@
1
- import {useCallback, useSyncExternalStore} from 'react';
1
+ import {useCallback} from 'react';
2
2
  import type {Store} from './type';
3
+ import {useSyncExternalStoreWithSelector} from 'use-sync-external-store/shim/with-selector';
3
4
 
5
+ const identity = <T>(s: T) => s;
4
6
  export function useStore<T, S = T>(
5
7
  store: Store<T>,
6
8
  selector?: (state: T) => S,
9
+ isEqual?: (a: S, b: S) => boolean,
7
10
  ): S {
8
- const getSnapshot = useCallback(
9
- () =>
10
- selector
11
- ? selector(store.getValue())
12
- : (store.getValue() as unknown as S),
13
- [store, selector],
14
- );
15
-
16
11
  const subscribe = useCallback(
17
- (onChange: () => void) => {
18
- if (selector && store.subscribe) {
19
- return store.subscribe(selector, onChange);
20
- }
21
- return store.subscribe(() => {
22
- onChange();
23
- });
12
+ (notify: () => void) => {
13
+ return store.subscribe(() => notify());
24
14
  },
25
- [store, selector],
15
+ [store],
26
16
  );
27
17
 
28
- return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
18
+ const getSnapshot = store.getValue;
19
+ const getServerSnapshot = store.getValue;
20
+
21
+ const sel = selector ?? (identity as unknown as (t: T) => S);
22
+ const eq = isEqual ?? Object.is;
23
+
24
+ return useSyncExternalStoreWithSelector(
25
+ subscribe,
26
+ getSnapshot,
27
+ getServerSnapshot,
28
+ sel,
29
+ eq,
30
+ );
29
31
  }