valtio-define 1.2.1 β 1.3.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 +28 -3
- 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/persist/index.mjs +1 -1
- 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-DD1IhWyl.d.mts} +8 -8
- package/package.json +9 -3
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
## π¦ Installation
|
|
13
13
|
|
|
14
14
|
```bash
|
|
15
|
-
pnpm add valtio-define
|
|
15
|
+
pnpm add valtio valtio-define
|
|
16
16
|
```
|
|
17
17
|
|
|
18
18
|
<details>
|
|
@@ -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)`**:
|
|
205
|
+
* **`$signal(selector)`**: Render reactive values inline in JSX (works best with the Signal plugin + `@jsxImportSource`).
|
|
206
206
|
|
|
207
207
|
-----
|
|
208
208
|
|
|
209
|
+
### π‘ Signal
|
|
210
|
+
|
|
211
|
+
Register the Signal plugin globally:
|
|
212
|
+
```tsx
|
|
213
|
+
import valtio from 'valtio-define'
|
|
214
|
+
import { signal } from 'valtio-define/plugins/signal'
|
|
215
|
+
|
|
216
|
+
valtio.use(signal())
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
Add `jsxImportSource` at the beginning of your `.tsx` file:
|
|
220
|
+
|
|
221
|
+
```tsx
|
|
222
|
+
/** @jsxImportSource valtio-define/plugins/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
|
|
@@ -213,6 +233,8 @@ Every store instance created with `defineStore` includes built-in utility method
|
|
|
213
233
|
Plugins can be applied to all stores or restricted to a single instance.
|
|
214
234
|
|
|
215
235
|
```tsx
|
|
236
|
+
import valtio, { defineStore } from 'valtio-define'
|
|
237
|
+
|
|
216
238
|
// Global
|
|
217
239
|
valtio.use(myPlugin())
|
|
218
240
|
|
|
@@ -227,6 +249,7 @@ Extend functionality by accessing the `store` instance and `options` through the
|
|
|
227
249
|
|
|
228
250
|
```tsx
|
|
229
251
|
import type { Plugin } from 'valtio-define'
|
|
252
|
+
import valtio from 'valtio-define'
|
|
230
253
|
|
|
231
254
|
function loggerPlugin(): Plugin {
|
|
232
255
|
return ({ store, options }) => {
|
|
@@ -236,6 +259,8 @@ function loggerPlugin(): Plugin {
|
|
|
236
259
|
}
|
|
237
260
|
}
|
|
238
261
|
|
|
262
|
+
valtio.use(loggerPlugin())
|
|
263
|
+
|
|
239
264
|
declare module 'valtio-define' {
|
|
240
265
|
export interface StoreDefineOptions<S extends object> {
|
|
241
266
|
$myPlugin?: {
|
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-DD1IhWyl.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{batch as n,computed as r}from"valtio-reactive";import{$ as i}from"valtio-signal";import{subscribeKey as a}from"valtio/utils";import{proxy as o,ref as s,subscribe as c}from"valtio/vanilla";const l=e([]);function u(e){l.push(e)}function d(e){let t=typeof e.state==`function`?e.state():e.state,u=e.getters||{},d=e.actions||{},m=o(t),h,g={},_=new WeakSet,v=r(p(u,m));for(let e of Object.keys(v))f(m,e,()=>v[e],{enumerable:!1});for(let e of Object.keys(d))g[e]=s(d[e].bind(m)),f(m,e,()=>g[e],{enumerable:!1});function y(e){return c(m,t=>e(m,t))}function b(e,t){return a(m,e,e=>t(e),!0)}function x(e){typeof e==`function`?n(()=>e(m)):Object.assign(m,e)}function S(e){return e(i(m))}function C(){h?.()}function w(e){D(e)}let T={$subscribe:y,$subscribeKey:b,$patch:x,$state:m,$actions:g,$getters:v,$dispose:C,$signal:S,use:w},E=new Proxy(T,{get(e,t){return t in g?g[t]:t in e?e[t]:m[t]},has(e,t){return t in e||t in g||t in m},set(e,t,n){return t in m?m[t]=n:e[t]=n,!0}});function D(t){_.has(t)||(_.add(t),t({store:E,options:e}))}for(let e of l)D(e);return h=c(l,()=>{for(let e of l)D(e)}),E}function f(e,t,n,r){Object.defineProperty(e,t,{get:n,enumerable:!0,...r})}function p(e,t){let n={};for(let r in e)n[r]=e[r].bind(t);return n}function m(e,n){return t(e.$state,n)}function h(e,n){return t(e.$getters,n)}function g(e,t,n){let r=m(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 _(e,t){return Object.fromEntries(Object.keys(e.$state).map(n=>[n,g(e,n,t)]))}var v={use:u};export{v as default,d as defineStore,l as plugins,g as storeToState,_ as storeToStates,u as use,h as useGetters,m 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-DD1IhWyl.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 };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{subscribe as e}from"valtio";import{get as t,set as n}from"@hairy/utils";import{destr as r}from"destr";import{generateStructureId as i}from"structure-id";function a({hydrate:a=!0}={}){return o=>{let{persist:s,getters:c}=o.options,{$state:l}=o.store;if(o.store.$persist?.unmount?.(),!s)return;let u=s===!0?{}:s,d=u.key||i(l),f=u.storage??(typeof localStorage<`u`?localStorage:void 0);if(!f?.getItem||!f?.setItem)return;let p=o.store.$persist?.meta??{mounted:!1,hydrated:!1};function m(e){let t=r(e);t&&typeof t==`object`&&
|
|
1
|
+
import{subscribe as e}from"valtio";import{get as t,set as n}from"@hairy/utils";import{destr as r}from"destr";import{generateStructureId as i}from"structure-id";function a({hydrate:a=!0}={}){return o=>{let{persist:s,getters:c}=o.options,{$state:l}=o.store;if(o.store.$persist?.unmount?.(),!s)return;let u=s===!0?{}:s,d=u.key||i(l),f=u.storage??(typeof localStorage<`u`?localStorage:void 0);if(!f?.getItem||!f?.setItem)return;let p=o.store.$persist?.meta??{mounted:!1,hydrated:!1};function m(e){let t=r(e);t&&typeof t==`object`&&Object.assign(l,t),p.hydrated=!0}function h(){p.mounted=!0;let e=f.getItem(d);e instanceof Promise?e.then(m):m(e)}function g(){p.unsubscribe?.(),p.unsubscribe=void 0}function _(){g(),p.unsubscribe=e(l,()=>{if(!p.hydrated)return;let e=(u.paths||Object.keys(l)).reduce((e,r)=>n(e,r,t(l,r)),{});f.setItem(d,JSON.stringify(e))})}o.store.$persist={mount:h,unmount:g,meta:p},a&&!p.mounted&&h(),_()}}export{a as persist};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { i as Getters, l as Plugin } from "../../types-DD1IhWyl.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-DD1IhWyl.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{};
|
|
@@ -5,9 +5,9 @@ type ActionsTree = Record<string, (...args: any[]) => any>;
|
|
|
5
5
|
type Actions<S = any> = Record<string, (this: S, ...args: any) => any>;
|
|
6
6
|
type Getters<S = any> = Record<string, (this: S) => any>;
|
|
7
7
|
type ActionsOmitThisParameter<A extends Actions<any>> = { [K in keyof A]: (...args: Parameters<A[K]>) => ReturnType<A[K]> };
|
|
8
|
-
type GettersReturnType<G extends Getters
|
|
8
|
+
type GettersReturnType<G extends Getters> = { [K in keyof G]: ReturnType<G[K]> };
|
|
9
9
|
interface StoreDefineOptions<S> {}
|
|
10
|
-
interface StoreDefine<S extends object, A extends object, G extends Getters
|
|
10
|
+
interface StoreDefine<S extends object, A extends object, G extends Getters> extends StoreDefineOptions<S> {
|
|
11
11
|
state: (() => S) | S;
|
|
12
12
|
actions?: A & ThisType<A & S & GettersReturnType<G>>;
|
|
13
13
|
getters?: G & ThisType<S & GettersReturnType<G>>;
|
|
@@ -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.1",
|
|
5
5
|
"description": "β‘quickly create a fully functional and robust Valtio factory",
|
|
6
6
|
"author": "Hairyf <wwu710632@gmail.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -26,19 +26,25 @@
|
|
|
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": [
|
|
32
36
|
"dist"
|
|
33
37
|
],
|
|
34
38
|
"peerDependencies": {
|
|
35
|
-
"react": "^
|
|
39
|
+
"react": "^19.2.4"
|
|
36
40
|
},
|
|
37
41
|
"dependencies": {
|
|
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",
|