valtio-define 1.1.1 → 1.1.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/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-C8WQypn2.mjs";
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-JMM2hTmB.mjs";
2
2
  import { Snapshot } from "valtio";
3
3
  import { Dispatch, SetStateAction } from "react";
4
4
 
@@ -35,7 +35,7 @@ declare function use(plugin: Plugin): void;
35
35
  *
36
36
  * ```
37
37
  */
38
- declare function defineStore<S extends object, A extends ActionsTree, G extends Getters<S>>(define: StoreDefine<S, A, G>): Store<S, A & Actions<S>, G>;
38
+ declare function defineStore<S extends object, A extends Actions<S>, G extends Getters<S>>(define: StoreDefine<S, A, G>): Store<S, A & Actions<S>, 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>): Snapshot<S & GettersReturnType<G> & A>;
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,l=s.getters||{},u=s.actions||{},d=e(c),f={},p={},m=new WeakSet;for(let e in u)f[e]=t(u[e].bind(d)),d[e]=f[e];for(let e in l)Object.defineProperty(d,e,{get:()=>l[e].call(d),enumerable:!0}),Object.defineProperty(p,e,{get:()=>d[e],enumerable:!0});function h(e){return n(d,t=>e(d,t))}function g(e,t){return a(d,e,e=>t(e),!0)}function _(e){typeof e==`function`?e(d):Object.assign(d,e)}function v(e){return i(()=>e(r(d)))}function y(e){o.push(e),S(e)}let b={$subscribe:h,$subscribeKey:g,$patch:_,$state:d,$actions:f,$getters:p,$signal:v,use:y},x=new Proxy(b,{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 S(e){m.has(e)||(m.add(e),e({store:x,options:s}))}for(let e of o)S(e);return n(o,()=>{for(let e of o)S(e)}),x}function l(e){return r(e.$state)}function u(e,t){let n=l(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 d(e){return Object.fromEntries(Object.keys(e.$state).map(t=>[t,u(e,t)]))}var f={use:s};export{f as default,c as defineStore,o as plugins,u as storeToState,d as storeToStates,s as use,l 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){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,2 +1,2 @@
1
- import { PersistentMountOptions, persist } from "./persist/index.mjs";
2
- export { PersistentMountOptions, persist };
1
+ import { PersistentMountOptions, StorePersistentOptions, persist } from "./persist/index.mjs";
2
+ export { PersistentMountOptions, StorePersistentOptions, persist };
@@ -1,10 +1,13 @@
1
- import { s as Plugin } from "../../types-C8WQypn2.mjs";
1
+ import { s as Plugin } from "../../types-JMM2hTmB.mjs";
2
2
  import { PersistentOptions } from "./types.mjs";
3
3
 
4
4
  //#region src/plugins/persist/index.d.ts
5
5
  interface PersistentMountOptions {
6
6
  automount?: boolean;
7
7
  }
8
+ interface StorePersistentOptions {
9
+ mount: () => void;
10
+ }
8
11
  declare function persist({
9
12
  automount
10
13
  }?: PersistentMountOptions): Plugin;
@@ -13,10 +16,8 @@ declare module 'valtio-define' {
13
16
  persist?: PersistentOptions<S> | boolean;
14
17
  }
15
18
  interface StoreOptions {
16
- persist: {
17
- mount: () => void;
18
- };
19
+ persist: StorePersistentOptions;
19
20
  }
20
21
  }
21
22
  //#endregion
22
- export { PersistentMountOptions, persist };
23
+ 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({automount: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))})}a&&h(),g()}}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({automount: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};
@@ -1,16 +1,16 @@
1
1
  import { ReactElement } from "react";
2
2
 
3
3
  //#region src/types.d.ts
4
- type Actions<S = any> = Record<string, (this: S, ...args: any) => any>;
5
4
  type ActionsTree = Record<string, (...args: any[]) => any>;
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
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 extends ActionsTree, G extends Getters<any>> extends StoreDefineOptions<S> {
10
+ interface StoreDefine<S extends object, A extends Actions<S>, G extends Getters<any>> extends StoreDefineOptions<S> {
11
11
  state: (() => S) | S;
12
- actions?: A & ThisType<A & S & GettersReturnType<G>>;
13
- getters?: G & ThisType<S & GettersReturnType<G>>;
12
+ actions?: A & ThisType<A & S>;
13
+ getters?: G & ThisType<S>;
14
14
  }
15
15
  interface Signal<S, G extends Getters<S>> {
16
16
  <T>(fn: (state: S & GettersReturnType<G>) => T): ReactElement;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "valtio-define",
3
3
  "type": "module",
4
- "version": "1.1.1",
4
+ "version": "1.1.3",
5
5
  "description": "⚡quickly create a fully functional and robust Valtio factory",
6
6
  "author": "Hairyf <wwu710632@gmail.com>",
7
7
  "license": "MIT",