@storesjs/stores 0.9.0 → 0.11.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 +55 -29
- package/dist/createBaseStore.d.ts +8 -8
- package/dist/createDerivedStore.d.ts +9 -17
- package/dist/createQueryStore.d.ts +11 -16
- package/dist/createVirtualStore.d.ts +1 -2
- package/dist/env.native.d.ts +0 -2
- package/dist/env.web.d.ts +0 -1
- package/dist/hooks/useListen.d.ts +2 -2
- package/dist/index.d.ts +1 -16
- package/dist/index.shared.d.ts +14 -0
- package/dist/index.vanilla.d.ts +2 -0
- package/dist/internal/baseStore.d.ts +2 -0
- package/dist/{config.d.ts → internal/config.d.ts} +5 -11
- package/dist/{utils → internal}/createStoreActions.d.ts +4 -5
- package/dist/{derivedStore → internal/derivedStore}/deriveProxy.d.ts +2 -2
- package/dist/{derivedStore → internal/derivedStore}/pathFinder.d.ts +1 -2
- package/dist/internal/derivedStore.d.ts +2 -0
- package/dist/{errors.d.ts → internal/errors.d.ts} +0 -4
- package/dist/{logger.d.ts → internal/logger.d.ts} +0 -2
- package/dist/internal/middleware/createHydrationGate.d.ts +20 -0
- package/dist/internal/queryStore/queryParams.d.ts +18 -0
- package/dist/internal/queryStore/queryStoreDefaults.d.ts +8 -0
- package/dist/internal/queryStore.d.ts +2 -0
- package/dist/internal/runtime.d.ts +12 -0
- package/dist/internal/storage/storageCreators.d.ts +12 -0
- package/dist/{utils → internal}/storeUtils.d.ts +3 -21
- package/dist/internal/sync/browserSyncEngine.d.ts +3 -0
- package/dist/internal/sync/noopSyncEngine.d.ts +3 -0
- package/dist/internal/sync/syncEnhancer.d.ts +26 -0
- package/dist/{sync → internal/sync}/syncUtils.d.ts +1 -1
- package/dist/internal/types/internalSubscribeTypes.d.ts +16 -0
- package/dist/internal/utils/persistUtils.d.ts +9 -0
- package/dist/internal/virtualStore.d.ts +2 -0
- package/dist/native/index.js +1 -1
- package/dist/native/index.mjs +1 -1
- package/dist/queryStore/classes/SubscriptionManager.d.ts +3 -31
- package/dist/queryStore/queryKey.d.ts +10 -0
- package/dist/queryStore/queryParam.d.ts +37 -0
- package/dist/queryStore/types.d.ts +12 -53
- package/dist/store/attachStoreHook.react.d.ts +5 -0
- package/dist/store/attachStoreHook.vanilla.d.ts +6 -0
- package/dist/store/batchStoreNotifications.native.d.ts +3 -0
- package/dist/store/batchStoreNotifications.web.d.ts +2 -0
- package/dist/store/cascadeScheduler.d.ts +38 -0
- package/dist/store/createStore.d.ts +6 -0
- package/dist/store/persist.d.ts +14 -0
- package/dist/store/stateUpdate.d.ts +10 -0
- package/dist/store/types.d.ts +108 -0
- package/dist/types/setState.d.ts +22 -0
- package/dist/types/subscribe.d.ts +27 -0
- package/dist/types/useStoreCallSignatures.d.ts +2 -0
- package/dist/types/useStoreCallSignatures.react.d.ts +6 -0
- package/dist/types/useStoreCallSignatures.vanilla.d.ts +2 -0
- package/dist/types/utils.d.ts +4 -0
- package/dist/types.d.ts +46 -94
- package/dist/utils/createMicrotaskScheduler.d.ts +2 -1
- package/dist/utils/time.d.ts +0 -7
- package/dist/vanilla/createBaseStore.d.ts +35 -0
- package/dist/vanilla/createDerivedStore.d.ts +62 -0
- package/dist/vanilla/createQueryStore.d.ts +110 -0
- package/dist/vanilla/createVirtualStore.d.ts +39 -0
- package/dist/vanilla/env.web.d.ts +6 -0
- package/dist/vanilla/index.d.ts +1 -0
- package/dist/vanilla/index.js +1 -0
- package/dist/vanilla/index.mjs +1 -0
- package/dist/vanilla/index.shared.d.ts +13 -0
- package/dist/vanilla/index.vanilla.d.ts +1 -0
- package/dist/vanilla/internal/baseStore.d.ts +1 -0
- package/dist/vanilla/internal/config.d.ts +63 -0
- package/dist/vanilla/internal/createStoreActions.d.ts +23 -0
- package/dist/vanilla/internal/derivedStore/deriveProxy.d.ts +14 -0
- package/dist/vanilla/internal/derivedStore/pathFinder.d.ts +25 -0
- package/dist/vanilla/internal/derivedStore.d.ts +1 -0
- package/dist/vanilla/internal/errors.d.ts +9 -0
- package/dist/vanilla/internal/logger.d.ts +9 -0
- package/dist/{middleware → vanilla/internal/middleware}/createHydrationGate.d.ts +1 -2
- package/dist/vanilla/internal/queryStore/queryParams.d.ts +17 -0
- package/dist/{queryStore → vanilla/internal/queryStore}/queryStoreDefaults.d.ts +1 -2
- package/dist/vanilla/internal/queryStore.d.ts +1 -0
- package/dist/vanilla/internal/runtime.d.ts +11 -0
- package/dist/vanilla/internal/storage/storageCreators.d.ts +11 -0
- package/dist/vanilla/internal/storeUtils.d.ts +45 -0
- package/dist/vanilla/internal/sync/browserSyncEngine.d.ts +2 -0
- package/dist/vanilla/internal/sync/noopSyncEngine.d.ts +2 -0
- package/dist/{sync → vanilla/internal/sync}/syncEnhancer.d.ts +2 -3
- package/dist/vanilla/internal/types/internalSubscribeTypes.d.ts +15 -0
- package/dist/{utils → vanilla/internal/utils}/persistUtils.d.ts +1 -2
- package/dist/vanilla/internal/virtualStore.d.ts +1 -0
- package/dist/vanilla/queryStore/classes/SubscriptionManager.d.ts +38 -0
- package/dist/vanilla/queryStore/queryKey.d.ts +9 -0
- package/dist/vanilla/queryStore/queryParam.d.ts +36 -0
- package/dist/vanilla/queryStore/types.d.ts +360 -0
- package/dist/vanilla/storage/storageTypes.d.ts +12 -0
- package/dist/vanilla/store/attachStoreHook.d.ts +5 -0
- package/dist/vanilla/store/attachStoreHook.vanilla.d.ts +5 -0
- package/dist/vanilla/store/batchStoreNotifications.web.d.ts +1 -0
- package/dist/{derivedStore → vanilla/store}/cascadeScheduler.d.ts +12 -6
- package/dist/vanilla/store/createStore.d.ts +5 -0
- package/dist/vanilla/store/persist.d.ts +13 -0
- package/dist/vanilla/store/stateUpdate.d.ts +9 -0
- package/dist/vanilla/store/types.d.ts +107 -0
- package/dist/vanilla/storesStorage.web.d.ts +2 -0
- package/dist/vanilla/sync/types.d.ts +114 -0
- package/dist/vanilla/types/functions.d.ts +23 -0
- package/dist/vanilla/types/objects.d.ts +10 -0
- package/dist/vanilla/types/setState.d.ts +21 -0
- package/dist/vanilla/types/subscribe.d.ts +26 -0
- package/dist/vanilla/types/useStoreCallSignatures.d.ts +1 -0
- package/dist/vanilla/types/useStoreCallSignatures.vanilla.d.ts +1 -0
- package/dist/vanilla/types/utils.d.ts +12 -0
- package/dist/vanilla/types.d.ts +287 -0
- package/dist/vanilla/utils/core.d.ts +25 -0
- package/dist/vanilla/utils/createAsyncMicrotaskScheduler.d.ts +34 -0
- package/dist/vanilla/utils/createMicrotaskScheduler.d.ts +28 -0
- package/dist/vanilla/utils/debounce.d.ts +21 -0
- package/dist/vanilla/utils/equality.d.ts +37 -0
- package/dist/vanilla/utils/hydrationCoordinator.d.ts +12 -0
- package/dist/vanilla/utils/promiseUtils.d.ts +1 -0
- package/dist/vanilla/utils/serialization.d.ts +13 -0
- package/dist/vanilla/utils/stringUtils.d.ts +6 -0
- package/dist/vanilla/utils/time.d.ts +36 -0
- package/dist/web/chrome.js +1 -1
- package/dist/web/chrome.mjs +1 -1
- package/dist/web/chunk-FENSEQ3V.mjs +1 -0
- package/dist/web/chunk-FPYZVFBM.mjs +1 -0
- package/dist/web/chunk-PLDNKWGX.mjs +1 -0
- package/dist/web/index.js +1 -1
- package/dist/web/index.mjs +1 -1
- package/dist/web/internal/runtime.js +1 -0
- package/dist/web/internal/runtime.mjs +1 -0
- package/package.json +13 -11
- package/dist/createStoreWithEqualityFn.d.ts +0 -5
- package/dist/hooks/compareHooks.d.ts +0 -50
- package/dist/queryStore/signalTypes.d.ts +0 -13
- package/dist/signal.d.ts +0 -11
- package/dist/storage/storageCreators.d.ts +0 -22
- package/dist/sync/browserSyncEngine.d.ts +0 -3
- package/dist/sync/noopSyncEngine.d.ts +0 -3
- package/dist/web/chunk-3U5A7HQV.mjs +0 -1
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import type { StorageValue } from '../storage/storageTypes';
|
|
2
|
+
import type { SetStateOverloads } from '../types/setState';
|
|
3
|
+
import type { SubscribeOverloads } from '../types/subscribe';
|
|
4
|
+
/**
|
|
5
|
+
* Store object contract shared by creators, middleware, and internal store utilities.
|
|
6
|
+
*/
|
|
7
|
+
export type StoreApi<S> = {
|
|
8
|
+
getInitialState: () => S;
|
|
9
|
+
getState: () => S;
|
|
10
|
+
setState: SetStateOverloads<S>;
|
|
11
|
+
subscribe: SubscribeOverloads<S>;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Callback that builds store state from the set/get/store API supplied during construction.
|
|
15
|
+
*/
|
|
16
|
+
export type StateCreator<S, Mutators extends StoreMutators = [], StoreMutatorOutput extends StoreMutators = [], U = S> = ((setState: Get<Mutate<StoreApi<S>, Mutators>, 'setState', never>, getState: Get<Mutate<StoreApi<S>, Mutators>, 'getState', never>, store: Mutate<StoreApi<S>, Mutators>) => U) & {
|
|
17
|
+
$$storeMutators?: StoreMutatorOutput;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Synchronous storage adapter consumed by the persistence wrapper.
|
|
21
|
+
*/
|
|
22
|
+
export type SyncPersistStorage<State, PersistedState = Partial<State>> = {
|
|
23
|
+
getItem: (name: string) => StorageValue<PersistedState> | null;
|
|
24
|
+
removeItem: (name: string) => void;
|
|
25
|
+
setItem: (name: string, state: State, version: number | undefined) => void;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Asynchronous storage adapter consumed by the persistence wrapper.
|
|
29
|
+
*/
|
|
30
|
+
export type AsyncPersistStorage<State, PersistedState = Partial<State>> = {
|
|
31
|
+
getItem: (name: string) => Promise<StorageValue<PersistedState> | null>;
|
|
32
|
+
removeItem: (name: string) => Promise<void>;
|
|
33
|
+
setItem: (name: string, state: State, version: number | undefined) => Promise<void>;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Accepted storage adapter types for sync or async persistence.
|
|
37
|
+
*/
|
|
38
|
+
export type PersistStorage<State, PersistedState = Partial<State>> = AsyncPersistStorage<State, PersistedState> | SyncPersistStorage<State, PersistedState>;
|
|
39
|
+
/**
|
|
40
|
+
* Resolved storage config for persisted stores.
|
|
41
|
+
*/
|
|
42
|
+
export type PersistStorageConfig<State, PersistedState extends Partial<State>> = {
|
|
43
|
+
async: false;
|
|
44
|
+
persistStorage: SyncPersistStorage<State, PersistedState>;
|
|
45
|
+
version: number;
|
|
46
|
+
} | {
|
|
47
|
+
async: true;
|
|
48
|
+
persistStorage: AsyncPersistStorage<State, PersistedState>;
|
|
49
|
+
version: number;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Persistence options consumed by the persistence wrapper.
|
|
53
|
+
*/
|
|
54
|
+
export type PersistOptions<S, PersistedState = Partial<S>> = {
|
|
55
|
+
merge?: (persistedState: PersistedState | undefined, currentState: S) => S;
|
|
56
|
+
migrate?: (persistedState: PersistedState, version: number) => PersistedState | Promise<PersistedState>;
|
|
57
|
+
name: string;
|
|
58
|
+
onRehydrateStorage?: (state: S) => ((state?: S, error?: unknown) => void) | void;
|
|
59
|
+
skipHydration?: boolean;
|
|
60
|
+
storage: PersistStorage<S, PersistedState>;
|
|
61
|
+
version: number;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Methods attached by the persistence wrapper before async hydration state is added.
|
|
65
|
+
*/
|
|
66
|
+
export type PersistMethods<S, PersistedState> = {
|
|
67
|
+
clearStorage: () => void;
|
|
68
|
+
getOptions: () => Partial<PersistOptions<S, PersistedState>>;
|
|
69
|
+
hasHydrated: () => boolean;
|
|
70
|
+
onHydrate: (listener: (state: S) => void) => () => void;
|
|
71
|
+
onFinishHydration: (listener: (state: S) => void) => () => void;
|
|
72
|
+
rehydrate: () => Promise<void> | void;
|
|
73
|
+
setOptions: (options: Partial<PersistOptions<S, PersistedState>>) => void;
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* Store shape after the persistence wrapper adds persistence methods, write-through `setState`,
|
|
77
|
+
* and async hydration state when applicable.
|
|
78
|
+
*/
|
|
79
|
+
export type WithPersist<Store, PersistedState, PersistReturn extends void | Promise<void>> = Store extends StoreApi<infer S> ? Write<Store, {
|
|
80
|
+
persist: PersistMethods<S, PersistedState> & HydrationPromise<PersistReturn>;
|
|
81
|
+
setState: SetStateOverloads<S, PersistReturn>;
|
|
82
|
+
}> : Store;
|
|
83
|
+
type HydrationPromise<PersistReturn> = PersistReturn extends Promise<void> ? {
|
|
84
|
+
/** Invoke to get a promise that resolves once hydration completes. */
|
|
85
|
+
hydrationPromise: () => Promise<void>;
|
|
86
|
+
} : {
|
|
87
|
+
hydrationPromise?: undefined;
|
|
88
|
+
};
|
|
89
|
+
type Get<T, K, Fallback> = K extends keyof T ? T[K] : Fallback;
|
|
90
|
+
type Write<T, U> = Omit<T, keyof U> & U;
|
|
91
|
+
type PersistMutator<Store, Args> = Args extends [infer PersistedState, infer PersistReturn extends void | Promise<void>] ? WithPersist<Store, PersistedState, PersistReturn> : Store;
|
|
92
|
+
type StoreMutatorIdentifier = keyof StoreMutatorMap<unknown, unknown>;
|
|
93
|
+
/**
|
|
94
|
+
* Mapping from store mutator identifiers to the store API shape they add.
|
|
95
|
+
*/
|
|
96
|
+
export interface StoreMutatorMap<Store, Args> {
|
|
97
|
+
'stores/persist': PersistMutator<Store, Args>;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Ordered list of store mutators applied to a state creator.
|
|
101
|
+
*/
|
|
102
|
+
export type StoreMutators = [StoreMutatorIdentifier, unknown][];
|
|
103
|
+
/**
|
|
104
|
+
* Applies a list of store mutators to a store API type.
|
|
105
|
+
*/
|
|
106
|
+
export type Mutate<Store, Mutators> = number extends Mutators['length' & keyof Mutators] ? Store : Mutators extends [] ? Store : Mutators extends [[infer Identifier, infer Args], ...infer Rest] ? Identifier extends StoreMutatorIdentifier ? Rest extends StoreMutators ? Mutate<StoreMutatorMap<Store, Args>[Identifier], Rest> : StoreMutatorMap<Store, Args>[Identifier] : Store : Store;
|
|
107
|
+
export {};
|
|
108
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { BivariantMethod } from './functions';
|
|
2
|
+
export type SetPartial<S> = S | Partial<S> | BivariantMethod<{
|
|
3
|
+
updater(state: S): S | Partial<S>;
|
|
4
|
+
}>;
|
|
5
|
+
export type SetFull<S> = S | BivariantMethod<{
|
|
6
|
+
updater(state: S): S;
|
|
7
|
+
}>;
|
|
8
|
+
export type SetStateReplaceArgs<S, ExtraArgs extends unknown[] = []> = [update: SetFull<S>, replace: true, ...extraArgs: ExtraArgs];
|
|
9
|
+
export type SetStatePartialArgs<S, ExtraArgs extends unknown[] = []> = [update: SetPartial<S>, replace?: false, ...extraArgs: ExtraArgs];
|
|
10
|
+
export type SetStateArgs<S, ExtraArgs extends unknown[] = []> = SetStatePartialArgs<S, ExtraArgs> | SetStateReplaceArgs<S, ExtraArgs>;
|
|
11
|
+
/**
|
|
12
|
+
* Rest-argument form of `setState`, used when forwarding calls.
|
|
13
|
+
*/
|
|
14
|
+
export type SetState<S, ExtraArgs extends unknown[] = [], PersistReturn extends Promise<void> | void = void> = (...args: SetStateArgs<S, ExtraArgs>) => PersistReturn;
|
|
15
|
+
/**
|
|
16
|
+
* Store `setState` method overloads for partial updates and full replacement.
|
|
17
|
+
*/
|
|
18
|
+
export type SetStateOverloads<S, PersistReturn extends Promise<void> | void = void> = BivariantMethod<{
|
|
19
|
+
setState(update: SetPartial<S>, replace?: false): PersistReturn;
|
|
20
|
+
setState(update: SetFull<S>, replace: true): PersistReturn;
|
|
21
|
+
}>;
|
|
22
|
+
//# sourceMappingURL=setState.d.ts.map
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export type Listener<S> = (state: S, prevState: S) => void;
|
|
2
|
+
export type Selector<S, Selected> = (state: S) => Selected;
|
|
3
|
+
export type EqualityFn<T = unknown> = (a: T, b: T) => boolean;
|
|
4
|
+
/**
|
|
5
|
+
* Minimum store API required for subscription.
|
|
6
|
+
*/
|
|
7
|
+
export type SubscribableStore = {
|
|
8
|
+
subscribe(selector: Selector<unknown, unknown>, listener: () => void, options?: SubscribeOptions<unknown>): UnsubscribeFn;
|
|
9
|
+
};
|
|
10
|
+
export type SubscribeOptions<Selected> = {
|
|
11
|
+
equalityFn?: EqualityFn<Selected>;
|
|
12
|
+
fireImmediately?: boolean;
|
|
13
|
+
};
|
|
14
|
+
export type ListenerArgs<S> = [listener: Listener<S>];
|
|
15
|
+
export type SelectorArgs<S, Selected> = [
|
|
16
|
+
selector: Selector<S, Selected>,
|
|
17
|
+
listener: Listener<Selected>,
|
|
18
|
+
options?: SubscribeOptions<Selected>
|
|
19
|
+
];
|
|
20
|
+
export type SubscribeArgs<S, Selected = unknown> = ListenerArgs<S> | SelectorArgs<S, Selected>;
|
|
21
|
+
export type UnsubscribeFn = () => void;
|
|
22
|
+
export type SubscribeFn<S, Selected = S> = (...args: SubscribeArgs<S, Selected>) => UnsubscribeFn;
|
|
23
|
+
export type SubscribeOverloads<S> = {
|
|
24
|
+
(listener: Listener<S>): UnsubscribeFn;
|
|
25
|
+
<Selected>(selector: Selector<S, Selected>, listener: Listener<Selected>, options?: SubscribeOptions<Selected>): UnsubscribeFn;
|
|
26
|
+
};
|
|
27
|
+
//# sourceMappingURL=subscribe.d.ts.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { EqualityFn, Selector } from './subscribe';
|
|
2
|
+
export type UseStoreCallSignatures<State> = {
|
|
3
|
+
(): State;
|
|
4
|
+
<Selected>(selector: Selector<State, Selected>, equalityFn?: EqualityFn<Selected>): Selected;
|
|
5
|
+
};
|
|
6
|
+
//# sourceMappingURL=useStoreCallSignatures.react.d.ts.map
|
package/dist/types/utils.d.ts
CHANGED
|
@@ -6,4 +6,8 @@ export declare function hasOwn<T extends Record<string, unknown>, K extends stri
|
|
|
6
6
|
* Checks prototype is `Object.prototype` or `null`. Excludes arrays, class instances, etc.
|
|
7
7
|
*/
|
|
8
8
|
export declare function isPlainObject(value: unknown): value is Record<string, unknown>;
|
|
9
|
+
/**
|
|
10
|
+
* Checks for non-null, non-array objects.
|
|
11
|
+
*/
|
|
12
|
+
export declare function isRecordLike(value: unknown): value is Record<string, unknown>;
|
|
9
13
|
//# sourceMappingURL=utils.d.ts.map
|
package/dist/types.d.ts
CHANGED
|
@@ -1,59 +1,44 @@
|
|
|
1
|
-
import type { Mutate, StateCreator as ZustandStateCreator, StoreApi, StoreMutatorIdentifier } from 'zustand';
|
|
2
|
-
import type { PersistOptions } from 'zustand/middleware';
|
|
3
1
|
import type { StorageValue } from './storage/storageTypes';
|
|
2
|
+
import type { PersistOptions, StateCreator as StoreStateCreator, StoreApi, StoreMutators, WithPersist } from './store/types';
|
|
4
3
|
import type { SyncConfig } from './sync/types';
|
|
5
|
-
type
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
type
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
4
|
+
import type { FunctionKeys } from './types/functions';
|
|
5
|
+
import type { SetFull, SetPartial, SetStateArgs } from './types/setState';
|
|
6
|
+
import type { EqualityFn, Selector, SubscribeOverloads } from './types/subscribe';
|
|
7
|
+
import type { UseStoreCallSignatures } from './types/useStoreCallSignatures';
|
|
8
|
+
export type { StoreMutators } from './store/types';
|
|
9
|
+
export type * from './types/setState';
|
|
10
|
+
export type * from './types/subscribe';
|
|
11
|
+
export type * from './types/useStoreCallSignatures';
|
|
12
|
+
export type StoreMutatorsWithSelector<Mutators extends StoreMutators = StoreMutators> = Mutators;
|
|
13
|
+
export type StateCreator<S, U = S> = StoreStateCreator<S, [], [], U>;
|
|
14
|
+
export type UseBoundStoreWithEqualityFn<Store extends {
|
|
15
|
+
getState: () => unknown;
|
|
16
|
+
}, State = InferStoreState<Store>> = UseStoreCallSignatures<State> & Store;
|
|
17
|
+
export type BaseStore<S> = UseBoundStoreWithEqualityFn<StoreApi<S>> & {
|
|
18
|
+
subscribe: SubscribeOverloads<S>;
|
|
13
19
|
};
|
|
14
|
-
type
|
|
15
|
-
|
|
16
|
-
setState: {
|
|
17
|
-
(...args: infer SetPartialArgs): infer _;
|
|
18
|
-
(...args: infer SetFullArgs): infer _;
|
|
19
|
-
};
|
|
20
|
-
} ? {
|
|
21
|
-
setState(...args: SetPartialArgs): PersistReturn;
|
|
22
|
-
setState(...args: SetFullArgs): PersistReturn;
|
|
23
|
-
persist: {
|
|
24
|
-
clearStorage: () => void;
|
|
25
|
-
getOptions: () => Partial<PersistOptions<S, PersistedState>>;
|
|
26
|
-
hasHydrated: () => boolean;
|
|
27
|
-
onHydrate: (listener: (state: S) => void) => () => void;
|
|
28
|
-
onFinishHydration: (listener: (state: S) => void) => () => void;
|
|
29
|
-
rehydrate: () => Promise<void> | void;
|
|
30
|
-
setOptions: (options: Partial<PersistOptions<S, PersistedState, PersistReturn>>) => void;
|
|
31
|
-
} & HydrationPromise<PersistReturn>;
|
|
32
|
-
} : never;
|
|
33
|
-
export type StateCreator<S, U = S> = ZustandStateCreator<S, [SubscribeWithSelector], [SubscribeWithSelector], U>;
|
|
34
|
-
export type UseBoundStoreWithEqualityFn<Store extends StoreApi<unknown>, State = InferStoreState<Store>> = UseStoreCallSignatures<State> & Store;
|
|
35
|
-
export type BaseStore<S, ExtraSubscribeOptions extends boolean = false> = UseBoundStoreWithEqualityFn<Mutate<StoreApi<S>, [SubscribeWithSelector]>> & {
|
|
36
|
-
subscribe: SubscribeOverloads<S, ExtraSubscribeOptions>;
|
|
37
|
-
};
|
|
38
|
-
export type PersistedStore<S, PersistedState = Partial<S>, PersistReturn = unknown, ExtraSubscribeOptions extends boolean = false> = UseStoreCallSignatures<S> & Omit<BaseStore<S, ExtraSubscribeOptions>, 'setState' | 'persist'> & WithPersist<BaseStore<S, ExtraSubscribeOptions>, PersistedState, PersistReturn>;
|
|
39
|
-
export type Store<S, PersistedState extends Partial<S> = never, PersistReturn = unknown, ExtraSubscribeOptions extends boolean = false> = [
|
|
20
|
+
export type PersistedStore<S, PersistedState = Partial<S>, PersistReturn extends void | Promise<void> = void | Promise<void>> = UseStoreCallSignatures<S> & WithPersist<BaseStore<S>, PersistedState, PersistReturn>;
|
|
21
|
+
export type Store<S, PersistedState extends Partial<S> = never, PersistReturn extends void | Promise<void> = void | Promise<void>> = [
|
|
40
22
|
PersistedState
|
|
41
|
-
] extends [never] ? BaseStore<S
|
|
42
|
-
|
|
43
|
-
|
|
23
|
+
] extends [never] ? BaseStore<S> : PersistedStore<S, PersistedState, PersistReturn>;
|
|
24
|
+
/**
|
|
25
|
+
* Store returned by creators that accept optional persistence options.
|
|
26
|
+
* `persist` exists only when persistence is configured.
|
|
27
|
+
*/
|
|
28
|
+
export type OptionallyPersistedStore<S, PersistedState, PersistReturn extends void | Promise<void> = void> = UseStoreCallSignatures<S> & Omit<BaseStore<S>, 'setState'> & {
|
|
29
|
+
persist?: PersistedStore<S, PersistedState, PersistReturn>['persist'];
|
|
30
|
+
setState(update: SetPartial<S>, replace?: false): PersistReturn;
|
|
31
|
+
setState(update: SetFull<S>, replace: true): PersistReturn;
|
|
44
32
|
};
|
|
45
33
|
export type NoInfer<T> = [T][T extends unknown ? 0 : never];
|
|
46
34
|
export type Timeout = ReturnType<typeof setTimeout>;
|
|
47
|
-
export type
|
|
48
|
-
export type Selector<S, Selected> = (state: S) => Selected;
|
|
49
|
-
export type EqualityFn<T = unknown> = (a: T, b: T) => boolean;
|
|
50
|
-
export type UseStoreCallSignatures<S> = {
|
|
51
|
-
(): S;
|
|
52
|
-
<Selected>(selector: Selector<S, Selected>, equalityFn?: EqualityFn<Selected>): Selected;
|
|
53
|
-
};
|
|
54
|
-
export type InferStoreState<Store extends StoreApi<unknown>> = Store extends {
|
|
35
|
+
export type InferStoreState<Store> = Store extends {
|
|
55
36
|
getState: () => infer T;
|
|
56
37
|
} ? T : never;
|
|
38
|
+
/**
|
|
39
|
+
* Action methods returned by `createStoreActions(store)`.
|
|
40
|
+
*/
|
|
41
|
+
export type StoreActions<Store extends StoreApi<unknown>> = Pick<InferStoreState<Store>, FunctionKeys<InferStoreState<Store>>>;
|
|
57
42
|
/**
|
|
58
43
|
* Extracts a store's `setState` return type.
|
|
59
44
|
* - `void` for non-persisted or synchronous persisted stores.
|
|
@@ -67,45 +52,15 @@ export type InferSetStateReturn<Store> = Store extends {
|
|
|
67
52
|
* Returns `never` if the store doesn't use persistence.
|
|
68
53
|
*/
|
|
69
54
|
export type InferPersistedState<PersistedStore> = PersistedStore extends Store<infer _, infer PersistedState> ? PersistedState : never;
|
|
70
|
-
export type SetPartial<S> = Partial<S> | ((state: S) => Partial<S>);
|
|
71
|
-
export type SetFull<S> = S | ((state: S) => S);
|
|
72
|
-
export type SetStateReplaceArgs<S, ExtraArgs extends unknown[] = []> = [update: SetFull<S>, replace: true, ...extraArgs: ExtraArgs];
|
|
73
|
-
export type SetStatePartialArgs<S, ExtraArgs extends unknown[] = []> = [update: SetPartial<S>, replace?: false, ...extraArgs: ExtraArgs];
|
|
74
|
-
export type SetStateArgs<S, ExtraArgs extends unknown[] = []> = SetStatePartialArgs<S, ExtraArgs> | SetStateReplaceArgs<S, ExtraArgs>;
|
|
75
|
-
export type SetState<S, ExtraArgs extends unknown[] = [], PersistReturn extends Promise<void> | void = void> = (...args: SetStateArgs<S, ExtraArgs>) => PersistReturn;
|
|
76
|
-
export type SetStateOverloads<S, PersistReturn extends Promise<void> | void = void> = {
|
|
77
|
-
(update: SetPartial<S>, replace?: false): PersistReturn;
|
|
78
|
-
(update: SetFull<S>, replace: true): PersistReturn;
|
|
79
|
-
};
|
|
80
|
-
export type WithAsyncSet<Store extends StoreApi<unknown>, S, PersistedState, PersistReturn> = Omit<Store, 'persist' | 'setState'> & {
|
|
81
|
-
persist?: PersistedStore<S, PersistedState, PersistReturn, false>['persist'];
|
|
82
|
-
setState(update: SetPartial<InferStoreState<Store>>, replace?: false): PersistReturn;
|
|
83
|
-
setState(update: SetFull<InferStoreState<Store>>, replace: true): PersistReturn;
|
|
84
|
-
};
|
|
85
55
|
/**
|
|
86
|
-
*
|
|
56
|
+
* Reads store state and records dependencies for tracked computations.
|
|
57
|
+
* - `$(store, selector, equalityFn?)` depends on the selected state
|
|
58
|
+
* - `$(store)` depends on accessed state paths
|
|
87
59
|
*/
|
|
88
|
-
export type
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
export type SubscribeOptions<Selected> = {
|
|
92
|
-
equalityFn?: EqualityFn<Selected>;
|
|
93
|
-
fireImmediately?: boolean;
|
|
94
|
-
isDerivedStore?: boolean;
|
|
60
|
+
export type DeriveGetter = {
|
|
61
|
+
<Store extends StoreApi<InferStoreState<Store>>>(store: Store): InferStoreState<Store>;
|
|
62
|
+
<Store extends StoreApi<InferStoreState<Store>>, Selected>(store: Store, selector: Selector<InferStoreState<Store>, Selected>, equalityFn?: EqualityFn<Selected>): Selected;
|
|
95
63
|
};
|
|
96
|
-
export type ListenerArgs<S> = [listener: Listener<S>];
|
|
97
|
-
export type SelectorArgs<S, Selected> = [
|
|
98
|
-
selector: Selector<S, Selected>,
|
|
99
|
-
listener: Listener<Selected>,
|
|
100
|
-
options?: SubscribeOptions<Selected>
|
|
101
|
-
];
|
|
102
|
-
export type SubscribeOverloads<S, ExtraOptions extends boolean = false> = {
|
|
103
|
-
(listener: Listener<S>): UnsubscribeFn<ExtraOptions>;
|
|
104
|
-
<Selected>(selector: Selector<S, Selected>, listener: Listener<Selected>, options?: SubscribeOptions<Selected>): UnsubscribeFn<ExtraOptions>;
|
|
105
|
-
};
|
|
106
|
-
export type SubscribeArgs<S, Selected = unknown> = ListenerArgs<S> | SelectorArgs<S, Selected>;
|
|
107
|
-
export type UnsubscribeFn<Options extends boolean = false> = Options extends true ? (skipAbortFetch?: boolean) => void : () => void;
|
|
108
|
-
export type SubscribeFn<S, Selected = S> = (...args: SubscribeArgs<S, Selected>) => UnsubscribeFn;
|
|
109
64
|
export type DerivedStore<S> = WithFlushUpdates<ReadOnlyDerivedStore<BaseStore<S>>>;
|
|
110
65
|
export type WithFlushUpdates<Store extends StoreApi<unknown>> = Store & {
|
|
111
66
|
/**
|
|
@@ -123,8 +78,7 @@ export type WithFlushUpdates<Store extends StoreApi<unknown>> = Store & {
|
|
|
123
78
|
};
|
|
124
79
|
export type WithGetSnapshot<Store extends StoreApi<unknown>> = Store & {
|
|
125
80
|
/**
|
|
126
|
-
*
|
|
127
|
-
* store when it gets the initial state before subscribing to the store.
|
|
81
|
+
* Reads the current snapshot while activating lazy derived stores before subscription.
|
|
128
82
|
*/
|
|
129
83
|
getSnapshot: () => InferStoreState<Store>;
|
|
130
84
|
};
|
|
@@ -199,7 +153,7 @@ export type Serializer<SerializedState> = <PersistedState>(storageValue: Storage
|
|
|
199
153
|
* Synchronous storage interface.
|
|
200
154
|
* Used for localStorage and MMKV implementations.
|
|
201
155
|
*/
|
|
202
|
-
export
|
|
156
|
+
export type SyncStorageInterface<SerializedState = unknown> = {
|
|
203
157
|
readonly async?: false;
|
|
204
158
|
/**
|
|
205
159
|
* Adapter-level deserializer used when a store does not supply its own.
|
|
@@ -217,7 +171,7 @@ export interface SyncStorageInterface<SerializedState = unknown> {
|
|
|
217
171
|
get(key: string): SerializedState | undefined;
|
|
218
172
|
getAllKeys(): string[];
|
|
219
173
|
set(key: string, value: SerializedState): void;
|
|
220
|
-
}
|
|
174
|
+
};
|
|
221
175
|
/**
|
|
222
176
|
* Asynchronous storage interface.
|
|
223
177
|
* Used for Chrome storage and other async storage implementations.
|
|
@@ -244,7 +198,7 @@ export type AsyncStorageInterface<SerializedState = unknown> = {
|
|
|
244
198
|
/**
|
|
245
199
|
* Configuration options for creating a persistable store.
|
|
246
200
|
*/
|
|
247
|
-
export type PersistConfig<S, PersistedState = Partial<S>, PersistReturn = void> = {
|
|
201
|
+
export type PersistConfig<S, PersistedState extends Partial<S> = Partial<S>, PersistReturn = void> = {
|
|
248
202
|
/**
|
|
249
203
|
* A function to convert the serialized value back into the state object.
|
|
250
204
|
* If not provided, the storage adapter's `deserializer` is used before falling back to the default implementation.
|
|
@@ -252,7 +206,7 @@ export type PersistConfig<S, PersistedState = Partial<S>, PersistReturn = void>
|
|
|
252
206
|
deserializer?: (serializedState: unknown) => StorageValue<PersistedState>;
|
|
253
207
|
/**
|
|
254
208
|
* A function to merge persisted state with current state during hydration.
|
|
255
|
-
* By default,
|
|
209
|
+
* By default, persisted state is shallow-merged into the current state.
|
|
256
210
|
*/
|
|
257
211
|
merge?: PersistOptions<S, PersistedState>['merge'];
|
|
258
212
|
/**
|
|
@@ -270,7 +224,7 @@ export type PersistConfig<S, PersistedState = Partial<S>, PersistReturn = void>
|
|
|
270
224
|
* A function that determines which parts of the state should be persisted.
|
|
271
225
|
* By default, the entire state is persisted.
|
|
272
226
|
*/
|
|
273
|
-
partialize?: (state: S) => PersistedState;
|
|
227
|
+
partialize?: (state: Readonly<S>) => PersistedState;
|
|
274
228
|
/**
|
|
275
229
|
* The throttle rate for the persist operation in milliseconds.
|
|
276
230
|
* @default iOS: time.seconds(3) | Android: time.seconds(5)
|
|
@@ -300,7 +254,7 @@ export type PersistConfig<S, PersistedState = Partial<S>, PersistReturn = void>
|
|
|
300
254
|
export type EnforceStorageKey<Options> = {
|
|
301
255
|
storageKey: string;
|
|
302
256
|
} extends Options ? Options : never;
|
|
303
|
-
export type BaseStoreOptions<S, PersistedState = Partial<S>, PersistReturn = void> = (PersistConfig<S, PersistedState, PersistReturn> & {
|
|
257
|
+
export type BaseStoreOptions<S, PersistedState extends Partial<S> = Partial<S>, PersistReturn = void> = (PersistConfig<S, PersistedState, PersistReturn> & {
|
|
304
258
|
sync?: S extends Record<string, unknown> ? SyncOption<S> : undefined;
|
|
305
259
|
}) | ({
|
|
306
260
|
sync: S extends Record<string, unknown> ? SyncWithoutStorageOption<NoInfer<S>> : undefined;
|
|
@@ -315,12 +269,11 @@ export type SyncOption<S extends Record<string, unknown>> = SyncConfig<S> | stri
|
|
|
315
269
|
type SyncWithoutStorageOption<S extends Record<string, unknown>> = string | (Omit<SyncConfig<S>, 'injectStorageMetadata' | 'key'> & {
|
|
316
270
|
injectStorageMetadata?: never;
|
|
317
271
|
key: SyncWithoutStorageKey<SyncConfig<S>>;
|
|
318
|
-
readonly __syncStateBrand?: (state: S) => S;
|
|
319
272
|
});
|
|
320
273
|
type SyncWithoutStorageKey<Config> = Config extends {
|
|
321
274
|
key?: infer Key;
|
|
322
275
|
} ? Extract<Key, string> extends never ? string : Extract<Key, string> : string;
|
|
323
|
-
type UndefinedPersistKeys<S, PersistedState
|
|
276
|
+
type UndefinedPersistKeys<S, PersistedState extends Partial<S>, PersistReturn> = {
|
|
324
277
|
[K in keyof PersistConfig<S, PersistedState, PersistReturn>]?: undefined;
|
|
325
278
|
};
|
|
326
279
|
/**
|
|
@@ -332,5 +285,4 @@ export type DebounceOptions = {
|
|
|
332
285
|
maxWait?: number;
|
|
333
286
|
trailing?: boolean;
|
|
334
287
|
};
|
|
335
|
-
export {};
|
|
336
288
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
*
|
|
11
11
|
* @template TArgs - The argument types of the function to schedule
|
|
12
12
|
* @param fn - The function to schedule for microtask execution
|
|
13
|
+
* @param enqueue - The queue that should run the scheduled task
|
|
13
14
|
* @returns A scheduler function with the same signature as the input function
|
|
14
15
|
*
|
|
15
16
|
* @example
|
|
@@ -24,5 +25,5 @@
|
|
|
24
25
|
* // Logs "Updated: 3" once in the next microtask
|
|
25
26
|
* ```
|
|
26
27
|
*/
|
|
27
|
-
export declare function createMicrotaskScheduler<Args extends unknown[]>(fn: (...args: Args) => void): (...args: Args) => void;
|
|
28
|
+
export declare function createMicrotaskScheduler<Args extends unknown[]>(fn: (...args: Args) => void, enqueue?: (task: () => void) => void): (...args: Args) => void;
|
|
28
29
|
//# sourceMappingURL=createMicrotaskScheduler.d.ts.map
|
package/dist/utils/time.d.ts
CHANGED
|
@@ -33,12 +33,5 @@ type TimeUtils = {
|
|
|
33
33
|
* time.zero // 0
|
|
34
34
|
*/
|
|
35
35
|
export declare const time: TimeUtils;
|
|
36
|
-
/**
|
|
37
|
-
* Waits for microtasks to complete.
|
|
38
|
-
* Uses setTimeout(0) to allow microtasks scheduled via queueMicrotask to execute.
|
|
39
|
-
*
|
|
40
|
-
* Useful in tests when waiting for async operations scheduled via microtasks.
|
|
41
|
-
*/
|
|
42
|
-
export declare function waitForMicrotask(): Promise<void>;
|
|
43
36
|
export {};
|
|
44
37
|
//# sourceMappingURL=time.d.ts.map
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { BaseStoreOptions, OptionallyPersistedStore, Store, StateCreator } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a base store without persistence.
|
|
4
|
+
* @param createState - The state creator function for the base store.
|
|
5
|
+
* @returns A store with the specified state.
|
|
6
|
+
*/
|
|
7
|
+
export declare function createBaseStore<S>(createState: StateCreator<S>): Store<S>;
|
|
8
|
+
/**
|
|
9
|
+
* Creates a base store with persistence.
|
|
10
|
+
* @param createState - The state creator function for the base store.
|
|
11
|
+
* @param options - The configuration options for persistence and sync.
|
|
12
|
+
* @returns A store with the specified state and persistence.
|
|
13
|
+
*/
|
|
14
|
+
export declare function createBaseStore<S, PersistedState extends Partial<S> = Partial<S>, PersistReturn extends void = void>(createState: StateCreator<S>, options: BaseStoreOptions<S, PersistedState, PersistReturn>): Store<S, PersistedState, PersistReturn>;
|
|
15
|
+
/**
|
|
16
|
+
* Creates a base store with async persistence.
|
|
17
|
+
* @param createState - The state creator function for the base store.
|
|
18
|
+
* @param options - The configuration options for persistence and sync.
|
|
19
|
+
* @returns A store with the specified state and persistence.
|
|
20
|
+
*/
|
|
21
|
+
export declare function createBaseStore<S, PersistedState extends Partial<S> = Partial<S>, PersistReturn extends Promise<void> = Promise<void>>(createState: StateCreator<S>, options: BaseStoreOptions<S, PersistedState, PersistReturn>): Store<S, PersistedState, PersistReturn>;
|
|
22
|
+
/**
|
|
23
|
+
* Creates a base store with optional persistence.
|
|
24
|
+
* @param createState - The state creator function for the base store.
|
|
25
|
+
* @param options - The configuration options for persistence and sync.
|
|
26
|
+
* @returns A store with the specified state and optional persistence.
|
|
27
|
+
*/
|
|
28
|
+
export declare function createBaseStore<S, PersistedState extends Partial<S> = Partial<S>, PersistReturn extends void = void>(createState: StateCreator<S>, options?: BaseStoreOptions<S, PersistedState, PersistReturn>): OptionallyPersistedStore<S, PersistedState, PersistReturn>;
|
|
29
|
+
/**
|
|
30
|
+
* Creates a base store with optional async persistence.
|
|
31
|
+
* @param createState - The state creator function for the base store.
|
|
32
|
+
* @param options - The configuration options for persistence and sync.
|
|
33
|
+
* @returns A store with the specified state and optional persistence.
|
|
34
|
+
*/
|
|
35
|
+
export declare function createBaseStore<S, PersistedState extends Partial<S> = Partial<S>, PersistReturn extends Promise<void> = Promise<void>>(createState: StateCreator<S>, options?: BaseStoreOptions<S, PersistedState, PersistReturn>): OptionallyPersistedStore<S, PersistedState, PersistReturn>;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { DeriveGetter, DeriveOptions, DerivedStore } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* ### `createDerivedStore`
|
|
4
|
+
*
|
|
5
|
+
* Creates a **read-only** store derived from one or more source stores.
|
|
6
|
+
*
|
|
7
|
+
* ---
|
|
8
|
+
* The `deriveFunction` is called whenever its dependencies change, producing a new derived state.
|
|
9
|
+
* Dependencies are automatically tracked through a special `$` helper, which supports:
|
|
10
|
+
*
|
|
11
|
+
* 1) **Selector-based** usage:
|
|
12
|
+
* ```ts
|
|
13
|
+
* $ => {
|
|
14
|
+
* const user = $(useUserStore, s => s.user, shallowEqual);
|
|
15
|
+
* const theme = $(useSettingsStore, s => s.appearance.theme);
|
|
16
|
+
* return { user, theme, isAdmin: user?.roles.includes('admin') };
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* 2) **Proxy-based** usage (auto-built selectors for nested properties):
|
|
21
|
+
* ```ts
|
|
22
|
+
* $ => {
|
|
23
|
+
* const { user } = $(useUserStore); // Subscribe to `user`
|
|
24
|
+
* const theme = $(useSettingsStore).appearance.theme; // Subscribe to `theme`
|
|
25
|
+
* return { isAdmin: user?.roles.includes('admin'), theme, user };
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* ---
|
|
30
|
+
* Derived stores automatically unsubscribe from all dependencies when no consumers remain, and
|
|
31
|
+
* resubscribe when new consumers appear. The returned store exposes:
|
|
32
|
+
*
|
|
33
|
+
* - A **store object** with `getState()`, `subscribe()`, and `destroy()`
|
|
34
|
+
* - In the React build, a **React hook** (`const state = derivedStore(selector, equalityFn?)`)
|
|
35
|
+
*
|
|
36
|
+
* ---
|
|
37
|
+
* You can optionally pass a second parameter (either an equality function or a config object)
|
|
38
|
+
* to enable debouncing, customize the equality function, or set `lockDependencies: true`.
|
|
39
|
+
*
|
|
40
|
+
* (When dependencies are locked, subscriptions created via `$` are established once and are
|
|
41
|
+
* not rebuilt on subsequent re-derives, which can be a performance win for certain workloads.)
|
|
42
|
+
*
|
|
43
|
+
* ---
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* // Create a derived store
|
|
47
|
+
* const searchResultsStore = createDerivedStore($ => {
|
|
48
|
+
* const query = $(searchStore).query.trim().toLowerCase();
|
|
49
|
+
* const items = $(itemsStore).items;
|
|
50
|
+
* return findResults(query, items);
|
|
51
|
+
* }, shallowEqual);
|
|
52
|
+
*
|
|
53
|
+
* const results = searchResultsStore.getState();
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* ---
|
|
57
|
+
* @param deriveFunction - Function that reads from other stores via `$` to produce derived state.
|
|
58
|
+
* @param optionsOrEqualityFn - Either an equality function or a config object (see `DeriveOptions`).
|
|
59
|
+
*
|
|
60
|
+
* @returns A read-only derived store. In the React build, it is also callable as a hook.
|
|
61
|
+
*/
|
|
62
|
+
export declare function createDerivedStore<Derived>(deriveFunction: ($: DeriveGetter) => Derived, optionsOrEqualityFn?: DeriveOptions<Derived>): DerivedStore<Derived>;
|