valtio-define 1.2.1 → 1.3.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 +22 -2
- package/dist/index.d.mts +6 -3
- package/dist/index.mjs +1 -1
- package/dist/plugins/index.d.mts +2 -1
- package/dist/plugins/index.mjs +1 -1
- package/dist/plugins/persist/index.d.mts +2 -2
- package/dist/plugins/signal/index.d.mts +10 -0
- package/dist/plugins/signal/index.mjs +1 -0
- package/dist/plugins/signal/jsx-dev-runtime.d.mts +2 -0
- package/dist/plugins/signal/jsx-dev-runtime.mjs +1 -0
- package/dist/plugins/signal/jsx-runtime.d.mts +11 -0
- package/dist/plugins/signal/jsx-runtime.mjs +1 -0
- package/dist/plugins/signal/types.d.mts +12 -0
- package/dist/plugins/signal/types.mjs +1 -0
- package/dist/{types-CqDu3QIJ.d.mts → types-Cu8iJxyS.d.mts} +6 -6
- package/package.json +8 -2
package/README.md
CHANGED
|
@@ -119,7 +119,7 @@ Save and restore your store state using the `persist` plugin.
|
|
|
119
119
|
|
|
120
120
|
```tsx
|
|
121
121
|
import valtio from 'valtio-define'
|
|
122
|
-
import { persist } from 'valtio-define/plugins'
|
|
122
|
+
import { persist } from 'valtio-define/plugins/persist'
|
|
123
123
|
|
|
124
124
|
valtio.use(persist())
|
|
125
125
|
```
|
|
@@ -202,10 +202,30 @@ Every store instance created with `defineStore` includes built-in utility method
|
|
|
202
202
|
* **`$patch(obj | fn)`**: Bulk update the state.
|
|
203
203
|
* **`$subscribe(callback)`**: Watch the entire store for changes.
|
|
204
204
|
* **`$subscribeKey(key, callback)`**: Watch a specific property.
|
|
205
|
-
* **`$signal(selector)`**: Use a selector function
|
|
205
|
+
* **`$signal(selector)`**: Use a selector function to create a signal.
|
|
206
206
|
|
|
207
207
|
-----
|
|
208
208
|
|
|
209
|
+
### 📡 Signal
|
|
210
|
+
|
|
211
|
+
> if you want to use the signal plugin, you need to import it and use it in your store.
|
|
212
|
+
```tsx
|
|
213
|
+
import { valtio } from 'valtio-define'
|
|
214
|
+
import { signal } from 'valtio-define/plugins/signal'
|
|
215
|
+
|
|
216
|
+
valtio.use(signal())
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
And add jsxImportSource at the beginning of your `.tsx` file
|
|
220
|
+
|
|
221
|
+
```tsx
|
|
222
|
+
/** @jsxImportSource valtio-signal */
|
|
223
|
+
|
|
224
|
+
function App() {
|
|
225
|
+
return <div>{store.$signal(state => state.count)}</div>
|
|
226
|
+
}
|
|
227
|
+
```
|
|
228
|
+
|
|
209
229
|
## 🔌 Plugins
|
|
210
230
|
|
|
211
231
|
### Global vs. Per-Store
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as GettersReturnType, c as
|
|
1
|
+
import { _ as SubscribeKey, a as GettersReturnType, c as Path, d as Signal, f as Store, g as Subscribe, h as StoreOptions, i as Getters, l as Plugin, m as StoreDefineOptions, n as ActionsOmitThisParameter, o as Op, p as StoreDefine, r as ActionsTree, s as Patch, t as Actions, u as PluginContext } from "./types-Cu8iJxyS.mjs";
|
|
2
2
|
import { Snapshot } from "valtio";
|
|
3
3
|
import { Dispatch, SetStateAction } from "react";
|
|
4
4
|
|
|
@@ -35,12 +35,15 @@ declare function use(plugin: Plugin): void;
|
|
|
35
35
|
*
|
|
36
36
|
* ```
|
|
37
37
|
*/
|
|
38
|
-
declare function defineStore<S extends object = {}, A extends Actions<S> = {}, G extends Getters<S> = {}>(
|
|
38
|
+
declare function defineStore<S extends object = {}, A extends Actions<S> = {}, G extends Getters<S> = {}>(options: StoreDefine<S, A, G>): Store<S, A, G>;
|
|
39
39
|
//#endregion
|
|
40
40
|
//#region src/hooks.d.ts
|
|
41
41
|
declare function useStore<S extends object, A extends Actions<S>, G extends Getters<S>>(store: Store<S, A, G>, options?: {
|
|
42
42
|
sync?: boolean;
|
|
43
43
|
}): Snapshot<S & GettersReturnType<G> & A>;
|
|
44
|
+
declare function useGetters<S extends object, G extends Getters<S>>(store: Store<S, any, G>, options?: {
|
|
45
|
+
sync?: boolean;
|
|
46
|
+
}): GettersReturnType<G>;
|
|
44
47
|
//#endregion
|
|
45
48
|
//#region src/utils.d.ts
|
|
46
49
|
declare function storeToState<S extends object, K extends keyof S>(store: Store<S>, key: K, options?: {
|
|
@@ -55,4 +58,4 @@ declare const _default: {
|
|
|
55
58
|
use: typeof use;
|
|
56
59
|
};
|
|
57
60
|
//#endregion
|
|
58
|
-
export { Actions, ActionsOmitThisParameter, ActionsTree, Getters, GettersReturnType, Patch, Plugin, PluginContext, Signal, Store, StoreDefine, StoreDefineOptions, StoreOptions, Subscribe, SubscribeKey, _default as default, defineStore, plugins, storeToState, storeToStates, use, useStore };
|
|
61
|
+
export { Actions, ActionsOmitThisParameter, ActionsTree, Getters, GettersReturnType, Op, Patch, Path, Plugin, PluginContext, Signal, Store, StoreDefine, StoreDefineOptions, StoreOptions, Subscribe, SubscribeKey, _default as default, defineStore, plugins, storeToState, storeToStates, use, useGetters, useStore };
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{proxy as e,
|
|
1
|
+
import{proxy as e,useSnapshot as t}from"valtio";import{computed as n}from"valtio-reactive";import{$ as r}from"valtio-signal";import{subscribeKey as i}from"valtio/utils";import{proxy as a,ref as o,subscribe as s}from"valtio/vanilla";const c=e([]);function l(e){c.push(e)}function u(e){let t=typeof e.state==`function`?e.state():e.state,l=e.getters||{},u=e.actions||{},p=a(t),m,h={},g=new WeakSet,_=n(f(l,p));for(let e of Object.keys(_))d(p,e,()=>_[e],{enumerable:!1});for(let e in u)h[e]=o(u[e].bind(p)),d(p,e,()=>h[e],{enumerable:!1});function v(e){return s(p,t=>e(p,t))}function y(e,t){return i(p,e,e=>t(e),!0)}function b(e){typeof e==`function`?e(p):Object.assign(p,e)}function x(e){return e(r(p))}function S(){m?.()}function C(e){E(e)}let w={$subscribe:v,$subscribeKey:y,$patch:b,$state:p,$actions:h,$getters:_,$dispose:S,$signal:x,use:C},T=new Proxy(w,{get(e,t){return t in h?h[t]:t in e?e[t]:p[t]},has(e,t){return t in e||t in h||t in p},set(e,t,n){return t in p?p[t]=n:e[t]=n,!0}});function E(t){g.has(t)||(g.add(t),t({store:T,options:e}))}for(let e of c)E(e);return m=s(c,()=>{for(let e of c)E(e)}),T}function d(e,t,n,r){Object.defineProperty(e,t,{get:n,enumerable:!0,...r})}function f(e,t){let n={};for(let r in e)n[r]=e[r].bind(t);return n}function p(e,n){return t(e.$state,n)}function m(e,n){return t(e.$getters,n)}function h(e,t,n){let r=p(e,n);function i(n){typeof n==`function`?e.$patch(e=>{e[t]=n(e[t])}):e.$patch(e=>{e[t]=n})}return[r[t],i]}function g(e,t){return Object.fromEntries(Object.keys(e.$state).map(n=>[n,h(e,n,t)]))}var _={use:l};export{_ as default,u as defineStore,c as plugins,h as storeToState,g as storeToStates,l as use,m as useGetters,p as useStore};
|
package/dist/plugins/index.d.mts
CHANGED
package/dist/plugins/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{persist as e}from"./persist/index.mjs";export{e as persist};
|
|
1
|
+
import{persist as e}from"./persist/index.mjs";import{signal as t}from"./signal/index.mjs";export{e as persist,t as signal};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { l as Plugin } from "../../types-Cu8iJxyS.mjs";
|
|
2
2
|
import { PersistentOptions, PersistentStore } from "./types.mjs";
|
|
3
3
|
|
|
4
4
|
//#region src/plugins/persist/index.d.ts
|
|
@@ -15,7 +15,7 @@ declare module 'valtio-define' {
|
|
|
15
15
|
interface StoreDefineOptions<S extends object> {
|
|
16
16
|
persist?: PersistentOptions<S> | boolean;
|
|
17
17
|
}
|
|
18
|
-
interface StoreOptions extends PersistentStore {}
|
|
18
|
+
interface StoreOptions<S, A, G> extends PersistentStore {}
|
|
19
19
|
}
|
|
20
20
|
//#endregion
|
|
21
21
|
export { PersistentMountOptions, persist };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { i as Getters, l as Plugin } from "../../types-Cu8iJxyS.mjs";
|
|
2
|
+
import { SignalStore } from "./types.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/plugins/signal/index.d.ts
|
|
5
|
+
declare function signal(): Plugin;
|
|
6
|
+
declare module 'valtio-define' {
|
|
7
|
+
interface StoreOptions<S, A, G extends Getters<S>> extends SignalStore<S, G> {}
|
|
8
|
+
}
|
|
9
|
+
//#endregion
|
|
10
|
+
export { signal };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{$ as e}from"valtio-signal";function t(){return t=>{function n(n){let r=e(t.store.$state);return n?n(r):r}t.store.$signal=n}}export{t as signal};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{Fragment as e,jsx as t,jsxDEV as n,jsxs as r}from"./jsx-runtime.mjs";export{e as Fragment,t as jsx,n as jsxDEV,r as jsxs};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Fragment, jsx, jsxDEV, jsxs } from "valtio-signal/jsx-runtime";
|
|
2
|
+
|
|
3
|
+
//#region src/plugins/signal/jsx-runtime.d.ts
|
|
4
|
+
declare global {
|
|
5
|
+
namespace JSX {
|
|
6
|
+
interface IntrinsicElements extends React.JSX.IntrinsicElements {}
|
|
7
|
+
interface Element extends React.ReactElement<any, any> {}
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
//#endregion
|
|
11
|
+
export { Fragment, jsx, jsxDEV, jsxs };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{Fragment as e,jsx as t,jsxDEV as n,jsxs as r}from"valtio-signal/jsx-runtime";export{e as Fragment,t as jsx,n as jsxDEV,r as jsxs};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { a as GettersReturnType, i as Getters } from "../../types-Cu8iJxyS.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/plugins/signal/types.d.ts
|
|
4
|
+
interface Signal<S> {
|
|
5
|
+
(): S;
|
|
6
|
+
<T>(fn: (state: S) => T): T;
|
|
7
|
+
}
|
|
8
|
+
interface SignalStore<S, G extends Getters<S>> {
|
|
9
|
+
$signal: Signal<S & GettersReturnType<G>>;
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
12
|
+
export { Signal, SignalStore };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export{};
|
|
@@ -18,7 +18,7 @@ interface Signal<S, G extends Getters<S>> {
|
|
|
18
18
|
type Path = (string | symbol)[];
|
|
19
19
|
type Op = [op: 'set', path: Path, value: unknown, prevValue: unknown] | [op: 'delete', path: Path, prevValue: unknown];
|
|
20
20
|
interface Subscribe<S, G extends Getters<S>> {
|
|
21
|
-
(listener: (state: S & GettersReturnType<G>, opts: Op) => void): () => void;
|
|
21
|
+
(listener: (state: S & GettersReturnType<G>, opts: Op[]) => void): () => void;
|
|
22
22
|
}
|
|
23
23
|
interface SubscribeKey<S, G extends Getters<S>> {
|
|
24
24
|
<T extends keyof S | keyof G>(key: T, listener: (state: (S & GettersReturnType<G>)[T]) => void): () => void;
|
|
@@ -26,17 +26,17 @@ interface SubscribeKey<S, G extends Getters<S>> {
|
|
|
26
26
|
interface Patch<S, G extends Getters<S>> {
|
|
27
27
|
(patch: Partial<S> | ((state: S & GettersReturnType<G>) => void)): void;
|
|
28
28
|
}
|
|
29
|
-
interface StoreOptions {}
|
|
29
|
+
interface StoreOptions<S, A, G> {}
|
|
30
30
|
type Store<S, A extends Actions<S> = {}, G extends Getters<S> = {}> = {
|
|
31
31
|
$subscribe: Subscribe<S, G>;
|
|
32
32
|
$subscribeKey: SubscribeKey<S, G>;
|
|
33
33
|
$patch: Patch<S, G>;
|
|
34
|
-
$state: S
|
|
34
|
+
$state: S;
|
|
35
35
|
$actions: ActionsOmitThisParameter<A>;
|
|
36
36
|
$getters: GettersReturnType<G>;
|
|
37
|
+
$dispose: () => void;
|
|
37
38
|
use: (plugin: Plugin) => void;
|
|
38
|
-
|
|
39
|
-
} & S & GettersReturnType<G> & ActionsOmitThisParameter<A> & StoreOptions;
|
|
39
|
+
} & S & GettersReturnType<G> & ActionsOmitThisParameter<A> & StoreOptions<S, A, G>;
|
|
40
40
|
interface PluginContext<S extends object = Record<string, unknown>> {
|
|
41
41
|
store: Store<S, Actions<S>, Getters<S>>;
|
|
42
42
|
options: StoreDefine<S, ActionsTree, Getters<S>>;
|
|
@@ -45,4 +45,4 @@ interface Plugin {
|
|
|
45
45
|
<S extends object = Record<string, unknown>>(context: PluginContext<S>): void;
|
|
46
46
|
}
|
|
47
47
|
//#endregion
|
|
48
|
-
export { GettersReturnType as a,
|
|
48
|
+
export { SubscribeKey as _, GettersReturnType as a, Path as c, Signal as d, Store as f, Subscribe as g, StoreOptions as h, Getters as i, Plugin as l, StoreDefineOptions as m, ActionsOmitThisParameter as n, Op as o, StoreDefine as p, ActionsTree as r, Patch as s, Actions as t, PluginContext as u };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "valtio-define",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.3.0",
|
|
5
5
|
"description": "⚡quickly create a fully functional and robust Valtio factory",
|
|
6
6
|
"author": "Hairyf <wwu710632@gmail.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -26,6 +26,10 @@
|
|
|
26
26
|
"./plugins": "./dist/plugins/index.mjs",
|
|
27
27
|
"./plugins/persist": "./dist/plugins/persist/index.mjs",
|
|
28
28
|
"./plugins/persist/types": "./dist/plugins/persist/types.mjs",
|
|
29
|
+
"./plugins/signal": "./dist/plugins/signal/index.mjs",
|
|
30
|
+
"./plugins/signal/jsx-dev-runtime": "./dist/plugins/signal/jsx-dev-runtime.mjs",
|
|
31
|
+
"./plugins/signal/jsx-runtime": "./dist/plugins/signal/jsx-runtime.mjs",
|
|
32
|
+
"./plugins/signal/types": "./dist/plugins/signal/types.mjs",
|
|
29
33
|
"./package.json": "./package.json"
|
|
30
34
|
},
|
|
31
35
|
"files": [
|
|
@@ -38,7 +42,9 @@
|
|
|
38
42
|
"@hairy/utils": "^1.47.0",
|
|
39
43
|
"destr": "^2.0.5",
|
|
40
44
|
"structure-id": "^1.2.9",
|
|
41
|
-
"valtio": "^2.3.0"
|
|
45
|
+
"valtio": "^2.3.0",
|
|
46
|
+
"valtio-reactive": "^0.2.0",
|
|
47
|
+
"valtio-signal": "^0.6.0"
|
|
42
48
|
},
|
|
43
49
|
"devDependencies": {
|
|
44
50
|
"@antfu/eslint-config": "^7.6.1",
|