valtio-define 1.2.1 → 1.3.0

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
@@ -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)`**: Use a selector function to create a signal.
206
206
 
207
207
  -----
208
208
 
209
+ ### 📡 Signal
210
+
211
+ > if you want to use the signal plugin, you need to import it and use it in your store.
212
+ ```tsx
213
+ import { valtio } from 'valtio-define'
214
+ import { signal } from 'valtio-define/plugins/signal'
215
+
216
+ valtio.use(signal())
217
+ ```
218
+
219
+ And add jsxImportSource at the beginning of your `.tsx` file
220
+
221
+ ```tsx
222
+ /** @jsxImportSource valtio-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
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-Cu8iJxyS.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{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,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-Cu8iJxyS.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 };
@@ -0,0 +1,10 @@
1
+ import { i as Getters, l as Plugin } from "../../types-Cu8iJxyS.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-Cu8iJxyS.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{};
@@ -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.0",
5
5
  "description": "⚡quickly create a fully functional and robust Valtio factory",
6
6
  "author": "Hairyf <wwu710632@gmail.com>",
7
7
  "license": "MIT",
@@ -26,6 +26,10 @@
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": [
@@ -38,7 +42,9 @@
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",