valtio-define 0.3.0 → 0.4.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/dist/index.d.mts +8 -6
- package/dist/index.mjs +8 -4
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -16,8 +16,8 @@ interface StoreDefine<S extends object, A extends Actions<S>, G extends Getters<
|
|
|
16
16
|
actions?: A;
|
|
17
17
|
getters?: G;
|
|
18
18
|
}
|
|
19
|
-
interface StoreOptions {
|
|
20
|
-
persist?: boolean | PersistentOptions
|
|
19
|
+
interface StoreOptions<S extends object = Record<string, unknown>> {
|
|
20
|
+
persist?: boolean | PersistentOptions<S>;
|
|
21
21
|
}
|
|
22
22
|
interface StoreSignal<S, A extends Actions<S>, G extends Getters<S>> {
|
|
23
23
|
<T>(fn: (state: S & GettersReturnType<G>) => T): T;
|
|
@@ -26,6 +26,7 @@ interface StoreSignal<S, A extends Actions<S>, G extends Getters<S>> {
|
|
|
26
26
|
interface StoreSubscribe<S, A extends Actions<S>, G extends Getters<S>> {
|
|
27
27
|
(listener: (state: S & GettersReturnType<G>) => void): () => void;
|
|
28
28
|
status: (listener: (status: ActionsStatus<A>) => void) => () => void;
|
|
29
|
+
key: <K$1 extends keyof S | keyof G>(key: K$1, listener: (state: (S & GettersReturnType<G>)[K$1]) => void) => () => void;
|
|
29
30
|
}
|
|
30
31
|
interface StorePatch<S, G extends Getters<S>> {
|
|
31
32
|
(patch: Partial<S> | ((state: S & GettersReturnType<G>) => void)): void;
|
|
@@ -39,10 +40,11 @@ type Store<S, A extends Actions<S>, G extends Getters<S>> = {
|
|
|
39
40
|
$status: ActionsStatus<A>;
|
|
40
41
|
$signal: StoreSignal<S, A, G>;
|
|
41
42
|
} & ActionsOmitThisParameter<A>;
|
|
42
|
-
interface PersistentOptions {
|
|
43
|
+
interface PersistentOptions<S extends object = Record<string, unknown>> {
|
|
43
44
|
key?: string;
|
|
44
|
-
storage?: Storage
|
|
45
|
-
paths?:
|
|
45
|
+
storage?: Partial<Storage> & Pick<Storage, 'getItem' | 'setItem'>;
|
|
46
|
+
paths?: (keyof S)[];
|
|
47
|
+
initial?: (initialState: S) => any | Promise<any>;
|
|
46
48
|
}
|
|
47
49
|
//#endregion
|
|
48
50
|
//#region src/define-store.d.ts
|
|
@@ -74,7 +76,7 @@ interface PersistentOptions {
|
|
|
74
76
|
*
|
|
75
77
|
* ```
|
|
76
78
|
*/
|
|
77
|
-
declare function defineStore<S extends object, A extends Actions<S>, G extends Getters<S>>(store: StoreDefine<S, A, G> & StoreOptions): Store<S, A, G>;
|
|
79
|
+
declare function defineStore<S extends object, A extends Actions<S>, G extends Getters<S>>(store: StoreDefine<S, A, G> & StoreOptions<S>): Store<S, A, G>;
|
|
78
80
|
//#endregion
|
|
79
81
|
//#region src/persistent.d.ts
|
|
80
82
|
declare function proxyWithPersistent<T extends object>(initialObject: T, options?: PersistentOptions): T;
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createElement } from "react";
|
|
2
2
|
import { proxy, ref, subscribe, useSnapshot } from "valtio";
|
|
3
|
+
import { subscribeKey } from "valtio/utils";
|
|
3
4
|
import { tryParseJson } from "@hairy/utils";
|
|
4
5
|
import { generateStructureId } from "structure-id";
|
|
5
6
|
|
|
@@ -101,7 +102,7 @@ function defineStore(store) {
|
|
|
101
102
|
const $actions = {};
|
|
102
103
|
const $getters = {};
|
|
103
104
|
setupActions($state, actions, $actions, $status);
|
|
104
|
-
setupGetters(
|
|
105
|
+
setupGetters($state, getters, $getters);
|
|
105
106
|
setupStatus($actions, $status);
|
|
106
107
|
function $subscribe(listener) {
|
|
107
108
|
return subscribe($state, () => listener($state));
|
|
@@ -109,6 +110,9 @@ function defineStore(store) {
|
|
|
109
110
|
$subscribe.status = function(listener) {
|
|
110
111
|
return subscribe($status, () => listener($status));
|
|
111
112
|
};
|
|
113
|
+
$subscribe.key = function(key, listener) {
|
|
114
|
+
return subscribeKey($state, key, () => listener($state));
|
|
115
|
+
};
|
|
112
116
|
function $patch(patch) {
|
|
113
117
|
if (typeof patch === "function") patch($state);
|
|
114
118
|
else Object.assign($state, patch);
|
|
@@ -144,13 +148,13 @@ function setupActions($state, actions, $actions, $status) {
|
|
|
144
148
|
});
|
|
145
149
|
}
|
|
146
150
|
}
|
|
147
|
-
function setupGetters(
|
|
151
|
+
function setupGetters($state, getters, $getters) {
|
|
148
152
|
for (const key in getters) {
|
|
149
153
|
Object.defineProperty($getters, key, {
|
|
150
|
-
get: () => state[key],
|
|
154
|
+
get: () => $state[key],
|
|
151
155
|
enumerable: true
|
|
152
156
|
});
|
|
153
|
-
Object.defineProperty(state, key, {
|
|
157
|
+
Object.defineProperty($state, key, {
|
|
154
158
|
get: () => getters[key].call($state),
|
|
155
159
|
enumerable: true
|
|
156
160
|
});
|
package/package.json
CHANGED