valtio-define 1.0.3 → 1.1.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 +22 -10
- package/dist/index.d.mts +46 -6
- package/dist/index.mjs +1 -1
- package/dist/plugins/index.d.mts +2 -3
- package/dist/plugins/index.mjs +1 -1
- package/dist/plugins/presist/index.d.mts +22 -0
- package/dist/plugins/presist/index.mjs +1 -0
- package/dist/plugins/presist/types.d.mts +16 -0
- package/dist/{index-CXmAkQQL.d.mts → types-C8WQypn2.d.mts} +6 -4
- package/package.json +33 -32
- package/dist/define-aC-aPNbX.d.mts +0 -35
- package/dist/define-vminFPaI.mjs +0 -1
- package/dist/define.d.mts +0 -3
- package/dist/define.mjs +0 -1
- package/dist/index-BvOmxIpj.d.mts +0 -23
- package/dist/persistent-Cp0WGzx8.mjs +0 -1
- package/dist/plugin-CnEGCdHz.d.mts +0 -7
- package/dist/plugin-D5lPjOi5.mjs +0 -1
- package/dist/plugin.d.mts +0 -3
- package/dist/plugin.mjs +0 -1
- package/dist/plugins/persistent/index.d.mts +0 -3
- package/dist/plugins/persistent/index.mjs +0 -1
- package/dist/types/index.d.mts +0 -2
- package/dist/types/index.mjs +0 -1
- package/dist/use-BJ-F80_h.mjs +0 -1
- package/dist/use-DUChuhuv.d.mts +0 -7
- package/dist/use.d.mts +0 -3
- package/dist/use.mjs +0 -1
- package/dist/utils-BBLhHVaU.d.mts +0 -8
- package/dist/utils-Cw3GrNQy.mjs +0 -1
- package/dist/utils.d.mts +0 -3
- package/dist/utils.mjs +0 -1
- /package/dist/{types-Di9Kuaj2.mjs → plugins/presist/types.mjs} +0 -0
package/README.md
CHANGED
|
@@ -85,14 +85,14 @@ function Counter() {
|
|
|
85
85
|
|
|
86
86
|
The persistence plugin allows you to persist store state to storage (e.g., localStorage).
|
|
87
87
|
|
|
88
|
-
First, register the
|
|
88
|
+
First, register the presist plugin:
|
|
89
89
|
|
|
90
90
|
```tsx
|
|
91
91
|
import valtio from 'valtio-define'
|
|
92
|
-
import {
|
|
92
|
+
import { presist } from 'valtio-define/plugins'
|
|
93
93
|
|
|
94
|
-
// Register the
|
|
95
|
-
valtio.use(
|
|
94
|
+
// Register the presist plugin globally
|
|
95
|
+
valtio.use(presist())
|
|
96
96
|
```
|
|
97
97
|
|
|
98
98
|
Then use it in your store:
|
|
@@ -124,6 +124,18 @@ const store = defineStore({
|
|
|
124
124
|
})
|
|
125
125
|
```
|
|
126
126
|
|
|
127
|
+
You can pass `automount` when registering the plugin (default `true`). When `true`, state is hydrated from storage as soon as the store is created. Set `automount: false` when you need to avoid running persistence during server-side rendering, and manually mount persist in your App entry instead:
|
|
128
|
+
|
|
129
|
+
```tsx
|
|
130
|
+
// Register with automount: false
|
|
131
|
+
store.use(presist({ automount: false }))
|
|
132
|
+
|
|
133
|
+
// In your App (client entry), after store is used:
|
|
134
|
+
useEffect(() => {
|
|
135
|
+
store.use(presist({ automount: false }))
|
|
136
|
+
}, [])
|
|
137
|
+
```
|
|
138
|
+
|
|
127
139
|
### Subscribe to Changes
|
|
128
140
|
|
|
129
141
|
```tsx
|
|
@@ -306,24 +318,24 @@ Plugins allow you to extend store functionality. You can use plugins globally or
|
|
|
306
318
|
|
|
307
319
|
```tsx
|
|
308
320
|
import valtio from 'valtio-define'
|
|
309
|
-
import {
|
|
321
|
+
import { presist } from 'valtio-define/plugins'
|
|
310
322
|
|
|
311
323
|
// Register plugin globally - applies to all stores
|
|
312
|
-
valtio.use(
|
|
324
|
+
valtio.use(presist())
|
|
313
325
|
```
|
|
314
326
|
|
|
315
327
|
#### Per-Store Plugin Registration
|
|
316
328
|
|
|
317
329
|
```tsx
|
|
318
330
|
import { defineStore } from 'valtio-define'
|
|
319
|
-
import {
|
|
331
|
+
import { presist } from 'valtio-define/plugins'
|
|
320
332
|
|
|
321
333
|
const store = defineStore({
|
|
322
334
|
state: () => ({ count: 0 }),
|
|
323
335
|
})
|
|
324
336
|
|
|
325
337
|
// Register plugin for this specific store
|
|
326
|
-
store.use(
|
|
338
|
+
store.use(presist())
|
|
327
339
|
```
|
|
328
340
|
|
|
329
341
|
#### Creating Custom Plugins
|
|
@@ -345,7 +357,7 @@ function myPlugin() {
|
|
|
345
357
|
}
|
|
346
358
|
}
|
|
347
359
|
|
|
348
|
-
declare module 'valtio-define
|
|
360
|
+
declare module 'valtio-define' {
|
|
349
361
|
export interface StoreDefine<S extends object, A extends ActionsTree, G extends Getters<any>> {
|
|
350
362
|
myPlugin?: {
|
|
351
363
|
someOption?: boolean
|
|
@@ -354,7 +366,7 @@ declare module 'valtio-define/types' {
|
|
|
354
366
|
}
|
|
355
367
|
|
|
356
368
|
// Use the plugin
|
|
357
|
-
use(myPlugin)
|
|
369
|
+
use(myPlugin())
|
|
358
370
|
```
|
|
359
371
|
|
|
360
372
|
**Plugin Context:**
|
package/dist/index.d.mts
CHANGED
|
@@ -1,12 +1,52 @@
|
|
|
1
|
-
import "./
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { t as useStore } from "./use-DUChuhuv.mjs";
|
|
5
|
-
import { n as storeToStates, t as storeToState } from "./utils-BBLhHVaU.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-C8WQypn2.mjs";
|
|
2
|
+
import { Snapshot } from "valtio";
|
|
3
|
+
import { Dispatch, SetStateAction } from "react";
|
|
6
4
|
|
|
5
|
+
//#region src/plugin.d.ts
|
|
6
|
+
declare const plugins: Plugin[];
|
|
7
|
+
declare function use(plugin: Plugin): void;
|
|
8
|
+
//#endregion
|
|
9
|
+
//#region src/define.d.ts
|
|
10
|
+
/**
|
|
11
|
+
* @description Define a store
|
|
12
|
+
* @example
|
|
13
|
+
* ```tsx
|
|
14
|
+
* const store = defineStore({
|
|
15
|
+
* state: () => ({ count: 0 }),
|
|
16
|
+
* actions: {
|
|
17
|
+
* increment() {
|
|
18
|
+
* this.count++
|
|
19
|
+
* },
|
|
20
|
+
* },
|
|
21
|
+
* })
|
|
22
|
+
*
|
|
23
|
+
* store.increment()
|
|
24
|
+
* console.log(store.$state.count) // 1
|
|
25
|
+
*
|
|
26
|
+
* function Component() {
|
|
27
|
+
* const store = useStore(store)
|
|
28
|
+
* return (
|
|
29
|
+
* <div>
|
|
30
|
+
* <button onClick={store.increment}>Increment</button>
|
|
31
|
+
* <div>{store.count}</div>
|
|
32
|
+
* </div>
|
|
33
|
+
* )
|
|
34
|
+
* }
|
|
35
|
+
*
|
|
36
|
+
* ```
|
|
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>;
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/hooks.d.ts
|
|
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>;
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/utils.d.ts
|
|
44
|
+
declare function storeToState<S extends object, K extends keyof S>(store: Store<S>, key: K): [S[K], Dispatch<SetStateAction<S[K]>>];
|
|
45
|
+
declare function storeToStates<S extends object>(store: Store<S>): { [K in keyof S]: [S[K], Dispatch<SetStateAction<S[K]>>] };
|
|
46
|
+
//#endregion
|
|
7
47
|
//#region src/index.d.ts
|
|
8
48
|
declare const _default: {
|
|
9
49
|
use: typeof use;
|
|
10
50
|
};
|
|
11
51
|
//#endregion
|
|
12
|
-
export { _default as default, defineStore, plugins, storeToState, storeToStates, use, useStore };
|
|
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 };
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
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};
|
package/dist/plugins/index.d.mts
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
import "
|
|
2
|
-
|
|
3
|
-
export { DeepKeys, PersistentOptions, Storage };
|
|
1
|
+
import { PersistentMountOptions, presist } from "./presist/index.mjs";
|
|
2
|
+
export { PersistentMountOptions, presist };
|
package/dist/plugins/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import"
|
|
1
|
+
import{presist as e}from"./presist/index.mjs";export{e as presist};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { s as Plugin } from "../../types-C8WQypn2.mjs";
|
|
2
|
+
import { PersistentOptions } from "./types.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/plugins/presist/index.d.ts
|
|
5
|
+
interface PersistentMountOptions {
|
|
6
|
+
automount?: boolean;
|
|
7
|
+
}
|
|
8
|
+
declare function presist({
|
|
9
|
+
automount
|
|
10
|
+
}?: PersistentMountOptions): Plugin;
|
|
11
|
+
declare module 'valtio-define' {
|
|
12
|
+
interface StoreDefineOptions<S extends object> {
|
|
13
|
+
persist?: PersistentOptions<S> | boolean;
|
|
14
|
+
}
|
|
15
|
+
interface StoreOptions {
|
|
16
|
+
persist: {
|
|
17
|
+
mount: () => void;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
//#endregion
|
|
22
|
+
export { PersistentMountOptions, presist };
|
|
@@ -0,0 +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 presist};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Awaitable } from "@hairy/utils";
|
|
2
|
+
|
|
3
|
+
//#region src/plugins/presist/types.d.ts
|
|
4
|
+
type DeepKeys<T> = T extends object ? { [K in keyof T & string]: T[K] extends object ? K | `${K}.${DeepKeys<T[K]>}` : K }[keyof T & string] : never;
|
|
5
|
+
interface Storage {
|
|
6
|
+
getItem: (key: string) => Awaitable<any>;
|
|
7
|
+
setItem: (key: string, value: any) => Awaitable<void>;
|
|
8
|
+
[key: string]: any;
|
|
9
|
+
}
|
|
10
|
+
interface PersistentOptions<S extends object> {
|
|
11
|
+
key?: string;
|
|
12
|
+
storage?: Storage;
|
|
13
|
+
paths?: DeepKeys<S>[];
|
|
14
|
+
}
|
|
15
|
+
//#endregion
|
|
16
|
+
export { DeepKeys, PersistentOptions, Storage };
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { ReactElement } from "react";
|
|
2
2
|
|
|
3
|
-
//#region src/types
|
|
3
|
+
//#region src/types.d.ts
|
|
4
4
|
type Actions<S = any> = Record<string, (this: S, ...args: any) => any>;
|
|
5
5
|
type ActionsTree = Record<string, (...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
|
-
interface
|
|
9
|
+
interface StoreDefineOptions<S> {}
|
|
10
|
+
interface StoreDefine<S extends object, A extends ActionsTree, G extends Getters<any>> extends StoreDefineOptions<S> {
|
|
10
11
|
state: (() => S) | S;
|
|
11
12
|
actions?: A & ThisType<A & S & GettersReturnType<G>>;
|
|
12
13
|
getters?: G & ThisType<S & GettersReturnType<G>>;
|
|
@@ -25,6 +26,7 @@ interface SubscribeKey<S, G extends Getters<S>> {
|
|
|
25
26
|
interface Patch<S, G extends Getters<S>> {
|
|
26
27
|
(patch: Partial<S> | ((state: S & GettersReturnType<G>) => void)): void;
|
|
27
28
|
}
|
|
29
|
+
interface StoreOptions {}
|
|
28
30
|
type Store<S, A extends Actions<S> = Actions<S>, G extends Getters<S> = Getters<S>> = {
|
|
29
31
|
$subscribe: Subscribe<S, G>;
|
|
30
32
|
$subscribeKey: SubscribeKey<S, G>;
|
|
@@ -34,7 +36,7 @@ type Store<S, A extends Actions<S> = Actions<S>, G extends Getters<S> = Getters<
|
|
|
34
36
|
$getters: GettersReturnType<G>;
|
|
35
37
|
use: (plugin: Plugin) => void;
|
|
36
38
|
$signal: Signal<S, G>;
|
|
37
|
-
} & S & GettersReturnType<G> & ActionsOmitThisParameter<A
|
|
39
|
+
} & S & GettersReturnType<G> & ActionsOmitThisParameter<A> & StoreOptions;
|
|
38
40
|
interface PluginContext<S extends object = Record<string, unknown>> {
|
|
39
41
|
store: Store<S, Actions<S>, Getters<S>>;
|
|
40
42
|
options: StoreDefine<S, ActionsTree, Getters<S>>;
|
|
@@ -43,4 +45,4 @@ interface Plugin {
|
|
|
43
45
|
<S extends object = Record<string, unknown>>(context: PluginContext<S>): void;
|
|
44
46
|
}
|
|
45
47
|
//#endregion
|
|
46
|
-
export { GettersReturnType as a, PluginContext as c, StoreDefine as d,
|
|
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 };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "valtio-define",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.0
|
|
4
|
+
"version": "1.1.0",
|
|
5
5
|
"description": "⚡quickly create a fully functional and robust Valtio factory",
|
|
6
6
|
"author": "Hairyf <wwu710632@gmail.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -11,21 +11,23 @@
|
|
|
11
11
|
"url": "git+https://github.com/hairyf/valtio-define.git"
|
|
12
12
|
},
|
|
13
13
|
"bugs": "https://github.com/hairyf/valtio-define/issues",
|
|
14
|
-
"keywords": [
|
|
14
|
+
"keywords": [
|
|
15
|
+
"valtio",
|
|
16
|
+
"define",
|
|
17
|
+
"pinia",
|
|
18
|
+
"factory",
|
|
19
|
+
"store",
|
|
20
|
+
"react",
|
|
21
|
+
"typescript"
|
|
22
|
+
],
|
|
15
23
|
"sideEffects": false,
|
|
16
24
|
"exports": {
|
|
17
25
|
".": "./dist/index.mjs",
|
|
18
|
-
"./define": "./dist/define.mjs",
|
|
19
|
-
"./plugin": "./dist/plugin.mjs",
|
|
20
26
|
"./plugins": "./dist/plugins/index.mjs",
|
|
21
|
-
"./plugins/
|
|
22
|
-
"./types": "./dist/types
|
|
23
|
-
"./use": "./dist/use.mjs",
|
|
24
|
-
"./utils": "./dist/utils.mjs",
|
|
27
|
+
"./plugins/presist": "./dist/plugins/presist/index.mjs",
|
|
28
|
+
"./plugins/presist/types": "./dist/plugins/presist/types.mjs",
|
|
25
29
|
"./package.json": "./package.json"
|
|
26
30
|
},
|
|
27
|
-
"main": "./dist/index.mjs",
|
|
28
|
-
"module": "./dist/index.mjs",
|
|
29
31
|
"files": [
|
|
30
32
|
"dist"
|
|
31
33
|
],
|
|
@@ -33,37 +35,37 @@
|
|
|
33
35
|
"react": "^18.2.0"
|
|
34
36
|
},
|
|
35
37
|
"dependencies": {
|
|
36
|
-
"@hairy/utils": "^1.
|
|
38
|
+
"@hairy/utils": "^1.47.0",
|
|
37
39
|
"destr": "^2.0.5",
|
|
38
40
|
"structure-id": "^1.2.9",
|
|
39
|
-
"valtio": "^2.
|
|
41
|
+
"valtio": "^2.3.0"
|
|
40
42
|
},
|
|
41
43
|
"devDependencies": {
|
|
42
|
-
"@antfu/eslint-config": "^6.
|
|
43
|
-
"@antfu/ni": "^
|
|
44
|
+
"@antfu/eslint-config": "^7.6.1",
|
|
45
|
+
"@antfu/ni": "^28.2.0",
|
|
44
46
|
"@antfu/utils": "^9.3.0",
|
|
45
|
-
"@types/node": "^
|
|
46
|
-
"@types/react": "^19.2.
|
|
47
|
+
"@types/node": "^25.3.3",
|
|
48
|
+
"@types/react": "^19.2.14",
|
|
47
49
|
"@types/react-dom": "^19.2.3",
|
|
48
50
|
"@vitejs/plugin-react": "^5.1.1",
|
|
49
|
-
"@vitest/browser-playwright": "^4.0.
|
|
50
|
-
"@vitest/coverage-v8": "^4.0.
|
|
51
|
-
"bumpp": "^10.
|
|
52
|
-
"eslint": "^
|
|
53
|
-
"lint-staged": "^16.
|
|
54
|
-
"playwright": "^1.
|
|
55
|
-
"react": "^19.2.
|
|
56
|
-
"react-dom": "^19.2.
|
|
51
|
+
"@vitest/browser-playwright": "^4.0.18",
|
|
52
|
+
"@vitest/coverage-v8": "^4.0.18",
|
|
53
|
+
"bumpp": "^10.4.1",
|
|
54
|
+
"eslint": "^10.0.2",
|
|
55
|
+
"lint-staged": "^16.3.1",
|
|
56
|
+
"playwright": "^1.58.2",
|
|
57
|
+
"react": "^19.2.4",
|
|
58
|
+
"react-dom": "^19.2.4",
|
|
57
59
|
"simple-git-hooks": "^2.13.1",
|
|
58
60
|
"tinyexec": "^1.0.2",
|
|
59
|
-
"tsdown": "^0.
|
|
60
|
-
"tsx": "^4.
|
|
61
|
+
"tsdown": "^0.20.3",
|
|
62
|
+
"tsx": "^4.21.0",
|
|
61
63
|
"typescript": "^5.9.3",
|
|
62
|
-
"vite": "^7.
|
|
63
|
-
"vitest": "^4.0.
|
|
64
|
-
"vitest-browser-react": "^2.0.
|
|
65
|
-
"vitest-package-exports": "^
|
|
66
|
-
"yaml": "^2.8.
|
|
64
|
+
"vite": "^7.3.1",
|
|
65
|
+
"vitest": "^4.0.18",
|
|
66
|
+
"vitest-browser-react": "^2.0.5",
|
|
67
|
+
"vitest-package-exports": "^1.2.0",
|
|
68
|
+
"yaml": "^2.8.2"
|
|
67
69
|
},
|
|
68
70
|
"simple-git-hooks": {
|
|
69
71
|
"pre-commit": "pnpm i --frozen-lockfile --ignore-scripts --offline && npx lint-staged"
|
|
@@ -71,7 +73,6 @@
|
|
|
71
73
|
"lint-staged": {
|
|
72
74
|
"*": "eslint --fix"
|
|
73
75
|
},
|
|
74
|
-
"types": "./dist/index.d.mts",
|
|
75
76
|
"scripts": {
|
|
76
77
|
"build": "tsdown",
|
|
77
78
|
"dev": "tsdown --watch",
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { d as StoreDefine, i as Getters, r as ActionsTree, t as Actions, u as Store } from "./index-CXmAkQQL.mjs";
|
|
2
|
-
|
|
3
|
-
//#region src/define.d.ts
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @description Define a store
|
|
7
|
-
* @example
|
|
8
|
-
* ```tsx
|
|
9
|
-
* const store = defineStore({
|
|
10
|
-
* state: () => ({ count: 0 }),
|
|
11
|
-
* actions: {
|
|
12
|
-
* increment() {
|
|
13
|
-
* this.count++
|
|
14
|
-
* },
|
|
15
|
-
* },
|
|
16
|
-
* })
|
|
17
|
-
*
|
|
18
|
-
* store.increment()
|
|
19
|
-
* console.log(store.$state.count) // 1
|
|
20
|
-
*
|
|
21
|
-
* function Component() {
|
|
22
|
-
* const store = useStore(store)
|
|
23
|
-
* return (
|
|
24
|
-
* <div>
|
|
25
|
-
* <button onClick={store.increment}>Increment</button>
|
|
26
|
-
* <div>{store.count}</div>
|
|
27
|
-
* </div>
|
|
28
|
-
* )
|
|
29
|
-
* }
|
|
30
|
-
*
|
|
31
|
-
* ```
|
|
32
|
-
*/
|
|
33
|
-
declare function defineStore<S extends object, A extends ActionsTree, G extends Getters<S>>(define: StoreDefine<S, A, G>): Store<S, A & Actions<S>, G>;
|
|
34
|
-
//#endregion
|
|
35
|
-
export { defineStore as t };
|
package/dist/define-vminFPaI.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{t as e}from"./plugin-D5lPjOi5.mjs";import{createElement as t}from"react";import{proxy as n,ref as r,subscribe as i,useSnapshot as a}from"valtio";import{subscribeKey as o}from"valtio/utils";function s(s){let c=typeof s.state==`function`?s.state():s.state,l=s.getters||{},u=s.actions||{},d=n(c),f={},p={},m=new WeakSet;for(let e in u)f[e]=r(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 i(d,t=>e(d,t))}function g(e,t){return o(d,e,e=>t(e),!0)}function _(e){typeof e==`function`?e(d):Object.assign(d,e)}function v(e){return t(()=>e(a(d)))}function y(t){e.push(t),S(t)}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 t of e)S(t);return i(e,()=>{for(let t of e)S(t)}),x}export{s as t};
|
package/dist/define.d.mts
DELETED
package/dist/define.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import"./plugin-D5lPjOi5.mjs";import{t as e}from"./define-vminFPaI.mjs";export{e as defineStore};
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { s as Plugin } from "./index-CXmAkQQL.mjs";
|
|
2
|
-
import { Awaitable } from "@hairy/utils";
|
|
3
|
-
|
|
4
|
-
//#region src/plugins/persistent/index.d.ts
|
|
5
|
-
declare function persistent(): Plugin;
|
|
6
|
-
type DeepKeys<T> = T extends object ? { [K in keyof T & string]: T[K] extends object ? K | `${K}.${DeepKeys<T[K]>}` : K }[keyof T & string] : never;
|
|
7
|
-
interface Storage {
|
|
8
|
-
getItem: (key: string) => Awaitable<any>;
|
|
9
|
-
setItem: (key: string, value: any) => Awaitable<void>;
|
|
10
|
-
[key: string]: any;
|
|
11
|
-
}
|
|
12
|
-
interface PersistentOptions<S extends object = Record<string, unknown>> {
|
|
13
|
-
key?: string;
|
|
14
|
-
storage?: Storage;
|
|
15
|
-
paths?: DeepKeys<S>[];
|
|
16
|
-
}
|
|
17
|
-
declare module 'valtio-define/types' {
|
|
18
|
-
interface StoreDefine<S extends object, A extends ActionsTree, G extends Getters<any>> {
|
|
19
|
-
persist?: PersistentOptions<S> | boolean;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
//#endregion
|
|
23
|
-
export { persistent as i, PersistentOptions as n, Storage as r, DeepKeys as t };
|
|
@@ -1 +0,0 @@
|
|
|
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(){return a=>{if(!a.options.persist)return;let o=typeof a.options.persist==`boolean`?{}:a.options.persist;o.key=o.key||i(a.store.$state);let s=o.storage||(typeof localStorage<`u`?localStorage:void 0),c=s?.getItem(o.key),l=!1;c instanceof Promise?c.then(u):u(c);function u(e){Object.assign(a.store.$state,r(e)),l=!0}e(a.store.$state,()=>{if(!l)return;let e=o.paths||Object.keys(a.store.$state),r={};for(let i of e)n(r,i,t(a.store.$state,i));let i=JSON.stringify(r);s?.setItem(o.key,i)})}}var o=a;export{o as t};
|
package/dist/plugin-D5lPjOi5.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{proxy as e}from"valtio";const t=e([]);function n(e){t.push(e)}export{n,t};
|
package/dist/plugin.d.mts
DELETED
package/dist/plugin.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{n as e,t}from"./plugin-D5lPjOi5.mjs";export{t as plugins,e as use};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{t as e}from"../../persistent-Cp0WGzx8.mjs";export{e as default};
|
package/dist/types/index.d.mts
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import { a as GettersReturnType, c as PluginContext, d as StoreDefine, f as Subscribe, i as Getters, l as Signal, n as ActionsOmitThisParameter, o as Patch, p as SubscribeKey, r as ActionsTree, s as Plugin, t as Actions, u as Store } from "../index-CXmAkQQL.mjs";
|
|
2
|
-
export { Actions, ActionsOmitThisParameter, ActionsTree, Getters, GettersReturnType, Patch, Plugin, PluginContext, Signal, Store, StoreDefine, Subscribe, SubscribeKey };
|
package/dist/types/index.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import"../types-Di9Kuaj2.mjs";export{};
|
package/dist/use-BJ-F80_h.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{useSnapshot as e}from"valtio";function t(t){return e(t.$state)}export{t};
|
package/dist/use-DUChuhuv.d.mts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { a as GettersReturnType, i as Getters, t as Actions, u as Store } from "./index-CXmAkQQL.mjs";
|
|
2
|
-
import { Snapshot } from "valtio";
|
|
3
|
-
|
|
4
|
-
//#region src/use.d.ts
|
|
5
|
-
declare function useStore<S extends object, A extends Actions<S>, G extends Getters<S>>(store: Store<S, A, G>): Snapshot<S & GettersReturnType<G> & A>;
|
|
6
|
-
//#endregion
|
|
7
|
-
export { useStore as t };
|
package/dist/use.d.mts
DELETED
package/dist/use.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{t as e}from"./use-BJ-F80_h.mjs";export{e as useStore};
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { u as Store } from "./index-CXmAkQQL.mjs";
|
|
2
|
-
import { Dispatch, SetStateAction } from "react";
|
|
3
|
-
|
|
4
|
-
//#region src/utils.d.ts
|
|
5
|
-
declare function storeToState<S extends object, K$1 extends keyof S>(store: Store<S>, key: K$1): [S[K$1], Dispatch<SetStateAction<S[K$1]>>];
|
|
6
|
-
declare function storeToStates<S extends object>(store: Store<S>): { [K in keyof S]: [S[K], Dispatch<SetStateAction<S[K]>>] };
|
|
7
|
-
//#endregion
|
|
8
|
-
export { storeToStates as n, storeToState as t };
|
package/dist/utils-Cw3GrNQy.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{t as e}from"./use-BJ-F80_h.mjs";function t(t,n){let r=e(t);function i(e){typeof e==`function`?t.$patch(t=>{t[n]=e(t[n])}):t.$patch(t=>{t[n]=e})}return[r[n],i]}function n(e){return Object.fromEntries(Object.keys(e.$state).map(n=>[n,t(e,n)]))}export{n,t};
|
package/dist/utils.d.mts
DELETED
package/dist/utils.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import"./use-BJ-F80_h.mjs";import{n as e,t}from"./utils-Cw3GrNQy.mjs";export{t as storeToState,e as storeToStates};
|
|
File without changes
|