valtio-define 1.1.3 → 1.1.4
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 +4 -6
- package/dist/index.d.mts +7 -4
- package/dist/index.mjs +1 -1
- package/dist/plugins/persist/index.d.mts +8 -7
- package/dist/plugins/persist/index.mjs +1 -1
- package/dist/plugins/persist/types.d.mts +6 -1
- package/dist/{types-JMM2hTmB.d.mts → types-BA_0y5TQ.d.mts} +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -124,16 +124,14 @@ const store = defineStore({
|
|
|
124
124
|
})
|
|
125
125
|
```
|
|
126
126
|
|
|
127
|
-
You can pass `
|
|
127
|
+
You can pass `hydrate` when registering the plugin (default `true`). When `true`, state is hydrated from storage as soon as the store is created. Set `hydrate: false` when you need to avoid running persistence during server-side rendering, and manually mount persist in your App entry instead:
|
|
128
128
|
|
|
129
129
|
```tsx
|
|
130
|
-
// Register with
|
|
131
|
-
store.use(persist({
|
|
130
|
+
// Register with hydrate: false
|
|
131
|
+
store.use(persist({ hydrate: false }))
|
|
132
132
|
|
|
133
133
|
// In your App (client entry), after store is used:
|
|
134
|
-
useEffect(() =>
|
|
135
|
-
store.use(persist({ automount: false }))
|
|
136
|
-
}, [])
|
|
134
|
+
useEffect(() => store.$persist.mount(), [])
|
|
137
135
|
```
|
|
138
136
|
|
|
139
137
|
### Subscribe to Changes
|
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-
|
|
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-BA_0y5TQ.mjs";
|
|
2
2
|
import { Snapshot } from "valtio";
|
|
3
3
|
import { Dispatch, SetStateAction } from "react";
|
|
4
4
|
|
|
@@ -35,10 +35,13 @@ 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
|
|
38
|
+
declare function defineStore<S extends object, A extends Actions<S>, G extends Getters<S>>(define: StoreDefine<S, A, G>): Store<S, A, G>;
|
|
39
39
|
//#endregion
|
|
40
40
|
//#region src/hooks.d.ts
|
|
41
|
-
|
|
41
|
+
interface UseStoreOptions {
|
|
42
|
+
sync?: boolean;
|
|
43
|
+
}
|
|
44
|
+
declare function useStore<S extends object, A extends Actions<S>, G extends Getters<S>>(store: Store<S, A, G>, options?: UseStoreOptions): Snapshot<S & GettersReturnType<G> & A>;
|
|
42
45
|
//#endregion
|
|
43
46
|
//#region src/utils.d.ts
|
|
44
47
|
declare function storeToState<S extends object, K extends keyof S>(store: Store<S>, key: K): [S[K], Dispatch<SetStateAction<S[K]>>];
|
|
@@ -49,4 +52,4 @@ declare const _default: {
|
|
|
49
52
|
use: typeof use;
|
|
50
53
|
};
|
|
51
54
|
//#endregion
|
|
52
|
-
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 };
|
|
55
|
+
export { Actions, ActionsOmitThisParameter, ActionsTree, Getters, GettersReturnType, Patch, Plugin, PluginContext, Signal, Store, StoreDefine, StoreDefineOptions, StoreOptions, Subscribe, SubscribeKey, UseStoreOptions, _default as default, defineStore, plugins, storeToState, storeToStates, use, 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){return r(e.$state)}function d(e,t){let n=u(e);function r(n){typeof n==`function`?e.$patch(e=>{e[t]=n(e[t])}):e.$patch(e=>{e[t]=n})}return[n[t],r]}function f(e){return Object.fromEntries(Object.keys(e.$state).map(t=>[t,d(e,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,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){let n=u(e);function r(n){typeof n==`function`?e.$patch(e=>{e[t]=n(e[t])}):e.$patch(e=>{e[t]=n})}return[n[t],r]}function f(e){return Object.fromEntries(Object.keys(e.$state).map(t=>[t,d(e,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,23 +1,24 @@
|
|
|
1
|
-
import { s as Plugin } from "../../types-
|
|
2
|
-
import { PersistentOptions } from "./types.mjs";
|
|
1
|
+
import { s as Plugin } from "../../types-BA_0y5TQ.mjs";
|
|
2
|
+
import { PersistentOptions, PersistentStore } from "./types.mjs";
|
|
3
3
|
|
|
4
4
|
//#region src/plugins/persist/index.d.ts
|
|
5
5
|
interface PersistentMountOptions {
|
|
6
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Whether to automatically mount the persist plugin when the store is created
|
|
8
|
+
*/
|
|
9
|
+
hydrate?: boolean;
|
|
7
10
|
}
|
|
8
11
|
interface StorePersistentOptions {
|
|
9
12
|
mount: () => void;
|
|
10
13
|
}
|
|
11
14
|
declare function persist({
|
|
12
|
-
|
|
15
|
+
hydrate
|
|
13
16
|
}?: PersistentMountOptions): Plugin;
|
|
14
17
|
declare module 'valtio-define' {
|
|
15
18
|
interface StoreDefineOptions<S extends object> {
|
|
16
19
|
persist?: PersistentOptions<S> | boolean;
|
|
17
20
|
}
|
|
18
|
-
interface StoreOptions {
|
|
19
|
-
persist: StorePersistentOptions;
|
|
20
|
-
}
|
|
21
|
+
interface StoreOptions extends PersistentStore {}
|
|
21
22
|
}
|
|
22
23
|
//#endregion
|
|
23
24
|
export { PersistentMountOptions, StorePersistentOptions, 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({
|
|
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(!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=!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=!0}function h(){let e=f.getItem(d);e instanceof Promise?e.then(m):m(e)}function g(){e(l,()=>{if(!p)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},a&&h(),g()}}export{a as persist};
|
|
@@ -12,5 +12,10 @@ interface PersistentOptions<S extends object> {
|
|
|
12
12
|
storage?: Storage;
|
|
13
13
|
paths?: DeepKeys<S>[];
|
|
14
14
|
}
|
|
15
|
+
interface PersistentStore {
|
|
16
|
+
$persist: {
|
|
17
|
+
mount: () => void;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
15
20
|
//#endregion
|
|
16
|
-
export { DeepKeys, PersistentOptions, Storage };
|
|
21
|
+
export { DeepKeys, PersistentOptions, PersistentStore, Storage };
|
|
@@ -7,7 +7,7 @@ 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
8
|
type GettersReturnType<G extends Getters<any>> = { [K in keyof G]: ReturnType<G[K]> };
|
|
9
9
|
interface StoreDefineOptions<S> {}
|
|
10
|
-
interface StoreDefine<S extends object, A
|
|
10
|
+
interface StoreDefine<S extends object, A, G extends Getters<any>> extends StoreDefineOptions<S> {
|
|
11
11
|
state: (() => S) | S;
|
|
12
12
|
actions?: A & ThisType<A & S>;
|
|
13
13
|
getters?: G & ThisType<S>;
|
package/package.json
CHANGED