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 +11 -6
- 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>
|
|
@@ -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)`**:
|
|
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
|
-
|
|
211
|
+
Register the Signal plugin globally:
|
|
212
212
|
```tsx
|
|
213
|
-
import
|
|
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
|
-
|
|
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-
|
|
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
|
|
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 +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.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": "^
|
|
39
|
+
"react": "^19.2.4"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
42
|
"@hairy/utils": "^1.47.0",
|