snap-store 0.1.0 → 0.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.
- package/README.md +35 -57
- package/dist/esm/helper.d.ts +2 -0
- package/dist/esm/helper.d.ts.map +1 -0
- package/dist/esm/helper.js +4 -0
- package/dist/esm/helper.js.map +1 -0
- package/dist/esm/index.d.ts +0 -2
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +0 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/store.d.ts.map +1 -1
- package/dist/esm/store.js +67 -37
- package/dist/esm/store.js.map +1 -1
- package/dist/esm/types.d.ts +1 -19
- package/dist/esm/types.d.ts.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,22 +1,34 @@
|
|
|
1
1
|
# snap-store
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
This is an easy-to-use global state management library for React.
|
|
3
|
+
Easy-to-use global state management library for React.
|
|
6
4
|
|
|
7
5
|
## Installation
|
|
8
6
|
```sh
|
|
9
7
|
npm install snap-store
|
|
10
8
|
```
|
|
11
9
|
|
|
12
|
-
##
|
|
10
|
+
## Overview
|
|
11
|
+
|
|
12
|
+
**snap-store** simplifies global state management in React.
|
|
13
|
+
|
|
14
|
+
In apps like graphical editors or tools with editing functionality, it's often desirable to maintain state in a global store accessible throughout the application.
|
|
15
|
+
|
|
16
|
+
This library is designed to let you place app state to a central store while keeping your existing component implementations mostly intact.
|
|
17
|
+
|
|
18
|
+
It minimizes boilerplate code and complex hook calls, making it easy to create a central store that can be shared across multiple components.
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
### Key Features
|
|
13
22
|
|
|
14
|
-
|
|
15
|
-
So I wanted to have a non-proxy version of this.
|
|
23
|
+
**snap-store** is designed with the following principles:
|
|
16
24
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
25
|
+
1. **Easy to Migrate from Local State**: Store state is subscribed as plain values and updated with methods like `setValue(newValue)`, making it easy to migrate from local `useState` to a global store with minimal component changes.
|
|
26
|
+
|
|
27
|
+
2. **Consistent Access Pattern**: Both in-component and out-of-component store access use a similar API, making it easy to extract complex state update logic outside of components.
|
|
28
|
+
|
|
29
|
+
3. **Convenient Update Methods**: In addition to basic setters, the library provides useful update methods like `patch*` for partial updates and `produce*` for immer-based updates.
|
|
30
|
+
|
|
31
|
+
## Basic Usage
|
|
20
32
|
|
|
21
33
|
```ts
|
|
22
34
|
import {createStore} from "snap-store"
|
|
@@ -24,7 +36,7 @@ import {createStore} from "snap-store"
|
|
|
24
36
|
const store = createStore({ count: 0});
|
|
25
37
|
|
|
26
38
|
const Counter = () => {
|
|
27
|
-
const { count } = store.
|
|
39
|
+
const { count } = store.useSnapshot();
|
|
28
40
|
const { setCount } = store.mutations;
|
|
29
41
|
return <button onClick={() => setCount(prev => prev + 1)}>
|
|
30
42
|
{count}
|
|
@@ -37,14 +49,13 @@ const Counter = () => {
|
|
|
37
49
|
const store = createStore({ count: 0});
|
|
38
50
|
```
|
|
39
51
|
`createStore` takes an initial state object and returns a store.
|
|
40
|
-
The store holds wrapper signals for each field of the state object.
|
|
41
52
|
```ts
|
|
42
|
-
const { count } = store.
|
|
53
|
+
const { count } = store.useSnapshot();
|
|
43
54
|
```
|
|
44
|
-
In a component, `
|
|
55
|
+
In a component, `useSnapshot()` hook is used to refer to the states and make them reactive.
|
|
45
56
|
For this line, `count` is actually a getter and it registers a listener to track the value change.
|
|
46
57
|
|
|
47
|
-
##
|
|
58
|
+
## Examples
|
|
48
59
|
```ts
|
|
49
60
|
const store = createStore({ count: 0});
|
|
50
61
|
|
|
@@ -55,11 +66,11 @@ function handleButton(){
|
|
|
55
66
|
}
|
|
56
67
|
|
|
57
68
|
const Component = () => {
|
|
58
|
-
const { count } = store.
|
|
69
|
+
const { count } = store.useSnapshot(); //refer store state as reactive
|
|
59
70
|
return <button onClick={handleButton}>push me {count}</button>
|
|
60
71
|
}
|
|
61
72
|
```
|
|
62
|
-
In the component, `store.
|
|
73
|
+
In the component, `store.useSnapshot()` is used to refer to the store state as a reactive value.
|
|
63
74
|
|
|
64
75
|
Since this is a global state library, you can also read and write store states outside components.
|
|
65
76
|
`store.state` is used to read the value in non-component functions.
|
|
@@ -95,13 +106,13 @@ store.mutations.setPenStyle('dashed');
|
|
|
95
106
|
In mutations, there is `assigns` method to set multiple fields at a time.
|
|
96
107
|
It is useful if you want to update multiple values.
|
|
97
108
|
|
|
98
|
-
There is no performance difference since reactive effects (i.e. rendering) are batched and executed in the next frame.
|
|
109
|
+
There is no performance difference since reactive effects (i.e. rendering) are batched by React and executed in the next frame.
|
|
99
110
|
|
|
100
111
|
```ts
|
|
101
112
|
const store = createStore<{theme: "light" | "dark"}>({theme: "light" })
|
|
102
113
|
|
|
103
114
|
const ThemeSelector = () => {
|
|
104
|
-
const { theme } = store.
|
|
115
|
+
const { theme } = store.useSnapshot();
|
|
105
116
|
const { setTheme } = store.mutations;
|
|
106
117
|
return <div>
|
|
107
118
|
<IconButton
|
|
@@ -126,7 +137,7 @@ const store = createStore<{textSize: number, bgColor: string}>({
|
|
|
126
137
|
})
|
|
127
138
|
|
|
128
139
|
const BookReaderSettings = () => {
|
|
129
|
-
const snap = store.
|
|
140
|
+
const snap = store.useSnapshot();;
|
|
130
141
|
const mut = store.mutations;
|
|
131
142
|
return <div>
|
|
132
143
|
<Slider
|
|
@@ -151,60 +162,27 @@ const store = createStore({ name: "Mike", age: 20 });
|
|
|
151
162
|
|
|
152
163
|
//wrong code
|
|
153
164
|
const Component = () => {
|
|
154
|
-
const snap = store.
|
|
165
|
+
const snap = store.useSnapshot();;
|
|
155
166
|
if(snap.age < 20) return; //bad early return
|
|
156
167
|
return <div>Hello Gentleman, {snap.name}</div>
|
|
157
168
|
}
|
|
158
169
|
|
|
159
170
|
//working code
|
|
160
171
|
const Component = () => {
|
|
161
|
-
const { age, name } = store.
|
|
172
|
+
const { age, name } = store.useSnapshot();;
|
|
162
173
|
if(age < 20) return; //no problem
|
|
163
174
|
return <div>Hello Gentleman, {name}</div>
|
|
164
175
|
}
|
|
165
176
|
```
|
|
166
|
-
Each member of the snapshot object is a getter and it calls a hooks
|
|
177
|
+
Each member of the snapshot object is a getter and it calls a hooks internally.
|
|
167
178
|
|
|
168
179
|
Since a component must have the same hooks count for each render, non-destructive assign and early return are a bad combination.
|
|
169
180
|
|
|
170
|
-
The snapshot should be destructured if you have an early return.
|
|
171
|
-
|
|
172
|
-
## Other Signal functionalities
|
|
173
|
-
```ts
|
|
174
|
-
const store = createStore({ count: 0 });
|
|
175
|
-
|
|
176
|
-
effect(() => {
|
|
177
|
-
const count = store.state.count;
|
|
178
|
-
console.log("Effect:", count);
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
store.mutations.setCount((prev) => prev + 1);
|
|
182
|
-
|
|
183
|
-
const doubled = computed(() => {
|
|
184
|
-
const cnt = store.state.count;
|
|
185
|
-
console.log("computing for:", cnt);
|
|
186
|
-
return cnt * 2;
|
|
187
|
-
});
|
|
188
|
-
console.log("Doubled:", doubled.value);
|
|
189
|
-
```
|
|
190
|
-
There are two `effect()` and `computed()` helper functions intended to be used in non-component context.
|
|
191
|
-
|
|
192
|
-
`effect()` tracks the referred state changes in the callback and is automatically re-evaluated when the tracked values change.
|
|
193
|
-
|
|
194
|
-
`computed()` is used for caching the computation result, returning a derived signal. It is re-evaluated when tracked values change.
|
|
195
|
-
|
|
196
|
-
## References
|
|
197
|
-
- [valtio](https://github.com/pmndrs/valtio)
|
|
198
|
-
- [@preact/signals-react](https://github.com/preactjs/signals)
|
|
199
|
-
|
|
200
|
-
`snap-store` is highly influenced by these libraries.
|
|
201
|
-
|
|
202
|
-
Compared to `valtio`, this library provides a similar design of global store but it doesn't use proxies. Also the mutations are applied by the methods not by assignment.
|
|
203
|
-
|
|
204
|
-
Compared to `@preact/signals-react`, although the mechanism of the signal is similar, this library is aimed to supply an opinionated store system whereas `@preact/signals` provides the basic primitive signal functions.
|
|
181
|
+
The snapshot object should be destructured if you have an early return.
|
|
205
182
|
|
|
206
183
|
## License
|
|
207
184
|
|
|
208
185
|
MIT License
|
|
209
186
|
|
|
210
187
|
|
|
188
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helper.d.ts","sourceRoot":"","sources":["../../src/helper.ts"],"names":[],"mappings":"AAAA,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,UAEjD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helper.js","sourceRoot":"","sources":["../../src/helper.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,qBAAqB,CAAC,IAAY;IAChD,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AACtD,CAAC"}
|
package/dist/esm/index.d.ts
CHANGED
package/dist/esm/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC"}
|
package/dist/esm/index.js
CHANGED
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC"}
|
package/dist/esm/store.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../src/store.ts"],"names":[],"mappings":"AAGA,OAAO,
|
|
1
|
+
{"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../src/store.ts"],"names":[],"mappings":"AAGA,OAAO,EAAa,KAAK,EAAE,MAAM,SAAS,CAAC;AAE3C,wBAAgB,WAAW,CAAC,CAAC,SAAS,MAAM,EAAE,YAAY,EAAE,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAwFvE"}
|
package/dist/esm/store.js
CHANGED
|
@@ -1,48 +1,78 @@
|
|
|
1
1
|
import { produce } from "immer";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { useEffect, useId, useMemo, useState } from "react";
|
|
3
|
+
import { capitalizeFirstLetter } from "./helper";
|
|
4
4
|
export function createStore(initialState) {
|
|
5
|
-
const
|
|
6
|
-
const
|
|
5
|
+
const hub = {};
|
|
6
|
+
const state = initialState;
|
|
7
7
|
const mutations = {};
|
|
8
|
-
const
|
|
9
|
-
for (const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
8
|
+
const _mutations = mutations;
|
|
9
|
+
for (const _key in initialState) {
|
|
10
|
+
const key = _key;
|
|
11
|
+
const setValue = (arg) => {
|
|
12
|
+
let value;
|
|
13
|
+
if (typeof arg === "function") {
|
|
14
|
+
value = arg(state[key]);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
value = arg;
|
|
18
|
+
}
|
|
19
|
+
state[key] = value;
|
|
20
|
+
for (const hubKey in hub) {
|
|
21
|
+
const entry = hub[hubKey];
|
|
22
|
+
if (entry.dependentFieldKeys.has(key)) {
|
|
23
|
+
entry.refreshView();
|
|
24
|
+
}
|
|
25
|
+
}
|
|
25
26
|
};
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
const suffix = capitalizeFirstLetter(key);
|
|
28
|
+
_mutations[`set${suffix}`] = setValue;
|
|
29
|
+
_mutations[`produce${suffix}`] = (fn) => {
|
|
30
|
+
setValue((draft) => produce(draft, fn));
|
|
31
|
+
};
|
|
32
|
+
_mutations[`patch${suffix}`] = (attrs) => {
|
|
33
|
+
setValue((prev) => ({ ...prev, ...attrs }));
|
|
28
34
|
};
|
|
29
|
-
signals[key] = signal;
|
|
30
35
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
36
|
+
mutations.assigns = (attrs) => {
|
|
37
|
+
for (const key in attrs) {
|
|
38
|
+
const suffix = capitalizeFirstLetter(key);
|
|
39
|
+
const value = attrs[key];
|
|
40
|
+
const setValue = _mutations[`set${suffix}`];
|
|
41
|
+
setValue?.(value);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
const createHookEntry = (hookId, refreshView) => {
|
|
45
|
+
hub[hookId] = { dependentFieldKeys: new Set(), refreshView };
|
|
46
|
+
const getterObject = {};
|
|
47
|
+
for (const key in initialState) {
|
|
48
|
+
Object.defineProperty(getterObject, key, {
|
|
49
|
+
get() {
|
|
50
|
+
hub[hookId].dependentFieldKeys.add(key);
|
|
51
|
+
return state[key];
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
const cleanup = () => {
|
|
56
|
+
delete hub[hookId];
|
|
57
|
+
};
|
|
58
|
+
return { getterObject, cleanup };
|
|
59
|
+
};
|
|
60
|
+
const useSnapshot = () => {
|
|
61
|
+
const hookId = useId();
|
|
62
|
+
const [, setRenderObject] = useState({});
|
|
63
|
+
const hookEntry = useMemo(() => createHookEntry(hookId, () => setRenderObject({})), [hookId]);
|
|
64
|
+
useEffect(() => {
|
|
65
|
+
return hookEntry.cleanup;
|
|
66
|
+
}, [hookEntry]);
|
|
67
|
+
return hookEntry.getterObject;
|
|
68
|
+
};
|
|
40
69
|
return {
|
|
41
|
-
state
|
|
42
|
-
snapshot: snapshotGetters,
|
|
43
|
-
useSnapshot: () => snapshotGetters,
|
|
70
|
+
state,
|
|
44
71
|
mutations,
|
|
45
|
-
|
|
72
|
+
useSnapshot,
|
|
73
|
+
get snapshot() {
|
|
74
|
+
return useSnapshot();
|
|
75
|
+
},
|
|
46
76
|
};
|
|
47
77
|
}
|
|
48
78
|
//# sourceMappingURL=store.js.map
|
package/dist/esm/store.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"store.js","sourceRoot":"","sources":["../../src/store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAChC,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"store.js","sourceRoot":"","sources":["../../src/store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAChC,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAC5D,OAAO,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AAGjD,MAAM,UAAU,WAAW,CAAmB,YAAe;IAI3D,MAAM,GAAG,GAGL,EAAE,CAAC;IAEP,MAAM,KAAK,GAAG,YAAY,CAAC;IAC3B,MAAM,SAAS,GAAG,EAAkB,CAAC;IACrC,MAAM,UAAU,GAAG,SAAgB,CAAC;IAEpC,KAAK,MAAM,IAAI,IAAI,YAAY,EAAE,CAAC;QAChC,MAAM,GAAG,GAAG,IAAS,CAAC;QAEtB,MAAM,QAAQ,GAAG,CAAC,GAAyB,EAAE,EAAE;YAC7C,IAAI,KAAQ,CAAC;YACb,IAAI,OAAO,GAAG,KAAK,UAAU,EAAE,CAAC;gBAC9B,KAAK,GAAI,GAAsB,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;YAC9C,CAAC;iBAAM,CAAC;gBACN,KAAK,GAAG,GAAG,CAAC;Y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d.ts
CHANGED
|
@@ -1,18 +1,3 @@
|
|
|
1
|
-
export type SignalListener = () => void;
|
|
2
|
-
export type SetterPayload<T> = T | ((prev: T) => T);
|
|
3
|
-
export type Signal<T> = {
|
|
4
|
-
readonly value: T;
|
|
5
|
-
setValue(arg: SetterPayload<T>): void;
|
|
6
|
-
subscribe(listener: SignalListener): () => void;
|
|
7
|
-
use(): T;
|
|
8
|
-
};
|
|
9
|
-
export type ComputedSignal<T> = {
|
|
10
|
-
readonly value: T;
|
|
11
|
-
subscribe(listener: SignalListener): () => void;
|
|
12
|
-
readonly snapshotValue: T;
|
|
13
|
-
use(): T;
|
|
14
|
-
cleanup(): void;
|
|
15
|
-
};
|
|
16
1
|
export type Mutations<T> = {
|
|
17
2
|
[K in keyof T as `set${Capitalize<K & string>}`]: (value: T[K] | ((prev: T[K]) => T[K])) => void;
|
|
18
3
|
} & {
|
|
@@ -24,11 +9,8 @@ export type Mutations<T> = {
|
|
|
24
9
|
};
|
|
25
10
|
export type Store<T extends object> = {
|
|
26
11
|
state: T;
|
|
27
|
-
snapshot: T;
|
|
28
12
|
useSnapshot(): T;
|
|
13
|
+
snapshot: T;
|
|
29
14
|
mutations: Mutations<T>;
|
|
30
|
-
signals: {
|
|
31
|
-
[K in keyof T]: Signal<T[K]>;
|
|
32
|
-
};
|
|
33
15
|
};
|
|
34
16
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/esm/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI;KACxB,CAAC,IAAI,MAAM,CAAC,IAAI,MAAM,UAAU,CAAC,CAAC,GAAG,MAAM,CAAC,EAAE,GAAG,CAChD,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,KACjC,IAAI;CACV,GAAG;KACD,CAAC,IAAI,MAAM,CAAC,IAAI,UAAU,UAAU,CAAC,CAAC,GAAG,MAAM,CAAC,EAAE,GAAG,CACpD,EAAE,EAAE,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC,KAAK,IAAI,KACtB,IAAI;CACV,GAAG;KACD,CAAC,IAAI,MAAM,CAAC,IAAI,QAAQ,UAAU,CAAC,CAAC,GAAG,MAAM,CAAC,EAAE,GAAG,CAClD,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KACjB,IAAI;CACV,GAAG;IACF,OAAO,EAAE,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC;CACtC,CAAC;AAEF,MAAM,MAAM,KAAK,CAAC,CAAC,SAAS,MAAM,IAAI;IACpC,KAAK,EAAE,CAAC,CAAC;IACT,WAAW,IAAI,CAAC,CAAC;IACjB,QAAQ,EAAE,CAAC,CAAC;IACZ,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC;CACzB,CAAC"}
|