valtio-define 1.3.0 → 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 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>
@@ -202,24 +202,24 @@ 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 to create a signal.
205
+ * **`$signal(selector)`**: Render reactive values inline in JSX (works best with the Signal plugin + `@jsxImportSource`).
206
206
 
207
207
  -----
208
208
 
209
209
  ### 📡 Signal
210
210
 
211
- > if you want to use the signal plugin, you need to import it and use it in your store.
211
+ Register the Signal plugin globally:
212
212
  ```tsx
213
- import { valtio } from 'valtio-define'
213
+ import valtio from 'valtio-define'
214
214
  import { signal } from 'valtio-define/plugins/signal'
215
215
 
216
216
  valtio.use(signal())
217
217
  ```
218
218
 
219
- And add jsxImportSource at the beginning of your `.tsx` file
219
+ Add `jsxImportSource` at the beginning of your `.tsx` file:
220
220
 
221
221
  ```tsx
222
- /** @jsxImportSource valtio-signal */
222
+ /** @jsxImportSource valtio-define/plugins/signal */
223
223
 
224
224
  function App() {
225
225
  return <div>{store.$signal(state => state.count)}</div>
@@ -233,6 +233,8 @@ function App() {
233
233
  Plugins can be applied to all stores or restricted to a single instance.
234
234
 
235
235
  ```tsx
236
+ import valtio, { defineStore } from 'valtio-define'
237
+
236
238
  // Global
237
239
  valtio.use(myPlugin())
238
240
 
@@ -247,6 +249,7 @@ Extend functionality by accessing the `store` instance and `options` through the
247
249
 
248
250
  ```tsx
249
251
  import type { Plugin } from 'valtio-define'
252
+ import valtio from 'valtio-define'
250
253
 
251
254
  function loggerPlugin(): Plugin {
252
255
  return ({ store, options }) => {
@@ -256,6 +259,8 @@ function loggerPlugin(): Plugin {
256
259
  }
257
260
  }
258
261
 
262
+ valtio.use(loggerPlugin())
263
+
259
264
  declare module 'valtio-define' {
260
265
  export interface StoreDefineOptions<S extends object> {
261
266
  $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,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};
@@ -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.1",
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",