@t8/react-store 1.1.0 → 1.1.1

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.
Files changed (2) hide show
  1. package/dist/index.js +34 -0
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -3,6 +3,39 @@ function isStore(x) {
3
3
  return x !== null && typeof x === "object" && "on" in x && typeof x.on === "function" && "getState" in x && typeof x.getState === "function" && "setState" in x && typeof x.setState === "function";
4
4
  }
5
5
 
6
+ // node_modules/@t8/store/src/persist.ts
7
+ function getStorage(session) {
8
+ if (typeof window === "undefined") return;
9
+ return session ? sessionStorage : localStorage;
10
+ }
11
+ function persist(store, storageKey, session = false) {
12
+ let inited = false;
13
+ function read() {
14
+ let storage = getStorage(session);
15
+ if (storage) {
16
+ try {
17
+ let rawState = storage.getItem(storageKey);
18
+ if (rawState !== null) store.setState(JSON.parse(rawState));
19
+ } catch {
20
+ }
21
+ }
22
+ if (!inited) inited = true;
23
+ }
24
+ function write(state) {
25
+ let storage = getStorage(session);
26
+ if (inited && storage) {
27
+ try {
28
+ storage.setItem(storageKey, JSON.stringify(state));
29
+ } catch {
30
+ }
31
+ }
32
+ }
33
+ store.on("sync", read);
34
+ store.once("effect", read);
35
+ store.on("update", write);
36
+ return store;
37
+ }
38
+
6
39
  // node_modules/@t8/store/src/Store.ts
7
40
  var Store = class {
8
41
  state;
@@ -73,5 +106,6 @@ function useStore(store, shouldUpdate = true) {
73
106
  export {
74
107
  Store,
75
108
  isStore,
109
+ persist,
76
110
  useStore
77
111
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@t8/react-store",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "Small React app state management lib aligned with React's state pattern, condensed to the essentials",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -45,6 +45,6 @@
45
45
  "typescript": "^5.9.3"
46
46
  },
47
47
  "dependencies": {
48
- "@t8/store": "^1.2.0"
48
+ "@t8/store": "^1.2.1"
49
49
  }
50
50
  }