valtio-define 0.2.0 → 0.4.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/README.md +12 -16
- package/dist/index.d.mts +8 -6
- package/dist/index.mjs +10 -6
- package/package.json +7 -3
package/README.md
CHANGED
|
@@ -31,11 +31,11 @@ const store = defineStore({
|
|
|
31
31
|
})
|
|
32
32
|
|
|
33
33
|
function Counter() {
|
|
34
|
-
const
|
|
34
|
+
const { count, increment } = useStore(store)
|
|
35
35
|
return (
|
|
36
36
|
<div>
|
|
37
|
-
<button onClick={
|
|
38
|
-
<div>{
|
|
37
|
+
<button onClick={increment}>Increment</button>
|
|
38
|
+
<div>{count}</div>
|
|
39
39
|
</div>
|
|
40
40
|
)
|
|
41
41
|
}
|
|
@@ -119,19 +119,15 @@ function DataComponent() {
|
|
|
119
119
|
### Persistence
|
|
120
120
|
|
|
121
121
|
```tsx
|
|
122
|
-
const store = defineStore(
|
|
123
|
-
{
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
this.count++
|
|
128
|
-
},
|
|
122
|
+
const store = defineStore({
|
|
123
|
+
state: () => ({ count: 0 }),
|
|
124
|
+
actions: {
|
|
125
|
+
increment() {
|
|
126
|
+
this.count++
|
|
129
127
|
},
|
|
130
128
|
},
|
|
131
|
-
{
|
|
132
|
-
|
|
133
|
-
}
|
|
134
|
-
)
|
|
129
|
+
persist: true // or { key: 'my-store', storage: localStorage, paths: ['count'] }
|
|
130
|
+
})
|
|
135
131
|
```
|
|
136
132
|
|
|
137
133
|
If the persist is a boolean value, it will use `structure-id` to generate a unique key for the store.
|
|
@@ -193,7 +189,7 @@ function App() {
|
|
|
193
189
|
|
|
194
190
|
## API
|
|
195
191
|
|
|
196
|
-
### `defineStore(store
|
|
192
|
+
### `defineStore(store)`
|
|
197
193
|
|
|
198
194
|
Creates a store with state, actions, and getters.
|
|
199
195
|
|
|
@@ -201,7 +197,7 @@ Creates a store with state, actions, and getters.
|
|
|
201
197
|
- `store.state`: Initial state object or factory function
|
|
202
198
|
- `store.actions`: Object containing action methods
|
|
203
199
|
- `store.getters`: Object containing getter methods
|
|
204
|
-
- `
|
|
200
|
+
- `store.persist`: Persistence configuration (boolean or object)
|
|
205
201
|
|
|
206
202
|
**Returns:** Store instance with reactive state and actions
|
|
207
203
|
|
package/dist/index.d.mts
CHANGED
|
@@ -16,8 +16,8 @@ interface StoreDefine<S extends object, A extends Actions<S>, G extends Getters<
|
|
|
16
16
|
actions?: A;
|
|
17
17
|
getters?: G;
|
|
18
18
|
}
|
|
19
|
-
interface StoreOptions {
|
|
20
|
-
persist?: boolean | PersistentOptions
|
|
19
|
+
interface StoreOptions<S extends object = Record<string, unknown>> {
|
|
20
|
+
persist?: boolean | PersistentOptions<S>;
|
|
21
21
|
}
|
|
22
22
|
interface StoreSignal<S, A extends Actions<S>, G extends Getters<S>> {
|
|
23
23
|
<T>(fn: (state: S & GettersReturnType<G>) => T): T;
|
|
@@ -26,6 +26,7 @@ interface StoreSignal<S, A extends Actions<S>, G extends Getters<S>> {
|
|
|
26
26
|
interface StoreSubscribe<S, A extends Actions<S>, G extends Getters<S>> {
|
|
27
27
|
(listener: (state: S & GettersReturnType<G>) => void): () => void;
|
|
28
28
|
status: (listener: (status: ActionsStatus<A>) => void) => () => void;
|
|
29
|
+
key: <K$1 extends keyof S | keyof G>(key: K$1, listener: (state: (S & GettersReturnType<G>)[K$1]) => void) => () => void;
|
|
29
30
|
}
|
|
30
31
|
interface StorePatch<S, G extends Getters<S>> {
|
|
31
32
|
(patch: Partial<S> | ((state: S & GettersReturnType<G>) => void)): void;
|
|
@@ -39,10 +40,11 @@ type Store<S, A extends Actions<S>, G extends Getters<S>> = {
|
|
|
39
40
|
$status: ActionsStatus<A>;
|
|
40
41
|
$signal: StoreSignal<S, A, G>;
|
|
41
42
|
} & ActionsOmitThisParameter<A>;
|
|
42
|
-
interface PersistentOptions {
|
|
43
|
+
interface PersistentOptions<S extends object = Record<string, unknown>> {
|
|
43
44
|
key?: string;
|
|
44
|
-
storage?: Storage
|
|
45
|
-
paths?:
|
|
45
|
+
storage?: Partial<Storage> & Pick<Storage, 'getItem' | 'setItem'>;
|
|
46
|
+
paths?: (keyof S)[];
|
|
47
|
+
initial?: (initialState: S) => any | Promise<any>;
|
|
46
48
|
}
|
|
47
49
|
//#endregion
|
|
48
50
|
//#region src/define-store.d.ts
|
|
@@ -74,7 +76,7 @@ interface PersistentOptions {
|
|
|
74
76
|
*
|
|
75
77
|
* ```
|
|
76
78
|
*/
|
|
77
|
-
declare function defineStore<S extends object, A extends Actions<S>, G extends Getters<S>>(store: StoreDefine<S, A, G> & StoreOptions): Store<S, A, G>;
|
|
79
|
+
declare function defineStore<S extends object, A extends Actions<S>, G extends Getters<S>>(store: StoreDefine<S, A, G> & StoreOptions<S>): Store<S, A, G>;
|
|
78
80
|
//#endregion
|
|
79
81
|
//#region src/persistent.d.ts
|
|
80
82
|
declare function proxyWithPersistent<T extends object>(initialObject: T, options?: PersistentOptions): T;
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createElement } from "react";
|
|
2
2
|
import { proxy, ref, subscribe, useSnapshot } from "valtio";
|
|
3
|
-
import {
|
|
3
|
+
import { subscribeKey } from "valtio/utils";
|
|
4
|
+
import { tryParseJson } from "@hairy/utils";
|
|
4
5
|
import { generateStructureId } from "structure-id";
|
|
5
6
|
|
|
6
7
|
//#region src/utils/index.ts
|
|
@@ -48,7 +49,7 @@ function set(obj, path, value) {
|
|
|
48
49
|
function proxyWithPersistent(initialObject, options = {}) {
|
|
49
50
|
options.key = options.key || generateStructureId(initialObject);
|
|
50
51
|
const storage = options.storage || (typeof localStorage !== "undefined" ? localStorage : void 0);
|
|
51
|
-
const state = proxy(
|
|
52
|
+
const state = proxy(tryParseJson(storage?.getItem(options.key)) || initialObject);
|
|
52
53
|
subscribe(state, () => {
|
|
53
54
|
const paths = options.paths || Object.keys(state);
|
|
54
55
|
const statePaths = {};
|
|
@@ -101,7 +102,7 @@ function defineStore(store) {
|
|
|
101
102
|
const $actions = {};
|
|
102
103
|
const $getters = {};
|
|
103
104
|
setupActions($state, actions, $actions, $status);
|
|
104
|
-
setupGetters(
|
|
105
|
+
setupGetters($state, getters, $getters);
|
|
105
106
|
setupStatus($actions, $status);
|
|
106
107
|
function $subscribe(listener) {
|
|
107
108
|
return subscribe($state, () => listener($state));
|
|
@@ -109,6 +110,9 @@ function defineStore(store) {
|
|
|
109
110
|
$subscribe.status = function(listener) {
|
|
110
111
|
return subscribe($status, () => listener($status));
|
|
111
112
|
};
|
|
113
|
+
$subscribe.key = function(key, listener) {
|
|
114
|
+
return subscribeKey($state, key, () => listener($state));
|
|
115
|
+
};
|
|
112
116
|
function $patch(patch) {
|
|
113
117
|
if (typeof patch === "function") patch($state);
|
|
114
118
|
else Object.assign($state, patch);
|
|
@@ -144,13 +148,13 @@ function setupActions($state, actions, $actions, $status) {
|
|
|
144
148
|
});
|
|
145
149
|
}
|
|
146
150
|
}
|
|
147
|
-
function setupGetters(
|
|
151
|
+
function setupGetters($state, getters, $getters) {
|
|
148
152
|
for (const key in getters) {
|
|
149
153
|
Object.defineProperty($getters, key, {
|
|
150
|
-
get: () => state[key],
|
|
154
|
+
get: () => $state[key],
|
|
151
155
|
enumerable: true
|
|
152
156
|
});
|
|
153
|
-
Object.defineProperty(state, key, {
|
|
157
|
+
Object.defineProperty($state, key, {
|
|
154
158
|
get: () => getters[key].call($state),
|
|
155
159
|
enumerable: true
|
|
156
160
|
});
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "valtio-define",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.4.0",
|
|
5
5
|
"description": "⚡quickly create a fully functional and robust Valtio factory",
|
|
6
6
|
"author": "Hairyf <wwu710632@gmail.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"react": "^18.2.0"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@hairy/utils": "^1.
|
|
32
|
+
"@hairy/utils": "^1.46.0",
|
|
33
33
|
"structure-id": "^1.2.9",
|
|
34
34
|
"valtio": "^2.2.0"
|
|
35
35
|
},
|
|
@@ -40,9 +40,12 @@
|
|
|
40
40
|
"@types/node": "^24.10.0",
|
|
41
41
|
"@types/react": "^19.2.6",
|
|
42
42
|
"@vitejs/plugin-react": "^5.1.1",
|
|
43
|
+
"@vitest/browser-playwright": "^4.0.15",
|
|
44
|
+
"@vitest/coverage-v8": "^4.0.15",
|
|
43
45
|
"bumpp": "^10.3.1",
|
|
44
46
|
"eslint": "^9.39.1",
|
|
45
47
|
"lint-staged": "^16.2.6",
|
|
48
|
+
"playwright": "^1.57.0",
|
|
46
49
|
"react": "^19.2.0",
|
|
47
50
|
"simple-git-hooks": "^2.13.1",
|
|
48
51
|
"tinyexec": "^1.0.2",
|
|
@@ -50,7 +53,8 @@
|
|
|
50
53
|
"tsx": "^4.20.6",
|
|
51
54
|
"typescript": "^5.9.3",
|
|
52
55
|
"vite": "^7.2.1",
|
|
53
|
-
"vitest": "^4.0.
|
|
56
|
+
"vitest": "^4.0.15",
|
|
57
|
+
"vitest-browser-react": "^2.0.2",
|
|
54
58
|
"vitest-package-exports": "^0.1.1",
|
|
55
59
|
"yaml": "^2.8.1"
|
|
56
60
|
},
|