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 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)`**: Use a selector function inside JSX for fine-grained reactivity.
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 PluginContext, d as StoreDefine, f as StoreDefineOptions, h as SubscribeKey, i as Getters, l as Signal, m as Subscribe, n as ActionsOmitThisParameter, o as Patch, p as StoreOptions, r as ActionsTree, s as Plugin, t as Actions, u as Store } from "./types-CqDu3QIJ.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
 
@@ -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> = {}>(define: StoreDefine<S, A, G>): Store<S, A, G>;
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,ref as t,subscribe as n,useSnapshot as r}from"valtio";import{createElement as i}from"react";import{subscribeKey as a}from"valtio/utils";const o=e([]);function s(e){o.push(e)}function c(s){let c=typeof s.state==`function`?s.state():s.state,u=s.getters||{},d=s.actions||{},f=e(c),p={},m={},h=new WeakSet;for(let e in d)p[e]=t(d[e].bind(f)),f[e]=p[e];for(let e in u)l(f,e,()=>u[e].call(f)),l(m,e,()=>f[e]);function g(e){return n(f,t=>e(f,t))}function _(e,t){return a(f,e,e=>t(e),!0)}function v(e){typeof e==`function`?e(f):Object.assign(f,e)}function y(e){return i(()=>e(r(f)))}function b(e){o.push(e),C(e)}let x={$subscribe:g,$subscribeKey:_,$patch:v,$state:f,$actions:p,$getters:m,$signal:y,use:b},S=new Proxy(x,{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 C(e){h.has(e)||(h.add(e),e({store:S,options:s}))}for(let e of o)C(e);return n(o,()=>{for(let e of o)C(e)}),S}function l(e,t,n){Object.defineProperty(e,t,{get:n,enumerable:!0})}function u(e,t){return r(e.$state,t)}function d(e,t,n){let r=u(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 f(e,t){return Object.fromEntries(Object.keys(e.$state).map(n=>[n,d(e,n,t)]))}var p={use:s};export{p as default,c as defineStore,o as plugins,d as storeToState,f as storeToStates,s as use,u 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,2 +1,3 @@
1
1
  import { PersistentMountOptions, persist } from "./persist/index.mjs";
2
- export { PersistentMountOptions, persist };
2
+ import { signal } from "./signal/index.mjs";
3
+ export { PersistentMountOptions, persist, signal };
@@ -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 { s as Plugin } from "../../types-CqDu3QIJ.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
@@ -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`&&(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};
@@ -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,2 @@
1
+ import { Fragment, jsx, jsxDEV, jsxs } from "./jsx-runtime.mjs";
2
+ export { Fragment, jsx, jsxDEV, jsxs };
@@ -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<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>>;
@@ -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 & GettersReturnType<G> & ActionsOmitThisParameter<A>;
34
+ $state: S;
35
35
  $actions: ActionsOmitThisParameter<A>;
36
36
  $getters: GettersReturnType<G>;
37
+ $dispose: () => void;
37
38
  use: (plugin: Plugin) => void;
38
- $signal: Signal<S, G>;
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, PluginContext as c, StoreDefine as d, StoreDefineOptions as f, SubscribeKey as h, Getters as i, Signal as l, Subscribe as m, ActionsOmitThisParameter as n, Patch as o, StoreOptions as p, ActionsTree as r, Plugin as s, Actions as t, Store as u };
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.2.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": "^18.2.0"
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",