@t8/react-store 1.2.9 → 1.2.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/README.md +4 -4
- package/dist/index.cjs +7 -7
- package/dist/index.mjs +4 -4
- package/package.json +2 -2
- package/src/useStore.ts +5 -7
package/README.md
CHANGED
|
@@ -173,14 +173,14 @@ A standalone store initialized outside a component can be used by the component
|
|
|
173
173
|
|
|
174
174
|
## Persistence across page reloads
|
|
175
175
|
|
|
176
|
-
Replacing `new Store(data)` with `new PersistentStore(data,
|
|
176
|
+
Replacing `new Store(data)` with `new PersistentStore(data, { key })` as shown below gets the store's value initially restored from and saved whenever updated to the specified `key` in `localStorage`. (Pass `session: true` to the second parameter of the constructor to use `sessionStorage` instead of `localStorage`.) Otherwise, persistent stores work pretty much like regular stores described above.
|
|
177
177
|
|
|
178
178
|
```js
|
|
179
|
-
import { PersistentStore } from "@t8/
|
|
179
|
+
import { PersistentStore } from "@t8/persistent-store";
|
|
180
180
|
|
|
181
|
-
let counterStore = new PersistentStore(0, "counter");
|
|
181
|
+
let counterStore = new PersistentStore(0, { key: "counter" });
|
|
182
182
|
```
|
|
183
183
|
|
|
184
|
-
⬥ The way data gets saved to and restored from a browser storage entry (including filtering out certain data or otherwise rearranging the saved data) can be redefined by setting `options.serialize` and `options.deserialize` in `new PersistentStore(data,
|
|
184
|
+
⬥ The way data gets saved to and restored from a browser storage entry (including filtering out certain data or otherwise rearranging the saved data) can be redefined by setting `options.serialize` and `options.deserialize` in `new PersistentStore(data, options)`. By default, these options act like `JSON.stringify()` and `JSON.parse()` respectively.
|
|
185
185
|
|
|
186
186
|
⬥ `PersistentStore` skips interaction with the browser storage in non-browser environments, which makes it equally usable with SSR.
|
package/dist/index.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
let
|
|
1
|
+
let _t8_store = require("@t8/store");
|
|
2
2
|
let react = require("react");
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -29,16 +29,16 @@ let react = require("react");
|
|
|
29
29
|
* when this function returns `true`.
|
|
30
30
|
*/
|
|
31
31
|
function useStore(store, shouldUpdate = true) {
|
|
32
|
-
if (!(0,
|
|
32
|
+
if (!(0, _t8_store.isStore)(store)) throw new Error("'store' is not an instance of Store");
|
|
33
33
|
let [, setRevision] = (0, react.useState)(-1);
|
|
34
34
|
let value = store.getValue();
|
|
35
35
|
let setValue = (0, react.useMemo)(() => store.setValue.bind(store), [store]);
|
|
36
36
|
let initialStoreRevision = (0, react.useRef)(store.revision);
|
|
37
37
|
(0, react.useEffect)(() => {
|
|
38
|
-
|
|
38
|
+
store.emit("effect");
|
|
39
39
|
if (!shouldUpdate) return;
|
|
40
|
-
let unsubscribe = store.
|
|
41
|
-
if (typeof shouldUpdate !== "function" || shouldUpdate(
|
|
40
|
+
let unsubscribe = store.on("update", ({ current, previous }) => {
|
|
41
|
+
if (typeof shouldUpdate !== "function" || shouldUpdate(current, previous)) setRevision(Math.random());
|
|
42
42
|
});
|
|
43
43
|
if (store.revision !== initialStoreRevision.current) setRevision(Math.random());
|
|
44
44
|
return () => {
|
|
@@ -50,9 +50,9 @@ function useStore(store, shouldUpdate = true) {
|
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
exports.useStore = useStore;
|
|
53
|
-
Object.keys(
|
|
53
|
+
Object.keys(_t8_store).forEach(function (k) {
|
|
54
54
|
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
55
55
|
enumerable: true,
|
|
56
|
-
get: function () { return
|
|
56
|
+
get: function () { return _t8_store[k]; }
|
|
57
57
|
});
|
|
58
58
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isStore } from "@t8/store";
|
|
2
2
|
import { useEffect, useMemo, useRef, useState } from "react";
|
|
3
3
|
|
|
4
4
|
export * from "@t8/store"
|
|
@@ -37,10 +37,10 @@ function useStore(store, shouldUpdate = true) {
|
|
|
37
37
|
let setValue = useMemo(() => store.setValue.bind(store), [store]);
|
|
38
38
|
let initialStoreRevision = useRef(store.revision);
|
|
39
39
|
useEffect(() => {
|
|
40
|
-
|
|
40
|
+
store.emit("effect");
|
|
41
41
|
if (!shouldUpdate) return;
|
|
42
|
-
let unsubscribe = store.
|
|
43
|
-
if (typeof shouldUpdate !== "function" || shouldUpdate(
|
|
42
|
+
let unsubscribe = store.on("update", ({ current, previous }) => {
|
|
43
|
+
if (typeof shouldUpdate !== "function" || shouldUpdate(current, previous)) setRevision(Math.random());
|
|
44
44
|
});
|
|
45
45
|
if (store.revision !== initialStoreRevision.current) setRevision(Math.random());
|
|
46
46
|
return () => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@t8/react-store",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.10",
|
|
4
4
|
"description": "React app state management condensed to the essentials",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -40,6 +40,6 @@
|
|
|
40
40
|
"react-dom": "^19.2.3"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@t8/store": "^1.
|
|
43
|
+
"@t8/store": "^1.5.0"
|
|
44
44
|
}
|
|
45
45
|
}
|
package/src/useStore.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isStore, type Store } from "@t8/store";
|
|
2
2
|
import { useEffect, useMemo, useRef, useState } from "react";
|
|
3
3
|
|
|
4
4
|
export type SetStoreValue<T> = Store<T>["setValue"];
|
|
@@ -46,15 +46,13 @@ export function useStore<T>(
|
|
|
46
46
|
let initialStoreRevision = useRef(store.revision);
|
|
47
47
|
|
|
48
48
|
useEffect(() => {
|
|
49
|
-
|
|
49
|
+
// Allow stores to hook into the effect
|
|
50
|
+
store.emit("effect");
|
|
50
51
|
|
|
51
52
|
if (!shouldUpdate) return;
|
|
52
53
|
|
|
53
|
-
let unsubscribe = store.
|
|
54
|
-
if (
|
|
55
|
-
typeof shouldUpdate !== "function" ||
|
|
56
|
-
shouldUpdate(nextValue, prevValue)
|
|
57
|
-
)
|
|
54
|
+
let unsubscribe = store.on("update", ({ current, previous }) => {
|
|
55
|
+
if (typeof shouldUpdate !== "function" || shouldUpdate(current, previous))
|
|
58
56
|
setRevision(Math.random());
|
|
59
57
|
});
|
|
60
58
|
|