tiny-persistent-store 0.1.0
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/LICENSE +21 -0
- package/README.md +97 -0
- package/dist/index.cjs +122 -0
- package/dist/index.d.cts +27 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +94 -0
- package/package.json +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Arie Steinberg
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# tiny-persistent-store
|
|
2
|
+
|
|
3
|
+
A tiny React external store with optional `localStorage` persistence.
|
|
4
|
+
|
|
5
|
+
It is built around a simple idea:
|
|
6
|
+
|
|
7
|
+
- keep the current value in memory
|
|
8
|
+
- notify subscribers when the value changes
|
|
9
|
+
- optionally persist the value to `localStorage`
|
|
10
|
+
- connect React components with `useSyncExternalStore`
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install tiny-persistent-store
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Why
|
|
17
|
+
|
|
18
|
+
`localStorage` can save data between page refreshes, but it does not notify
|
|
19
|
+
React when that data changes.
|
|
20
|
+
|
|
21
|
+
This package gives you a small external store API for cases where you want a
|
|
22
|
+
simple global value without setting up a larger state-management solution.
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
// stores/counterStore.ts
|
|
28
|
+
import { createStorageStore } from "tiny-persistent-store";
|
|
29
|
+
|
|
30
|
+
// Pass a key to persist to localStorage.
|
|
31
|
+
export const counterStore = createStorageStore("counter", 0);
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
```tsx
|
|
35
|
+
// components/Counter.tsx
|
|
36
|
+
import { useStore } from "tiny-persistent-store";
|
|
37
|
+
import { counterStore } from "../stores/counterStore";
|
|
38
|
+
|
|
39
|
+
export function Counter() {
|
|
40
|
+
const count = useStore(counterStore);
|
|
41
|
+
|
|
42
|
+
return (
|
|
43
|
+
<div>
|
|
44
|
+
<p>Count: {count}</p>
|
|
45
|
+
<button onClick={() => counterStore.update((current) => current + 1)}>
|
|
46
|
+
+
|
|
47
|
+
</button>
|
|
48
|
+
<button onClick={() => counterStore.clear()}>Reset</button>
|
|
49
|
+
</div>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Any component that calls `useStore(counterStore)` will re-render when the value
|
|
55
|
+
changes.
|
|
56
|
+
|
|
57
|
+
## In-memory only store
|
|
58
|
+
|
|
59
|
+
Pass `null` as the key if you do not want persistence:
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
import { createStorageStore } from "tiny-persistent-store";
|
|
63
|
+
|
|
64
|
+
export const modalStore = createStorageStore(null, false);
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## API
|
|
68
|
+
|
|
69
|
+
### `createStorageStore(key, initialValue)`
|
|
70
|
+
|
|
71
|
+
Creates a store.
|
|
72
|
+
|
|
73
|
+
- `key: string | null` — `localStorage` key. Pass `null` to disable persistence.
|
|
74
|
+
- `initialValue: T` — the initial value for the store.
|
|
75
|
+
|
|
76
|
+
Returns:
|
|
77
|
+
|
|
78
|
+
- `getSnapshot()` — returns the current value for React's external store API
|
|
79
|
+
- `get()` — returns the current value manually
|
|
80
|
+
- `set(value)` — replaces the current value
|
|
81
|
+
- `update(fn)` — updates the value based on the current value
|
|
82
|
+
- `clear()` — resets to `initialValue` and removes the persisted value
|
|
83
|
+
- `subscribe(listener)` — subscribes to changes and returns an unsubscribe function
|
|
84
|
+
|
|
85
|
+
### `useStore(store)`
|
|
86
|
+
|
|
87
|
+
React hook that subscribes a component to a store and returns the latest value.
|
|
88
|
+
|
|
89
|
+
## Notes
|
|
90
|
+
|
|
91
|
+
This is not intended to replace Redux or other full state-management libraries
|
|
92
|
+
in large applications. It is a small utility for simple global state and for
|
|
93
|
+
learning how external stores work in React.
|
|
94
|
+
|
|
95
|
+
## License
|
|
96
|
+
|
|
97
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
createStorageStore: () => createStorageStore,
|
|
24
|
+
useStore: () => useStore
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
|
|
28
|
+
// src/createStorageStore.ts
|
|
29
|
+
function canUseLocalStorage() {
|
|
30
|
+
return typeof window !== "undefined" && typeof window.localStorage !== "undefined";
|
|
31
|
+
}
|
|
32
|
+
function readFromStorage(key, initialValue) {
|
|
33
|
+
if (!canUseLocalStorage()) {
|
|
34
|
+
return initialValue;
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
const savedValue = window.localStorage.getItem(key);
|
|
38
|
+
if (savedValue === null) {
|
|
39
|
+
return initialValue;
|
|
40
|
+
}
|
|
41
|
+
return JSON.parse(savedValue);
|
|
42
|
+
} catch {
|
|
43
|
+
return initialValue;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function writeToStorage(key, value) {
|
|
47
|
+
if (!canUseLocalStorage()) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
try {
|
|
51
|
+
window.localStorage.setItem(key, JSON.stringify(value));
|
|
52
|
+
} catch {
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function removeFromStorage(key) {
|
|
56
|
+
if (!canUseLocalStorage()) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
try {
|
|
60
|
+
window.localStorage.removeItem(key);
|
|
61
|
+
} catch {
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function createStorageStore(key, initialValue) {
|
|
65
|
+
let state = key === null ? initialValue : readFromStorage(key, initialValue);
|
|
66
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
67
|
+
function getSnapshot() {
|
|
68
|
+
return state;
|
|
69
|
+
}
|
|
70
|
+
function get() {
|
|
71
|
+
return state;
|
|
72
|
+
}
|
|
73
|
+
function notify() {
|
|
74
|
+
listeners.forEach((listener) => listener());
|
|
75
|
+
}
|
|
76
|
+
function set(value) {
|
|
77
|
+
state = value;
|
|
78
|
+
if (key !== null) {
|
|
79
|
+
writeToStorage(key, state);
|
|
80
|
+
}
|
|
81
|
+
notify();
|
|
82
|
+
}
|
|
83
|
+
function update(updater) {
|
|
84
|
+
set(updater(state));
|
|
85
|
+
}
|
|
86
|
+
function clear() {
|
|
87
|
+
state = initialValue;
|
|
88
|
+
if (key !== null) {
|
|
89
|
+
removeFromStorage(key);
|
|
90
|
+
}
|
|
91
|
+
notify();
|
|
92
|
+
}
|
|
93
|
+
function subscribe(listener) {
|
|
94
|
+
listeners.add(listener);
|
|
95
|
+
return () => {
|
|
96
|
+
listeners.delete(listener);
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
getSnapshot,
|
|
101
|
+
get,
|
|
102
|
+
set,
|
|
103
|
+
update,
|
|
104
|
+
clear,
|
|
105
|
+
subscribe
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// src/useStore.ts
|
|
110
|
+
var import_react = require("react");
|
|
111
|
+
function useStore(store) {
|
|
112
|
+
return (0, import_react.useSyncExternalStore)(
|
|
113
|
+
store.subscribe,
|
|
114
|
+
store.getSnapshot,
|
|
115
|
+
store.getSnapshot
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
119
|
+
0 && (module.exports = {
|
|
120
|
+
createStorageStore,
|
|
121
|
+
useStore
|
|
122
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
type Listener = () => void;
|
|
2
|
+
type StorageStore<T> = {
|
|
3
|
+
getSnapshot: () => T;
|
|
4
|
+
get: () => T;
|
|
5
|
+
set: (value: T) => void;
|
|
6
|
+
update: (updater: (current: T) => T) => void;
|
|
7
|
+
clear: () => void;
|
|
8
|
+
subscribe: (listener: Listener) => () => void;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Creates a tiny reactive store that can optionally persist its value to
|
|
12
|
+
* localStorage. The returned store is React-agnostic; use the `useStore`
|
|
13
|
+
* hook from this package to read it inside React components.
|
|
14
|
+
*
|
|
15
|
+
* @param key - localStorage key. Pass `null` to disable persistence.
|
|
16
|
+
* @param initialValue - value used before anything is read from storage.
|
|
17
|
+
*/
|
|
18
|
+
declare function createStorageStore<T>(key: string | null, initialValue: T): StorageStore<T>;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Connects a store created by `createStorageStore` to React.
|
|
22
|
+
* Subscribes to changes and returns the latest snapshot,
|
|
23
|
+
* triggering a re-render whenever the store updates.
|
|
24
|
+
*/
|
|
25
|
+
declare function useStore<T>(store: StorageStore<T>): T;
|
|
26
|
+
|
|
27
|
+
export { type StorageStore, createStorageStore, useStore };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
type Listener = () => void;
|
|
2
|
+
type StorageStore<T> = {
|
|
3
|
+
getSnapshot: () => T;
|
|
4
|
+
get: () => T;
|
|
5
|
+
set: (value: T) => void;
|
|
6
|
+
update: (updater: (current: T) => T) => void;
|
|
7
|
+
clear: () => void;
|
|
8
|
+
subscribe: (listener: Listener) => () => void;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Creates a tiny reactive store that can optionally persist its value to
|
|
12
|
+
* localStorage. The returned store is React-agnostic; use the `useStore`
|
|
13
|
+
* hook from this package to read it inside React components.
|
|
14
|
+
*
|
|
15
|
+
* @param key - localStorage key. Pass `null` to disable persistence.
|
|
16
|
+
* @param initialValue - value used before anything is read from storage.
|
|
17
|
+
*/
|
|
18
|
+
declare function createStorageStore<T>(key: string | null, initialValue: T): StorageStore<T>;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Connects a store created by `createStorageStore` to React.
|
|
22
|
+
* Subscribes to changes and returns the latest snapshot,
|
|
23
|
+
* triggering a re-render whenever the store updates.
|
|
24
|
+
*/
|
|
25
|
+
declare function useStore<T>(store: StorageStore<T>): T;
|
|
26
|
+
|
|
27
|
+
export { type StorageStore, createStorageStore, useStore };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// src/createStorageStore.ts
|
|
2
|
+
function canUseLocalStorage() {
|
|
3
|
+
return typeof window !== "undefined" && typeof window.localStorage !== "undefined";
|
|
4
|
+
}
|
|
5
|
+
function readFromStorage(key, initialValue) {
|
|
6
|
+
if (!canUseLocalStorage()) {
|
|
7
|
+
return initialValue;
|
|
8
|
+
}
|
|
9
|
+
try {
|
|
10
|
+
const savedValue = window.localStorage.getItem(key);
|
|
11
|
+
if (savedValue === null) {
|
|
12
|
+
return initialValue;
|
|
13
|
+
}
|
|
14
|
+
return JSON.parse(savedValue);
|
|
15
|
+
} catch {
|
|
16
|
+
return initialValue;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function writeToStorage(key, value) {
|
|
20
|
+
if (!canUseLocalStorage()) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
try {
|
|
24
|
+
window.localStorage.setItem(key, JSON.stringify(value));
|
|
25
|
+
} catch {
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function removeFromStorage(key) {
|
|
29
|
+
if (!canUseLocalStorage()) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
window.localStorage.removeItem(key);
|
|
34
|
+
} catch {
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function createStorageStore(key, initialValue) {
|
|
38
|
+
let state = key === null ? initialValue : readFromStorage(key, initialValue);
|
|
39
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
40
|
+
function getSnapshot() {
|
|
41
|
+
return state;
|
|
42
|
+
}
|
|
43
|
+
function get() {
|
|
44
|
+
return state;
|
|
45
|
+
}
|
|
46
|
+
function notify() {
|
|
47
|
+
listeners.forEach((listener) => listener());
|
|
48
|
+
}
|
|
49
|
+
function set(value) {
|
|
50
|
+
state = value;
|
|
51
|
+
if (key !== null) {
|
|
52
|
+
writeToStorage(key, state);
|
|
53
|
+
}
|
|
54
|
+
notify();
|
|
55
|
+
}
|
|
56
|
+
function update(updater) {
|
|
57
|
+
set(updater(state));
|
|
58
|
+
}
|
|
59
|
+
function clear() {
|
|
60
|
+
state = initialValue;
|
|
61
|
+
if (key !== null) {
|
|
62
|
+
removeFromStorage(key);
|
|
63
|
+
}
|
|
64
|
+
notify();
|
|
65
|
+
}
|
|
66
|
+
function subscribe(listener) {
|
|
67
|
+
listeners.add(listener);
|
|
68
|
+
return () => {
|
|
69
|
+
listeners.delete(listener);
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
getSnapshot,
|
|
74
|
+
get,
|
|
75
|
+
set,
|
|
76
|
+
update,
|
|
77
|
+
clear,
|
|
78
|
+
subscribe
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// src/useStore.ts
|
|
83
|
+
import { useSyncExternalStore } from "react";
|
|
84
|
+
function useStore(store) {
|
|
85
|
+
return useSyncExternalStore(
|
|
86
|
+
store.subscribe,
|
|
87
|
+
store.getSnapshot,
|
|
88
|
+
store.getSnapshot
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
export {
|
|
92
|
+
createStorageStore,
|
|
93
|
+
useStore
|
|
94
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "tiny-persistent-store",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A tiny React external store with optional localStorage persistence.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsup src/index.ts --format esm,cjs --dts --clean",
|
|
22
|
+
"prepublishOnly": "npm run build"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"react",
|
|
26
|
+
"state",
|
|
27
|
+
"state-management",
|
|
28
|
+
"store",
|
|
29
|
+
"localStorage",
|
|
30
|
+
"persistence",
|
|
31
|
+
"external-store",
|
|
32
|
+
"useSyncExternalStore"
|
|
33
|
+
],
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"react": ">=18"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@types/react": "^18.3.0",
|
|
40
|
+
"tsup": "^8.0.0",
|
|
41
|
+
"typescript": "^5.4.0"
|
|
42
|
+
}
|
|
43
|
+
}
|