@t8/react-store 1.0.6 → 1.0.8

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/dist/index.js CHANGED
@@ -6,10 +6,10 @@ function isStore(x) {
6
6
  // node_modules/@t8/store/src/Store.ts
7
7
  var Store = class {
8
8
  state;
9
- callbacks;
9
+ callbacks = [];
10
+ revision = -1;
10
11
  constructor(data) {
11
12
  this.state = data;
12
- this.callbacks = [];
13
13
  }
14
14
  onUpdate(callback) {
15
15
  this.callbacks.push(callback);
@@ -26,6 +26,7 @@ var Store = class {
26
26
  let prevState = this.state;
27
27
  let nextState = update instanceof Function ? update(this.state) : update;
28
28
  this.state = nextState;
29
+ this.revision = Math.random();
29
30
  for (let callback of this.callbacks) callback(nextState, prevState);
30
31
  }
31
32
  };
@@ -38,6 +39,7 @@ function useStore(store, shouldUpdate = true) {
38
39
  let initedRef = useRef(false);
39
40
  let state = store.getState();
40
41
  let setState = useMemo(() => store.setState.bind(store), [store]);
42
+ let initialStoreRevision = useRef(store.revision);
41
43
  useEffect(() => {
42
44
  if (!shouldUpdate) return;
43
45
  let unsubscribe = store.onUpdate((nextState, prevState) => {
@@ -46,11 +48,13 @@ function useStore(store, shouldUpdate = true) {
46
48
  });
47
49
  if (!initedRef.current) {
48
50
  initedRef.current = true;
49
- setRevision(Math.random());
51
+ if (store.revision !== initialStoreRevision.current)
52
+ setRevision(Math.random());
50
53
  }
51
54
  return () => {
52
55
  unsubscribe();
53
56
  initedRef.current = false;
57
+ initialStoreRevision.current = store.revision;
54
58
  };
55
59
  }, [store, shouldUpdate]);
56
60
  return [state, setState];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@t8/react-store",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "description": "Straightforward and minimalist shared state management for React apps",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -32,6 +32,6 @@
32
32
  "@types/react": "^19.1.10"
33
33
  },
34
34
  "dependencies": {
35
- "@t8/store": "^1.0.1"
35
+ "@t8/store": "^1.1.1"
36
36
  }
37
37
  }
package/src/useStore.ts CHANGED
@@ -33,6 +33,7 @@ export function useStore<T>(
33
33
 
34
34
  let state = store.getState();
35
35
  let setState = useMemo(() => store.setState.bind(store), [store]);
36
+ let initialStoreRevision = useRef(store.revision);
36
37
 
37
38
  useEffect(() => {
38
39
  if (!shouldUpdate) return;
@@ -45,14 +46,19 @@ export function useStore<T>(
45
46
  setRevision(Math.random());
46
47
  });
47
48
 
49
+ // Trigger a rerender if the store state changed after the component
50
+ // last read it and before the component subscribed to its changes.
48
51
  if (!initedRef.current) {
49
52
  initedRef.current = true;
50
- setRevision(Math.random());
53
+
54
+ if (store.revision !== initialStoreRevision.current)
55
+ setRevision(Math.random());
51
56
  }
52
57
 
53
58
  return () => {
54
59
  unsubscribe();
55
60
  initedRef.current = false;
61
+ initialStoreRevision.current = store.revision;
56
62
  };
57
63
  }, [store, shouldUpdate]);
58
64