snap-store 0.1.0 → 0.1.2
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 +53 -77
- 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 +68 -38
- package/dist/esm/store.js.map +1 -1
- package/dist/esm/types.d.ts +3 -22
- package/dist/esm/types.d.ts.map +1 -1
- package/package.json +2 -2
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,9 +36,8 @@ import {createStore} from "snap-store"
|
|
|
24
36
|
const store = createStore({ count: 0});
|
|
25
37
|
|
|
26
38
|
const Counter = () => {
|
|
27
|
-
const { count } = store.
|
|
28
|
-
|
|
29
|
-
return <button onClick={() => setCount(prev => prev + 1)}>
|
|
39
|
+
const { count } = store.useSnapshot();
|
|
40
|
+
return <button onClick={() => store.setCount(prev => prev + 1)}>
|
|
30
41
|
{count}
|
|
31
42
|
</button>
|
|
32
43
|
}
|
|
@@ -37,43 +48,43 @@ const Counter = () => {
|
|
|
37
48
|
const store = createStore({ count: 0});
|
|
38
49
|
```
|
|
39
50
|
`createStore` takes an initial state object and returns a store.
|
|
40
|
-
The store holds wrapper signals for each field of the state object.
|
|
41
51
|
```ts
|
|
42
|
-
const { count } = store.
|
|
52
|
+
const { count } = store.useSnapshot();
|
|
43
53
|
```
|
|
44
|
-
In a component, `
|
|
54
|
+
In a component, `useSnapshot()` hook is used to refer to the states and make them reactive.
|
|
45
55
|
For this line, `count` is actually a getter and it registers a listener to track the value change.
|
|
46
56
|
|
|
47
|
-
##
|
|
57
|
+
## Examples
|
|
48
58
|
```ts
|
|
49
59
|
const store = createStore({ count: 0});
|
|
50
60
|
|
|
51
61
|
function handleButton(){
|
|
52
62
|
const { count } = store.state; //read store state
|
|
53
|
-
store.
|
|
54
|
-
store.
|
|
63
|
+
store.setCount(count + 1); //mutate by value
|
|
64
|
+
store.setCount(prev => prev + 1); //mutate by function
|
|
55
65
|
}
|
|
56
66
|
|
|
57
67
|
const Component = () => {
|
|
58
|
-
const { count } = store.
|
|
68
|
+
const { count } = store.useSnapshot(); //refer store state as reactive
|
|
59
69
|
return <button onClick={handleButton}>push me {count}</button>
|
|
60
70
|
}
|
|
61
71
|
```
|
|
62
|
-
In the component, `store.
|
|
72
|
+
In the component, `store.useSnapshot()` is used to refer to the store state as a reactive value.
|
|
63
73
|
|
|
64
74
|
Since this is a global state library, you can also read and write store states outside components.
|
|
65
75
|
`store.state` is used to read the value in non-component functions.
|
|
66
76
|
|
|
67
|
-
|
|
77
|
+
Mutation methods have no difference in component or non-component context.
|
|
68
78
|
|
|
69
79
|
```ts
|
|
70
80
|
const store = createStore({ user: {name: "John", age: 20 }});
|
|
71
|
-
store.
|
|
72
|
-
store.
|
|
73
|
-
store.
|
|
74
|
-
store.
|
|
81
|
+
store.setUser({ name: "Mike", age: 20}); //value
|
|
82
|
+
store.setUser(prev => ({...prev, age: 21})); //by function
|
|
83
|
+
store.patchUser({ age: 22}); //partial update (merged)
|
|
84
|
+
store.produceUser(draft => { draft.age = 23 }) //update with immer
|
|
75
85
|
```
|
|
76
|
-
|
|
86
|
+
|
|
87
|
+
`store` object has a set of update methods for each field.
|
|
77
88
|
|
|
78
89
|
`set*` methods are similar to the setter function of `useState`. It takes a value or a function.
|
|
79
90
|
|
|
@@ -87,32 +98,31 @@ const store = createStore({
|
|
|
87
98
|
penColor: 'black',
|
|
88
99
|
penStyle: 'normal'
|
|
89
100
|
});
|
|
90
|
-
store.
|
|
101
|
+
store.assigns({ penWidth: 1, penStyle: 'dashed' });
|
|
91
102
|
//is equivalent to
|
|
92
|
-
store.
|
|
93
|
-
store.
|
|
103
|
+
store.setPenWidth(1);
|
|
104
|
+
store.setPenStyle('dashed');
|
|
94
105
|
```
|
|
95
|
-
In
|
|
106
|
+
In store, 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.
|
|
105
|
-
const { setTheme } = store.mutations;
|
|
115
|
+
const { theme } = store.useSnapshot();
|
|
106
116
|
return <div>
|
|
107
117
|
<IconButton
|
|
108
118
|
icon="☀️"
|
|
109
119
|
active={theme === 'light'}
|
|
110
|
-
onClick={() => setTheme("light")}
|
|
120
|
+
onClick={() => store.setTheme("light")}
|
|
111
121
|
/>
|
|
112
122
|
<IconButton
|
|
113
123
|
icon="🌙"
|
|
114
124
|
active={theme === 'dark'}
|
|
115
|
-
onClick={() => setTheme("dark")}
|
|
125
|
+
onClick={() => store.setTheme("dark")}
|
|
116
126
|
/>
|
|
117
127
|
</div>
|
|
118
128
|
}
|
|
@@ -126,18 +136,17 @@ const store = createStore<{textSize: number, bgColor: string}>({
|
|
|
126
136
|
})
|
|
127
137
|
|
|
128
138
|
const BookReaderSettings = () => {
|
|
129
|
-
const snap = store.
|
|
130
|
-
const mut = store.mutations;
|
|
139
|
+
const snap = store.useSnapshot();;
|
|
131
140
|
return <div>
|
|
132
141
|
<Slider
|
|
133
142
|
value={snap.textSize}
|
|
134
|
-
onChange={
|
|
143
|
+
onChange={store.setTextSize}
|
|
135
144
|
min={10}
|
|
136
145
|
max={20}
|
|
137
146
|
/>
|
|
138
147
|
<ColorInput
|
|
139
148
|
value={snap.bgColor}
|
|
140
|
-
onChange={
|
|
149
|
+
onChange={store.setBgColor}
|
|
141
150
|
/>
|
|
142
151
|
</div>
|
|
143
152
|
}
|
|
@@ -151,60 +160,27 @@ const store = createStore({ name: "Mike", age: 20 });
|
|
|
151
160
|
|
|
152
161
|
//wrong code
|
|
153
162
|
const Component = () => {
|
|
154
|
-
const snap = store.
|
|
163
|
+
const snap = store.useSnapshot();;
|
|
155
164
|
if(snap.age < 20) return; //bad early return
|
|
156
165
|
return <div>Hello Gentleman, {snap.name}</div>
|
|
157
166
|
}
|
|
158
167
|
|
|
159
168
|
//working code
|
|
160
169
|
const Component = () => {
|
|
161
|
-
const { age, name } = store.
|
|
170
|
+
const { age, name } = store.useSnapshot();;
|
|
162
171
|
if(age < 20) return; //no problem
|
|
163
172
|
return <div>Hello Gentleman, {name}</div>
|
|
164
173
|
}
|
|
165
174
|
```
|
|
166
|
-
Each member of the snapshot object is a getter and it calls a hooks
|
|
175
|
+
Each member of the snapshot object is a getter and it calls a hooks internally.
|
|
167
176
|
|
|
168
177
|
Since a component must have the same hooks count for each render, non-destructive assign and early return are a bad combination.
|
|
169
178
|
|
|
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.
|
|
179
|
+
The snapshot object should be destructured if you have an early return.
|
|
205
180
|
|
|
206
181
|
## License
|
|
207
182
|
|
|
208
183
|
MIT License
|
|
209
184
|
|
|
210
185
|
|
|
186
|
+
|
|
@@ -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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
70
|
+
state,
|
|
71
|
+
useSnapshot,
|
|
72
|
+
get snapshot() {
|
|
73
|
+
return useSnapshot();
|
|
74
|
+
},
|
|
75
|
+
...mutations,
|
|
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,GA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E,CAAC,SAAS,CAAC,CAAC,CAAC;QAChB,OAAO,SAAS,CAAC,YAAY,CAAC;IAChC,CAAC,CAAC;IAEF,OAAO;QACL,KAAK;QACL,WAAW;QACX,IAAI,QAAQ;YACV,OAAO,WAAW,EAAE,CAAC;QACvB,CAAC;QACD,GAAG,SAAS;KACb,CAAC;AACJ,CAAC"}
|
package/dist/esm/types.d.ts
CHANGED
|
@@ -1,34 +1,15 @@
|
|
|
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
|
} & {
|
|
19
4
|
[K in keyof T as `produce${Capitalize<K & string>}`]: (fn: (draft: T[K]) => void) => void;
|
|
20
5
|
} & {
|
|
21
|
-
[K in keyof T as `patch${Capitalize<K & string>}`]: (attrs: Partial<T[K]
|
|
6
|
+
[K in keyof T as `patch${Capitalize<K & string>}`]: (attrs: Partial<Extract<T[K], object>>) => void;
|
|
22
7
|
} & {
|
|
23
8
|
assigns: (attrs: Partial<T>) => void;
|
|
24
9
|
};
|
|
25
10
|
export type Store<T extends object> = {
|
|
26
11
|
state: T;
|
|
27
|
-
snapshot: T;
|
|
28
12
|
useSnapshot(): T;
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
[K in keyof T]: Signal<T[K]>;
|
|
32
|
-
};
|
|
33
|
-
};
|
|
13
|
+
snapshot: T;
|
|
14
|
+
} & Mutations<T>;
|
|
34
15
|
//# 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,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,KAClC,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;CACb,GAAG,SAAS,CAAC,CAAC,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "snap-store",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "An easy-to-use global state management library for React.",
|
|
5
5
|
"author": "yahiro",
|
|
6
6
|
"license": "MIT",
|
|
@@ -53,4 +53,4 @@
|
|
|
53
53
|
"engines": {
|
|
54
54
|
"node": ">=16.0.0"
|
|
55
55
|
}
|
|
56
|
-
}
|
|
56
|
+
}
|