@t8/react-store 1.2.5 → 1.2.6
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.cjs +18 -20
- package/dist/index.mjs +19 -21
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
let __t8_store = require("@t8/store");
|
|
2
2
|
let react = require("react");
|
|
3
3
|
|
|
4
|
-
//#region src/useStore.ts
|
|
5
4
|
/**
|
|
6
5
|
* Returns the state value of `store` passed as the parameter and
|
|
7
6
|
* a function to update the store state value.
|
|
@@ -30,27 +29,26 @@ let react = require("react");
|
|
|
30
29
|
* when this function returns `true`.
|
|
31
30
|
*/
|
|
32
31
|
function useStore(store, shouldUpdate = true) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
32
|
+
if (!(0, __t8_store.isStore)(store)) throw new Error("'store' is not an instance of Store");
|
|
33
|
+
let [, setRevision] = (0, react.useState)(-1);
|
|
34
|
+
let state = store.getState();
|
|
35
|
+
let setState = (0, react.useMemo)(() => store.setState.bind(store), [store]);
|
|
36
|
+
let initialStoreRevision = (0, react.useRef)(store.revision);
|
|
37
|
+
(0, react.useEffect)(() => {
|
|
38
|
+
if ((0, __t8_store.isPersistentStore)(store)) store.syncOnce();
|
|
39
|
+
if (!shouldUpdate) return;
|
|
40
|
+
let unsubscribe = store.onUpdate((nextState, prevState) => {
|
|
41
|
+
if (typeof shouldUpdate !== "function" || shouldUpdate(nextState, prevState)) setRevision(Math.random());
|
|
42
|
+
});
|
|
43
|
+
if (store.revision !== initialStoreRevision.current) setRevision(Math.random());
|
|
44
|
+
return () => {
|
|
45
|
+
unsubscribe();
|
|
46
|
+
initialStoreRevision.current = store.revision;
|
|
47
|
+
};
|
|
48
|
+
}, [store, shouldUpdate]);
|
|
49
|
+
return [state, setState];
|
|
51
50
|
}
|
|
52
51
|
|
|
53
|
-
//#endregion
|
|
54
52
|
exports.useStore = useStore;
|
|
55
53
|
Object.keys(__t8_store).forEach(function (k) {
|
|
56
54
|
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
package/dist/index.mjs
CHANGED
|
@@ -3,7 +3,6 @@ import { useEffect, useMemo, useRef, useState } from "react";
|
|
|
3
3
|
|
|
4
4
|
export * from "@t8/store"
|
|
5
5
|
|
|
6
|
-
//#region src/useStore.ts
|
|
7
6
|
/**
|
|
8
7
|
* Returns the state value of `store` passed as the parameter and
|
|
9
8
|
* a function to update the store state value.
|
|
@@ -32,25 +31,24 @@ export * from "@t8/store"
|
|
|
32
31
|
* when this function returns `true`.
|
|
33
32
|
*/
|
|
34
33
|
function useStore(store, shouldUpdate = true) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
34
|
+
if (!isStore(store)) throw new Error("'store' is not an instance of Store");
|
|
35
|
+
let [, setRevision] = useState(-1);
|
|
36
|
+
let state = store.getState();
|
|
37
|
+
let setState = useMemo(() => store.setState.bind(store), [store]);
|
|
38
|
+
let initialStoreRevision = useRef(store.revision);
|
|
39
|
+
useEffect(() => {
|
|
40
|
+
if (isPersistentStore(store)) store.syncOnce();
|
|
41
|
+
if (!shouldUpdate) return;
|
|
42
|
+
let unsubscribe = store.onUpdate((nextState, prevState) => {
|
|
43
|
+
if (typeof shouldUpdate !== "function" || shouldUpdate(nextState, prevState)) setRevision(Math.random());
|
|
44
|
+
});
|
|
45
|
+
if (store.revision !== initialStoreRevision.current) setRevision(Math.random());
|
|
46
|
+
return () => {
|
|
47
|
+
unsubscribe();
|
|
48
|
+
initialStoreRevision.current = store.revision;
|
|
49
|
+
};
|
|
50
|
+
}, [store, shouldUpdate]);
|
|
51
|
+
return [state, setState];
|
|
53
52
|
}
|
|
54
53
|
|
|
55
|
-
|
|
56
|
-
export { useStore };
|
|
54
|
+
export { useStore };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@t8/react-store",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.6",
|
|
4
4
|
"description": "Small React app state management lib aligned with React's state pattern, condensed to the essentials",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@playwright/test": "^1.56.0",
|
|
35
|
-
"@t8/serve": "^0.1.
|
|
35
|
+
"@t8/serve": "^0.1.38",
|
|
36
36
|
"@types/node": "^24.5.2",
|
|
37
37
|
"@types/react": "^19.2.7",
|
|
38
38
|
"@types/react-dom": "^19.2.3",
|
|
@@ -40,6 +40,6 @@
|
|
|
40
40
|
"react-dom": "^19.2.1"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@t8/store": "^1.3.
|
|
43
|
+
"@t8/store": "^1.3.8"
|
|
44
44
|
}
|
|
45
45
|
}
|