valtio-define 1.3.0 → 1.3.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 +19 -31
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +1 -1
- package/dist/plugins/persist/index.d.mts +1 -1
- package/dist/plugins/persist/index.mjs +1 -1
- package/dist/plugins/signal/index.d.mts +1 -1
- package/dist/plugins/signal/types.d.mts +1 -1
- package/dist/{types-Cu8iJxyS.d.mts → types-DD1IhWyl.d.mts} +2 -2
- package/package.json +2 -2
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>
|
|
@@ -42,43 +42,26 @@ const store = defineStore({
|
|
|
42
42
|
this.count++
|
|
43
43
|
},
|
|
44
44
|
},
|
|
45
|
+
getters: {
|
|
46
|
+
doubled() {
|
|
47
|
+
return this.count * 2
|
|
48
|
+
},
|
|
49
|
+
},
|
|
45
50
|
})
|
|
46
51
|
|
|
47
52
|
function Counter() {
|
|
48
|
-
const { count } = useStore(store)
|
|
53
|
+
const { count, doubled } = useStore(store)
|
|
49
54
|
|
|
50
55
|
return (
|
|
51
56
|
<div>
|
|
52
|
-
<div>
|
|
53
|
-
Count:
|
|
54
|
-
{count}
|
|
55
|
-
</div>
|
|
56
57
|
<button onClick={store.increment}>Increment</button>
|
|
58
|
+
<div>Count: {count}</div>
|
|
59
|
+
<div>Doubled: {doubled}</div>
|
|
57
60
|
</div>
|
|
58
61
|
)
|
|
59
62
|
}
|
|
60
63
|
```
|
|
61
64
|
|
|
62
|
-
### Derived State (Getters)
|
|
63
|
-
|
|
64
|
-
Getters are **computed properties**. They automatically re-evaluate when their dependencies change, providing a clean way to derive data.
|
|
65
|
-
|
|
66
|
-
```tsx
|
|
67
|
-
const store = defineStore({
|
|
68
|
-
state: () => ({ count: 0 }),
|
|
69
|
-
getters: {
|
|
70
|
-
doubled() {
|
|
71
|
-
return this.count * 2
|
|
72
|
-
},
|
|
73
|
-
},
|
|
74
|
-
actions: {
|
|
75
|
-
increment() {
|
|
76
|
-
this.count++
|
|
77
|
-
},
|
|
78
|
-
},
|
|
79
|
-
})
|
|
80
|
-
```
|
|
81
|
-
|
|
82
65
|
-----
|
|
83
66
|
|
|
84
67
|
## 🛠 Advanced Features
|
|
@@ -202,24 +185,24 @@ Every store instance created with `defineStore` includes built-in utility method
|
|
|
202
185
|
* **`$patch(obj | fn)`**: Bulk update the state.
|
|
203
186
|
* **`$subscribe(callback)`**: Watch the entire store for changes.
|
|
204
187
|
* **`$subscribeKey(key, callback)`**: Watch a specific property.
|
|
205
|
-
* **`$signal(selector)`**:
|
|
188
|
+
* **`$signal(selector)`**: Render reactive values inline in JSX (works best with the Signal plugin + `@jsxImportSource`).
|
|
206
189
|
|
|
207
190
|
-----
|
|
208
191
|
|
|
209
192
|
### 📡 Signal
|
|
210
193
|
|
|
211
|
-
|
|
194
|
+
Register the Signal plugin globally:
|
|
212
195
|
```tsx
|
|
213
|
-
import
|
|
196
|
+
import valtio from 'valtio-define'
|
|
214
197
|
import { signal } from 'valtio-define/plugins/signal'
|
|
215
198
|
|
|
216
199
|
valtio.use(signal())
|
|
217
200
|
```
|
|
218
201
|
|
|
219
|
-
|
|
202
|
+
Add `jsxImportSource` at the beginning of your `.tsx` file:
|
|
220
203
|
|
|
221
204
|
```tsx
|
|
222
|
-
/** @jsxImportSource valtio-signal */
|
|
205
|
+
/** @jsxImportSource valtio-define/plugins/signal */
|
|
223
206
|
|
|
224
207
|
function App() {
|
|
225
208
|
return <div>{store.$signal(state => state.count)}</div>
|
|
@@ -233,6 +216,8 @@ function App() {
|
|
|
233
216
|
Plugins can be applied to all stores or restricted to a single instance.
|
|
234
217
|
|
|
235
218
|
```tsx
|
|
219
|
+
import valtio, { defineStore } from 'valtio-define'
|
|
220
|
+
|
|
236
221
|
// Global
|
|
237
222
|
valtio.use(myPlugin())
|
|
238
223
|
|
|
@@ -247,6 +232,7 @@ Extend functionality by accessing the `store` instance and `options` through the
|
|
|
247
232
|
|
|
248
233
|
```tsx
|
|
249
234
|
import type { Plugin } from 'valtio-define'
|
|
235
|
+
import valtio from 'valtio-define'
|
|
250
236
|
|
|
251
237
|
function loggerPlugin(): Plugin {
|
|
252
238
|
return ({ store, options }) => {
|
|
@@ -256,6 +242,8 @@ function loggerPlugin(): Plugin {
|
|
|
256
242
|
}
|
|
257
243
|
}
|
|
258
244
|
|
|
245
|
+
valtio.use(loggerPlugin())
|
|
246
|
+
|
|
259
247
|
declare module 'valtio-define' {
|
|
260
248
|
export interface StoreDefineOptions<S extends object> {
|
|
261
249
|
$myPlugin?: {
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
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-
|
|
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
|
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{proxy as e,useSnapshot as t}from"valtio";import{
|
|
1
|
+
import{proxy as e,useSnapshot as t}from"valtio";import{batch 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||{},f=a(t),p={},m=a({}),h=new WeakSet,g;for(let e of Object.keys(l))d(f,e,l[e].bind(f),{enumerable:!1}),d(m,e,()=>f[e]);for(let e of Object.keys(u))p[e]=o(u[e].bind(f)),d(f,e,()=>p[e],{enumerable:!1});function _(e){return s(f,t=>e(f,t))}function v(e,t){return i(f,e,e=>t(e),!0)}function y(e){typeof e==`function`?n(()=>e(f)):Object.assign(f,e)}function b(e){return e(r(f))}function x(){g?.()}function S(e){T(e)}let C={$subscribe:_,$subscribeKey:v,$patch:y,$state:f,$actions:p,$getters:m,$dispose:x,$signal:b,use:S},w=new Proxy(C,{get(e,t){return t in p?p[t]:t in e?e[t]:f[t]},has(e,t){return t in e||t in p||t in f},set(e,t,n){return t in f?f[t]=n:e[t]=n,!0}});function T(t){h.has(t)||(h.add(t),t({store:w,options:e}))}for(let e of c)T(e);return g=s(c,()=>{for(let e of c)T(e)}),w}function d(e,t,n,r){Object.defineProperty(e,t,{get:n,enumerable:!0,...r})}function f(e,n){return t(e.$state,n)}function p(e,n){return t(e.$getters,n)}function m(e,t,n){let r=f(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 h(e,t){return Object.fromEntries(Object.keys(e.$state).map(n=>[n,m(e,n,t)]))}var g={use:l};export{g as default,u as defineStore,c as plugins,m as storeToState,h as storeToStates,l as use,p as useGetters,f as useStore};
|
|
@@ -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};
|
|
@@ -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>>;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "valtio-define",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.3.
|
|
4
|
+
"version": "1.3.2",
|
|
5
5
|
"description": "⚡quickly create a fully functional and robust Valtio factory",
|
|
6
6
|
"author": "Hairyf <wwu710632@gmail.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"dist"
|
|
37
37
|
],
|
|
38
38
|
"peerDependencies": {
|
|
39
|
-
"react": "^
|
|
39
|
+
"react": "^19.2.4"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
42
|
"@hairy/utils": "^1.47.0",
|