valtio-define 1.3.1 → 1.3.3
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 +8 -25
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +1 -1
- package/dist/plugins/persist/index.mjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -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
|
package/dist/index.d.mts
CHANGED
|
@@ -54,8 +54,8 @@ declare function storeToStates<S extends object>(store: Store<S>, options?: {
|
|
|
54
54
|
}): { [K in keyof S]: [S[K], Dispatch<SetStateAction<S[K]>>] };
|
|
55
55
|
//#endregion
|
|
56
56
|
//#region src/index.d.ts
|
|
57
|
-
declare const
|
|
57
|
+
declare const defaultExport: {
|
|
58
58
|
use: typeof use;
|
|
59
59
|
};
|
|
60
60
|
//#endregion
|
|
61
|
-
export { Actions, ActionsOmitThisParameter, ActionsTree, Getters, GettersReturnType, Op, Patch, Path, Plugin, PluginContext, Signal, Store, StoreDefine, StoreDefineOptions, StoreOptions, Subscribe, SubscribeKey,
|
|
61
|
+
export { Actions, ActionsOmitThisParameter, ActionsTree, Getters, GettersReturnType, Op, Patch, Path, Plugin, PluginContext, Signal, Store, StoreDefine, StoreDefineOptions, StoreOptions, Subscribe, SubscribeKey, defaultExport as default, defineStore, plugins, storeToState, storeToStates, use, useGetters, useStore };
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{proxy as e,useSnapshot as t}from"valtio";import{batch as n
|
|
1
|
+
import{proxy as e,useSnapshot as t}from"valtio";import{batch as n}from"valtio-reactive";import{subscribeKey as r}from"valtio/utils";import{proxy as i,ref as a,subscribe as o}from"valtio/vanilla";const s=e([]);function c(e){s.push(e)}function l(e){let t=typeof e.state==`function`?e.state():e.state,c=e.getters||{},l=e.actions||{},d=i(t),f={},p=i({}),m=new WeakSet,h;for(let e of Object.keys(c))u(d,e,c[e].bind(d),{enumerable:!1}),u(p,e,()=>d[e]);for(let e of Object.keys(l))f[e]=a(l[e].bind(d)),u(d,e,()=>f[e],{enumerable:!1});function g(e){return o(d,t=>e(d,t))}function _(e,t){return r(d,e,e=>t(e),!0)}function v(e){typeof e==`function`?n(()=>e(d)):Object.assign(d,e)}function y(){h?.()}function b(e){C(e)}let x={$subscribe:g,$subscribeKey:_,$patch:v,$state:d,$actions:f,$getters:p,$dispose:y,use:b},S=new Proxy(x,{get(e,t){return t in f?f[t]:t in e?e[t]:d[t]},has(e,t){return t in e||t in f||t in d},set(e,t,n){return t in d?d[t]=n:e[t]=n,!0}});function C(t){m.has(t)||(m.add(t),t({store:S,options:e}))}for(let e of s)C(e);return h=o(s,()=>{for(let e of s)C(e)}),S}function u(e,t,n,r){Object.defineProperty(e,t,{get:n,enumerable:!0,...r})}function d(e,n){return t(e.$state,n)}function f(e,n){return t(e.$getters,n)}function p(e,t,n){let r=d(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 m(e,t){return Object.fromEntries(Object.keys(e.$state).map(n=>[n,p(e,n,t)]))}const h={use:c};export{h as default,l as defineStore,s as plugins,p as storeToState,m as storeToStates,c as use,f as useGetters,d 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
|
|
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=new Set(Object.keys(c||{})),d=s===!0?{}:s,f=d.key||i(l),p=d.storage??(typeof localStorage<`u`?localStorage:void 0);if(!p?.getItem||!p?.setItem)return;let m=o.store.$persist?.meta??{mounted:!1,hydrated:!1};function h(e){let t=r(e);if(t&&typeof t==`object`){for(let e of Object.keys(t))u.has(e)&&delete t[e];Object.assign(l,t)}m.hydrated=!0}function g(){m.mounted=!0;let e=p.getItem(f);e instanceof Promise?e.then(h):h(e)}function _(){m.unsubscribe?.(),m.unsubscribe=void 0}function v(){_(),m.unsubscribe=e(l,()=>{if(!m.hydrated)return;let e=(d.paths||Object.keys(l)).filter(e=>!u.has(e)).reduce((e,r)=>n(e,r,t(l,r)),{});p.setItem(f,JSON.stringify(e))})}o.store.$persist={mount:g,unmount:_,meta:m},a&&!m.mounted&&g(),v()}}export{a as persist};
|
package/package.json
CHANGED