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 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)`**: Use a selector function to create a signal.
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
- > if you want to use the signal plugin, you need to import it and use it in your store.
194
+ Register the Signal plugin globally:
212
195
  ```tsx
213
- import { valtio } from 'valtio-define'
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
- And add jsxImportSource at the beginning of your `.tsx` file
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-Cu8iJxyS.mjs";
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{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};
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,4 +1,4 @@
1
- import { l as Plugin } from "../../types-Cu8iJxyS.mjs";
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
@@ -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`&&(Object.keys(c||{}).forEach(e=>Reflect.deleteProperty(t,e)),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};
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};
@@ -1,4 +1,4 @@
1
- import { i as Getters, l as Plugin } from "../../types-Cu8iJxyS.mjs";
1
+ import { i as Getters, l as Plugin } from "../../types-DD1IhWyl.mjs";
2
2
  import { SignalStore } from "./types.mjs";
3
3
 
4
4
  //#region src/plugins/signal/index.d.ts
@@ -1,4 +1,4 @@
1
- import { a as GettersReturnType, i as Getters } from "../../types-Cu8iJxyS.mjs";
1
+ import { a as GettersReturnType, i as Getters } from "../../types-DD1IhWyl.mjs";
2
2
 
3
3
  //#region src/plugins/signal/types.d.ts
4
4
  interface Signal<S> {
@@ -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<any>> = { [K in keyof G]: ReturnType<G[K]> };
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<any>> extends StoreDefineOptions<S> {
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.0",
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": "^18.2.0"
39
+ "react": "^19.2.4"
40
40
  },
41
41
  "dependencies": {
42
42
  "@hairy/utils": "^1.47.0",