@storesjs/stores 0.8.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/LICENSE +21 -0
- package/dist/config.d.ts +13 -0
- package/dist/createBaseStore.d.ts +35 -0
- package/dist/createDerivedStore.d.ts +70 -0
- package/dist/createQueryStore.d.ts +131 -0
- package/dist/createVirtualStore.d.ts +39 -0
- package/dist/derivedStore/deriveProxy.d.ts +24 -0
- package/dist/derivedStore/globalDeriveScheduler.d.ts +22 -0
- package/dist/env.d.ts +7 -0
- package/dist/env.native.d.ts +8 -0
- package/dist/env.web.d.ts +7 -0
- package/dist/hooks/compareHooks.d.ts +49 -0
- package/dist/hooks/useLazyRef.d.ts +12 -0
- package/dist/hooks/useListen.d.ts +97 -0
- package/dist/hooks/useStableValue.d.ts +15 -0
- package/dist/index.d.ts +16 -0
- package/dist/logger.d.ts +16 -0
- package/dist/middleware/createHydrationGate.d.ts +19 -0
- package/dist/middleware/createSubscriptionManager.d.ts +10 -0
- package/dist/native/index.js +1 -0
- package/dist/native/index.mjs +1 -0
- package/dist/plugins/chrome/chromeExtensionSyncEngine.d.ts +31 -0
- package/dist/plugins/chrome/chromeStorageAdapter.d.ts +32 -0
- package/dist/plugins/chrome/createSyncedChromeStorage.d.ts +16 -0
- package/dist/plugins/chrome/index.d.ts +3 -0
- package/dist/plugins/chrome/utils.d.ts +1 -0
- package/dist/plugins/delta/deltaInstrumentation.d.ts +42 -0
- package/dist/plugins/delta/index.d.ts +1 -0
- package/dist/plugins/delta/recordDelta.d.ts +14 -0
- package/dist/plugins/network/index.d.ts +2 -0
- package/dist/plugins/router/RouteErrorBoundary.d.ts +19 -0
- package/dist/plugins/router/__tests__/router-types.test.d.ts +0 -0
- package/dist/plugins/router/constants.d.ts +38 -0
- package/dist/plugins/router/createRouter-old-version.d.ts +183 -0
- package/dist/plugins/router/createRouter.d.ts +183 -0
- package/dist/plugins/router/errorBoundary.d.ts +23 -0
- package/dist/plugins/router/index.d.ts +6 -0
- package/dist/plugins/router/lazy.d.ts +14 -0
- package/dist/plugins/router/locationStore.d.ts +24 -0
- package/dist/plugins/router/pathMatching.d.ts +28 -0
- package/dist/plugins/router/scrollRestoration.d.ts +8 -0
- package/dist/plugins/router/searchParams.d.ts +53 -0
- package/dist/plugins/router/test-link-params.d.ts +1 -0
- package/dist/plugins/router/test-prefetch.d.ts +17 -0
- package/dist/plugins/router/test-types.d.ts +1 -0
- package/dist/plugins/router/types.d.ts +11 -0
- package/dist/plugins/router/utils.d.ts +14 -0
- package/dist/plugins/router/vite.d.ts +5 -0
- package/dist/presence/heartbeat.d.ts +23 -0
- package/dist/presence/presenceChannel.d.ts +19 -0
- package/dist/presence/pruning.d.ts +34 -0
- package/dist/queryStore/classes/SubscriptionManager.d.ts +66 -0
- package/dist/queryStore/createParamManager.d.ts +27 -0
- package/dist/queryStore/types.d.ts +396 -0
- package/dist/signal.d.ts +19 -0
- package/dist/storage/storageCreators.d.ts +21 -0
- package/dist/storage/storageTypes.d.ts +12 -0
- package/dist/storesStorage.d.ts +2 -0
- package/dist/storesStorage.native.d.ts +2 -0
- package/dist/storesStorage.web.d.ts +2 -0
- package/dist/sync/browserSyncEngine.d.ts +2 -0
- package/dist/sync/networkSyncEngine.d.ts +137 -0
- package/dist/sync/noopSyncEngine.d.ts +2 -0
- package/dist/sync/syncEnhancer.d.ts +21 -0
- package/dist/sync/syncUtils.d.ts +5 -0
- package/dist/sync/transport.d.ts +112 -0
- package/dist/sync/types.d.ts +189 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/types/functions.d.ts +6 -0
- package/dist/types/objects.d.ts +3 -0
- package/dist/types/utils.d.ts +8 -0
- package/dist/types.d.ts +332 -0
- package/dist/utils/core.d.ts +6 -0
- package/dist/utils/createAsyncMicrotaskScheduler.d.ts +34 -0
- package/dist/utils/createMicrotaskScheduler.d.ts +27 -0
- package/dist/utils/createStoreActions.d.ts +24 -0
- package/dist/utils/debounce.d.ts +21 -0
- package/dist/utils/equality.d.ts +37 -0
- package/dist/utils/factoryUtils.d.ts +21 -0
- package/dist/utils/hydrationCoordinator.d.ts +12 -0
- package/dist/utils/persistUtils.d.ts +8 -0
- package/dist/utils/promiseUtils.d.ts +1 -0
- package/dist/utils/serialization.d.ts +13 -0
- package/dist/utils/storeUtils.d.ts +56 -0
- package/dist/utils/stringUtils.d.ts +6 -0
- package/dist/utils/time.d.ts +46 -0
- package/dist/web/chrome.js +1 -0
- package/dist/web/chrome.mjs +1 -0
- package/dist/web/chunk-YWUZUTPH.mjs +1 -0
- package/dist/web/index.js +1 -0
- package/dist/web/index.mjs +1 -0
- package/package.json +75 -0
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import { Mutate, StateCreator as ZustandStateCreator, StoreApi } from 'zustand';
|
|
2
|
+
import { PersistOptions } from 'zustand/middleware';
|
|
3
|
+
import { UseBoundStoreWithEqualityFn } from 'zustand/traditional';
|
|
4
|
+
import { StorageValue } from './storage/storageTypes';
|
|
5
|
+
import { SyncConfig } from './sync/types';
|
|
6
|
+
type SubscribeWithSelector = ['zustand/subscribeWithSelector', never];
|
|
7
|
+
type HydrationPromise<PersistReturn> = PersistReturn extends Promise<void> ? {
|
|
8
|
+
/** Invoke to get a promise that resolves once hydration completes. */
|
|
9
|
+
hydrationPromise: () => Promise<void>;
|
|
10
|
+
} : {
|
|
11
|
+
hydrationPromise?: undefined;
|
|
12
|
+
};
|
|
13
|
+
export type StorePersist<Store, PersistedState, PersistReturn> = Store extends {
|
|
14
|
+
getState: () => infer S;
|
|
15
|
+
setState: {
|
|
16
|
+
(...args: infer SetPartialArgs): infer _;
|
|
17
|
+
(...args: infer SetFullArgs): infer _;
|
|
18
|
+
};
|
|
19
|
+
} ? {
|
|
20
|
+
setState(...args: SetPartialArgs): PersistReturn;
|
|
21
|
+
setState(...args: SetFullArgs): PersistReturn;
|
|
22
|
+
persist: {
|
|
23
|
+
clearStorage: () => void;
|
|
24
|
+
getOptions: () => Partial<PersistOptions<S, PersistedState>>;
|
|
25
|
+
hasHydrated: () => boolean;
|
|
26
|
+
onHydrate: (listener: (state: S) => void) => () => void;
|
|
27
|
+
onFinishHydration: (listener: (state: S) => void) => () => void;
|
|
28
|
+
rehydrate: () => Promise<void> | void;
|
|
29
|
+
setOptions: (options: Partial<PersistOptions<S, PersistedState, PersistReturn>>) => void;
|
|
30
|
+
} & HydrationPromise<PersistReturn>;
|
|
31
|
+
} : never;
|
|
32
|
+
export type StateCreator<S, U = S> = ZustandStateCreator<S, [SubscribeWithSelector], [SubscribeWithSelector], U>;
|
|
33
|
+
export type BaseStore<S, ExtraSubscribeOptions extends boolean = false> = UseBoundStoreWithEqualityFn<Mutate<StoreApi<S>, [SubscribeWithSelector]>> & {
|
|
34
|
+
subscribe: SubscribeOverloads<S, ExtraSubscribeOptions>;
|
|
35
|
+
};
|
|
36
|
+
export type PersistedStore<S, PersistedState = Partial<S>, ExtraSubscribeOptions extends boolean = false, PersistReturn = unknown> = {
|
|
37
|
+
(): S;
|
|
38
|
+
<U>(selector: (state: S) => U, equalityFn?: (a: U, b: U) => boolean): U;
|
|
39
|
+
} & Omit<BaseStore<S, ExtraSubscribeOptions>, 'setState' | 'persist'> & StorePersist<BaseStore<S, ExtraSubscribeOptions>, PersistedState, PersistReturn>;
|
|
40
|
+
export type Store<S, PersistedState extends Partial<S> = never, ExtraSubscribeOptions extends boolean = false, PersistReturn = unknown> = [
|
|
41
|
+
PersistedState
|
|
42
|
+
] extends [never] ? BaseStore<S, ExtraSubscribeOptions> : PersistedStore<S, PersistedState, ExtraSubscribeOptions, PersistReturn>;
|
|
43
|
+
export type OptionallyPersistedStore<S, PersistedState, PersistReturn = void> = WithAsyncSet<Store<S>, S, PersistedState, PersistReturn> & UseStoreCallSignatures<S> & {
|
|
44
|
+
persist?: PersistedStore<S, PersistedState, false, PersistReturn>['persist'];
|
|
45
|
+
};
|
|
46
|
+
export type NoInfer<T> = [T][T extends unknown ? 0 : never];
|
|
47
|
+
export type Timeout = ReturnType<typeof setTimeout>;
|
|
48
|
+
export type Listener<S> = (state: S, prevState: S) => void;
|
|
49
|
+
export type Selector<S, Selected> = (state: S) => Selected;
|
|
50
|
+
export type EqualityFn<T = unknown> = (a: T, b: T) => boolean;
|
|
51
|
+
export type UseStoreCallSignatures<S> = {
|
|
52
|
+
(): S;
|
|
53
|
+
<Selected>(selector: Selector<S, Selected>, equalityFn?: EqualityFn<Selected>): Selected;
|
|
54
|
+
};
|
|
55
|
+
export type InferStoreState<Store extends StoreApi<unknown>> = Store extends {
|
|
56
|
+
getState: () => infer T;
|
|
57
|
+
} ? T : never;
|
|
58
|
+
/**
|
|
59
|
+
* Extracts the return type of setState from a store.
|
|
60
|
+
* Returns `void` for non-persisted or sync-persisted stores.
|
|
61
|
+
* Returns `Promise<void>` for async-persisted stores.
|
|
62
|
+
*/
|
|
63
|
+
export type InferSetStateReturn<Store> = Store extends {
|
|
64
|
+
setState(...args: SetStateArgs<infer S>): infer R;
|
|
65
|
+
} ? R : void;
|
|
66
|
+
/**
|
|
67
|
+
* Extracts the PersistedState type from a store's persist property.
|
|
68
|
+
* Returns `Partial<State>` if the store doesn't have persistence.
|
|
69
|
+
*/
|
|
70
|
+
export type InferPersistedState<Store, State> = Store extends {
|
|
71
|
+
persist: {
|
|
72
|
+
getOptions: () => infer Options;
|
|
73
|
+
};
|
|
74
|
+
} ? Options extends {
|
|
75
|
+
name?: string;
|
|
76
|
+
} ? Partial<State> : Partial<State> : Partial<State>;
|
|
77
|
+
export type SetPartial<S> = Partial<S> | ((state: S) => Partial<S>);
|
|
78
|
+
export type SetFull<S> = S | ((state: S) => S);
|
|
79
|
+
export type SetStateReplaceArgs<S, ExtraArgs extends unknown[] = []> = [update: SetFull<S>, replace: true, ...extraArgs: ExtraArgs];
|
|
80
|
+
export type SetStatePartialArgs<S, ExtraArgs extends unknown[] = []> = [update: SetPartial<S>, replace?: false, ...extraArgs: ExtraArgs];
|
|
81
|
+
export type SetStateArgs<S, ExtraArgs extends unknown[] = []> = SetStatePartialArgs<S, ExtraArgs> | SetStateReplaceArgs<S, ExtraArgs>;
|
|
82
|
+
export type SetState<S, ExtraArgs extends unknown[] = [], PersistReturn extends Promise<void> | void = void> = (...args: SetStateArgs<S, ExtraArgs>) => PersistReturn;
|
|
83
|
+
export type SetStateOverloads<S, PersistReturn extends Promise<void> | void = void> = {
|
|
84
|
+
(update: SetPartial<S>, replace?: false): PersistReturn;
|
|
85
|
+
(update: SetFull<S>, replace: true): PersistReturn;
|
|
86
|
+
};
|
|
87
|
+
export type WithAsyncSet<Store extends StoreApi<unknown>, S, PersistedState, PersistReturn> = Omit<Store, 'persist' | 'setState'> & {
|
|
88
|
+
persist?: PersistedStore<S, PersistedState, false, PersistReturn>['persist'];
|
|
89
|
+
setState(update: SetPartial<InferStoreState<Store>>, replace?: false): PersistReturn;
|
|
90
|
+
setState(update: SetFull<InferStoreState<Store>>, replace: true): PersistReturn;
|
|
91
|
+
};
|
|
92
|
+
export type SubscribeOptions<Selected> = {
|
|
93
|
+
equalityFn?: EqualityFn<Selected>;
|
|
94
|
+
fireImmediately?: boolean;
|
|
95
|
+
isDerivedStore?: boolean;
|
|
96
|
+
};
|
|
97
|
+
export type ListenerArgs<S> = [listener: Listener<S>];
|
|
98
|
+
export type SelectorArgs<S, Selected> = [
|
|
99
|
+
selector: Selector<S, Selected>,
|
|
100
|
+
listener: Listener<Selected>,
|
|
101
|
+
options?: SubscribeOptions<Selected>
|
|
102
|
+
];
|
|
103
|
+
export type SubscribeOverloads<S, ExtraOptions extends boolean = false> = {
|
|
104
|
+
(listener: Listener<S>): UnsubscribeFn<ExtraOptions>;
|
|
105
|
+
<Selected>(selector: Selector<S, Selected>, listener: Listener<Selected>, options?: SubscribeOptions<Selected>): UnsubscribeFn<ExtraOptions>;
|
|
106
|
+
};
|
|
107
|
+
export type SubscribeArgs<S, Selected = unknown> = ListenerArgs<S> | SelectorArgs<S, Selected>;
|
|
108
|
+
export type UnsubscribeFn<Options extends boolean = false> = Options extends true ? (skipAbortFetch?: boolean) => void : () => void;
|
|
109
|
+
export type SubscribeFn<S, Selected = S> = (...args: SubscribeArgs<S, Selected>) => UnsubscribeFn;
|
|
110
|
+
export type DerivedStore<S> = WithFlushUpdates<ReadOnlyDerivedStore<BaseStore<S>>>;
|
|
111
|
+
export type WithFlushUpdates<Store extends StoreApi<unknown>> = Store & {
|
|
112
|
+
/**
|
|
113
|
+
* Destroy the derived store and its subscriptions.
|
|
114
|
+
*/
|
|
115
|
+
destroy: () => void;
|
|
116
|
+
/**
|
|
117
|
+
* Flush all pending updates — only applicable to **debounced** derived stores.
|
|
118
|
+
*/
|
|
119
|
+
flushUpdates: () => void;
|
|
120
|
+
};
|
|
121
|
+
export type WithGetSnapshot<Store extends StoreApi<unknown>> = Store & {
|
|
122
|
+
/**
|
|
123
|
+
* Provided to `useSyncExternalStoreWithSelector` to ensure it activates the derived
|
|
124
|
+
* store when it gets the initial state before subscribing to the store.
|
|
125
|
+
*/
|
|
126
|
+
getSnapshot: () => InferStoreState<Store>;
|
|
127
|
+
};
|
|
128
|
+
type ReadOnlyDerivedStore<Store extends BaseStore<unknown>> = Omit<Store, 'getInitialState' | 'setState'> & UseStoreCallSignatures<InferStoreState<Store>> & {
|
|
129
|
+
/**
|
|
130
|
+
* @deprecated **Not applicable to derived stores.** Will throw an error.
|
|
131
|
+
*/
|
|
132
|
+
getInitialState: Store['getInitialState'];
|
|
133
|
+
/**
|
|
134
|
+
* @deprecated **Not applicable to derived stores.** Will throw an error.
|
|
135
|
+
*/
|
|
136
|
+
setState: Store['setState'];
|
|
137
|
+
};
|
|
138
|
+
/**
|
|
139
|
+
* Configuration for creating derived stores. You can pass either:
|
|
140
|
+
* - A **function** (used as `equalityFn`), or
|
|
141
|
+
* - An **object** with the fields below
|
|
142
|
+
*/
|
|
143
|
+
export type DeriveOptions<DerivedState = unknown> = EqualityFn<DerivedState> | {
|
|
144
|
+
/**
|
|
145
|
+
* Delay before triggering a re-derive when dependencies change.
|
|
146
|
+
* Accepts a number (ms) or debounce options:
|
|
147
|
+
*
|
|
148
|
+
* `{ delay: number, leading?: boolean, trailing?: boolean, maxWait?: number }`
|
|
149
|
+
* @default 0
|
|
150
|
+
*/
|
|
151
|
+
debounce?: number | DebounceOptions;
|
|
152
|
+
/**
|
|
153
|
+
* If `true`, the store will log debug messages to the console.
|
|
154
|
+
*
|
|
155
|
+
* If `'verbose'`, the store will log the subscriptions it creates each time the derive
|
|
156
|
+
* function is run, rather than only the first time.
|
|
157
|
+
* @default false
|
|
158
|
+
*/
|
|
159
|
+
debugMode?: boolean | 'verbose';
|
|
160
|
+
/**
|
|
161
|
+
* A custom comparison function for detecting state changes.
|
|
162
|
+
* @default `Object.is`
|
|
163
|
+
*/
|
|
164
|
+
equalityFn?: EqualityFn<DerivedState>;
|
|
165
|
+
/**
|
|
166
|
+
* If `true`, the derived store will never destroy itself. Useful in cases where your
|
|
167
|
+
* derived store serves as a permanent cache subscribed to intermittently or not at all.
|
|
168
|
+
* Manual calls to `destroy` *will* destroy the store even when `keepAlive` is `true`.
|
|
169
|
+
*
|
|
170
|
+
* *Use this option carefully.*
|
|
171
|
+
* @default false
|
|
172
|
+
*/
|
|
173
|
+
keepAlive?: boolean;
|
|
174
|
+
/**
|
|
175
|
+
* Locks the dependency graph after the first derivation. Subscriptions to
|
|
176
|
+
* underlying stores are established once and reused on subsequent runs, rather
|
|
177
|
+
* than being torn down and rebuilt (the default behavior).
|
|
178
|
+
*
|
|
179
|
+
* This avoids the overhead of regenerating selectors and prevents unnecessary
|
|
180
|
+
* subscription churn. However, it means only the *initially tracked* dependencies
|
|
181
|
+
* will trigger updates — even if your derive function conditionally reads different
|
|
182
|
+
* stores in later runs.
|
|
183
|
+
*
|
|
184
|
+
* **Requirement**: All `$` calls in your derive function must be consistent and
|
|
185
|
+
* top level. Conditional dependency tracking will not work correctly.
|
|
186
|
+
*
|
|
187
|
+
* Safe to enable for most derived stores where dependencies are static.
|
|
188
|
+
*
|
|
189
|
+
* @default false
|
|
190
|
+
*/
|
|
191
|
+
lockDependencies?: boolean;
|
|
192
|
+
};
|
|
193
|
+
export type Deserializer<SerializedState, PersistedState> = (serializedState: SerializedState) => StorageValue<PersistedState>;
|
|
194
|
+
export type Serializer<SerializedState> = <PersistedState>(storageValue: StorageValue<PersistedState>) => SerializedState;
|
|
195
|
+
/**
|
|
196
|
+
* Synchronous storage interface.
|
|
197
|
+
* Used for localStorage and MMKV implementations.
|
|
198
|
+
*/
|
|
199
|
+
export interface SyncStorageInterface<SerializedState = unknown> {
|
|
200
|
+
readonly async?: false;
|
|
201
|
+
/**
|
|
202
|
+
* Adapter-level deserializer used when a store does not supply its own.
|
|
203
|
+
* Acts as the default before falling back to the framework implementation.
|
|
204
|
+
*/
|
|
205
|
+
readonly deserializer?: <PersistedState>(serializedState: SerializedState) => StorageValue<PersistedState>;
|
|
206
|
+
/**
|
|
207
|
+
* Adapter-level serializer used when a store does not supply its own.
|
|
208
|
+
* Acts as the default before falling back to the framework implementation.
|
|
209
|
+
*/
|
|
210
|
+
readonly serializer?: <PersistedState>(storageValue: StorageValue<PersistedState>) => SerializedState;
|
|
211
|
+
clearAll(): void;
|
|
212
|
+
contains(key: string): boolean;
|
|
213
|
+
delete(key: string): void;
|
|
214
|
+
get(key: string): SerializedState | undefined;
|
|
215
|
+
getAllKeys(): string[];
|
|
216
|
+
set(key: string, value: SerializedState): void;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Asynchronous storage interface.
|
|
220
|
+
* Used for Chrome storage and other async storage implementations.
|
|
221
|
+
*/
|
|
222
|
+
export type AsyncStorageInterface<SerializedState = unknown> = {
|
|
223
|
+
readonly async: true;
|
|
224
|
+
/**
|
|
225
|
+
* Adapter-level deserializer used when a store does not supply its own.
|
|
226
|
+
* Acts as the default before falling back to the framework implementation.
|
|
227
|
+
*/
|
|
228
|
+
readonly deserializer?: <PersistedState>(serializedState: SerializedState) => StorageValue<PersistedState>;
|
|
229
|
+
/**
|
|
230
|
+
* Adapter-level serializer used when a store does not supply its own.
|
|
231
|
+
* Acts as the default before falling back to the framework implementation.
|
|
232
|
+
*/
|
|
233
|
+
readonly serializer?: <PersistedState>(storageValue: StorageValue<PersistedState>) => SerializedState;
|
|
234
|
+
clearAll(): Promise<void>;
|
|
235
|
+
contains(key: string): Promise<boolean>;
|
|
236
|
+
delete(key: string): Promise<void>;
|
|
237
|
+
get(key: string): Promise<SerializedState | undefined>;
|
|
238
|
+
getAllKeys(): Promise<string[]>;
|
|
239
|
+
set(key: string, value: SerializedState): Promise<void>;
|
|
240
|
+
};
|
|
241
|
+
/**
|
|
242
|
+
* Configuration options for creating a persistable store.
|
|
243
|
+
*/
|
|
244
|
+
export type PersistConfig<S, PersistedState = Partial<S>, PersistReturn = void> = {
|
|
245
|
+
/**
|
|
246
|
+
* A function to convert the serialized value back into the state object.
|
|
247
|
+
* If not provided, the storage adapter's `deserializer` is used before falling back to the default implementation.
|
|
248
|
+
*/
|
|
249
|
+
deserializer?: <SerializedState>(serializedState: SerializedState) => StorageValue<PersistedState>;
|
|
250
|
+
/**
|
|
251
|
+
* A function to merge persisted state with current state during hydration.
|
|
252
|
+
* By default, zustand does a shallow merge, but this allows custom logic for deep merging nested objects.
|
|
253
|
+
*/
|
|
254
|
+
merge?: PersistOptions<S, PersistedState>['merge'];
|
|
255
|
+
/**
|
|
256
|
+
* A function to perform persisted state migration.
|
|
257
|
+
* This function will be called when persisted state versions mismatch with the one specified here.
|
|
258
|
+
*/
|
|
259
|
+
migrate?: PersistOptions<S, PersistedState>['migrate'];
|
|
260
|
+
/**
|
|
261
|
+
* A function returning another (optional) function.
|
|
262
|
+
* The main function will be called before the state rehydration.
|
|
263
|
+
* The returned function will be called after the state rehydration or when an error occurred.
|
|
264
|
+
*/
|
|
265
|
+
onRehydrateStorage?: PersistOptions<S, PersistedState>['onRehydrateStorage'];
|
|
266
|
+
/**
|
|
267
|
+
* A function that determines which parts of the state should be persisted.
|
|
268
|
+
* By default, the entire state is persisted.
|
|
269
|
+
*/
|
|
270
|
+
partialize?: (state: S) => PersistedState;
|
|
271
|
+
/**
|
|
272
|
+
* The throttle rate for the persist operation in milliseconds.
|
|
273
|
+
* @default iOS: time.seconds(3) | Android: time.seconds(5)
|
|
274
|
+
*/
|
|
275
|
+
persistThrottleMs?: PersistReturn extends Promise<unknown> ? undefined : number;
|
|
276
|
+
/**
|
|
277
|
+
* A function to serialize the state and version for storage.
|
|
278
|
+
* If not provided, the storage adapter's `serializer` is used before falling back to the default implementation.
|
|
279
|
+
*/
|
|
280
|
+
serializer?: <SerializedState>(storageValue: StorageValue<PersistedState>) => SerializedState;
|
|
281
|
+
/**
|
|
282
|
+
* Custom storage implementation. If async, `setState` will return a Promise that resolves
|
|
283
|
+
* when the state is persisted. If sync, `setState` will return void.
|
|
284
|
+
*/
|
|
285
|
+
storage?: PersistReturn extends Promise<unknown> ? AsyncStorageInterface : SyncStorageInterface;
|
|
286
|
+
/**
|
|
287
|
+
* The unique key for the persisted store.
|
|
288
|
+
*/
|
|
289
|
+
storageKey: string;
|
|
290
|
+
/**
|
|
291
|
+
* The version of the store's schema.
|
|
292
|
+
* Useful for handling schema changes across app versions.
|
|
293
|
+
* @default 0
|
|
294
|
+
*/
|
|
295
|
+
version?: number;
|
|
296
|
+
};
|
|
297
|
+
export type EnforceStorageKey<Options> = {
|
|
298
|
+
storageKey: string;
|
|
299
|
+
} extends Options ? Options : never;
|
|
300
|
+
export type BaseStoreOptions<S, PersistedState = Partial<S>, PersistReturn = void> = (PersistConfig<S, PersistedState, PersistReturn> & {
|
|
301
|
+
sync?: S extends Record<string, unknown> ? SyncOption<S> : undefined;
|
|
302
|
+
}) | ({
|
|
303
|
+
sync: S extends Record<string, unknown> ? SyncWithoutStorageOption<NoInfer<S>> : undefined;
|
|
304
|
+
} & UndefinedPersistKeys<S, PersistedState, PersistReturn>);
|
|
305
|
+
/**
|
|
306
|
+
* The `sync` option can be:
|
|
307
|
+
* - `SyncConfig` object
|
|
308
|
+
* - `string` (shorthand for `{ key: string }`)
|
|
309
|
+
* - `true` (inherits key from `storageKey` — only valid with persistence)
|
|
310
|
+
*/
|
|
311
|
+
export type SyncOption<S extends Record<string, unknown>> = SyncConfig<S> | string | true;
|
|
312
|
+
type SyncWithoutStorageOption<S extends Record<string, unknown>> = string | (Omit<SyncConfig<S>, 'injectStorageMetadata' | 'key'> & {
|
|
313
|
+
injectStorageMetadata?: never;
|
|
314
|
+
key: SyncWithoutStorageKey<SyncConfig<S>>;
|
|
315
|
+
readonly __syncStateBrand?: (state: S) => S;
|
|
316
|
+
});
|
|
317
|
+
type SyncWithoutStorageKey<Config> = Config extends {
|
|
318
|
+
key?: infer Key;
|
|
319
|
+
} ? Extract<Key, string> extends never ? string : Extract<Key, string> : string;
|
|
320
|
+
type UndefinedPersistKeys<S, PersistedState, PersistReturn> = {
|
|
321
|
+
[K in keyof PersistConfig<S, PersistedState, PersistReturn>]?: undefined;
|
|
322
|
+
};
|
|
323
|
+
/**
|
|
324
|
+
* Expanded options for custom debounce behavior.
|
|
325
|
+
*/
|
|
326
|
+
export type DebounceOptions = {
|
|
327
|
+
delay: number;
|
|
328
|
+
leading?: boolean;
|
|
329
|
+
maxWait?: number;
|
|
330
|
+
trailing?: boolean;
|
|
331
|
+
};
|
|
332
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates an async microtask-batched scheduler for a given async function.
|
|
3
|
+
*
|
|
4
|
+
* When the returned scheduler is called multiple times synchronously, only a single
|
|
5
|
+
* microtask execution occurs. All callers receive the same Promise that resolves
|
|
6
|
+
* after the batched execution completes. The function will be invoked with the
|
|
7
|
+
* arguments from the most recent call.
|
|
8
|
+
*
|
|
9
|
+
* This is useful for batching rapid successive async operations (e.g., async storage
|
|
10
|
+
* writes, network requests) to reduce unnecessary work while ensuring:
|
|
11
|
+
* - The latest values are used
|
|
12
|
+
* - All callers can await the completion of the batched operation
|
|
13
|
+
*
|
|
14
|
+
* @template Args - The argument types of the async function to schedule
|
|
15
|
+
* @param fn - The async function to schedule for microtask execution
|
|
16
|
+
* @returns A scheduler function that returns a Promise resolving after execution
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* const batchedSave = createAsyncMicrotaskScheduler(async (data: string) => {
|
|
21
|
+
* await storage.write(data);
|
|
22
|
+
* console.log('Saved:', data);
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* await Promise.all([
|
|
26
|
+
* batchedSave('first'),
|
|
27
|
+
* batchedSave('second'),
|
|
28
|
+
* batchedSave('third'),
|
|
29
|
+
* ]);
|
|
30
|
+
* // Writes "third" to storage once, all three Promises resolve together
|
|
31
|
+
* // Logs "Saved: third"
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare function createAsyncMicrotaskScheduler<Args extends unknown[]>(fn: (...args: Args) => Promise<void>): (...args: Args) => Promise<void>;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a microtask-batched scheduler for a given function.
|
|
3
|
+
*
|
|
4
|
+
* When the returned scheduler is called multiple times synchronously, only a single
|
|
5
|
+
* microtask execution occurs. The function will be invoked with the arguments from
|
|
6
|
+
* the most recent call.
|
|
7
|
+
*
|
|
8
|
+
* This is useful for batching rapid successive calls (e.g., state updates, change
|
|
9
|
+
* handlers) to reduce unnecessary work while ensuring the latest values are used.
|
|
10
|
+
*
|
|
11
|
+
* @template TArgs - The argument types of the function to schedule
|
|
12
|
+
* @param fn - The function to schedule for microtask execution
|
|
13
|
+
* @returns A scheduler function with the same signature as the input function
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const batchedUpdate = createMicrotaskScheduler((value: number) => {
|
|
18
|
+
* console.log('Updated:', value);
|
|
19
|
+
* });
|
|
20
|
+
*
|
|
21
|
+
* batchedUpdate(1);
|
|
22
|
+
* batchedUpdate(2);
|
|
23
|
+
* batchedUpdate(3);
|
|
24
|
+
* // Logs "Updated: 3" once in the next microtask
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export declare function createMicrotaskScheduler<Args extends unknown[]>(fn: (...args: Args) => void): (...args: Args) => void;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { StoreApi } from 'zustand';
|
|
2
|
+
import { InferStoreState } from '../types';
|
|
3
|
+
import { NoOverlap, ObjectMethods } from '../types/objects';
|
|
4
|
+
import { FunctionKeys } from '../types/functions';
|
|
5
|
+
export type StoreActions<Store extends StoreApi<unknown>> = Pick<InferStoreState<Store>, FunctionKeys<InferStoreState<Store>>>;
|
|
6
|
+
/**
|
|
7
|
+
* Given a store instance, produces a new object containing only its actions.
|
|
8
|
+
*
|
|
9
|
+
* Intended for export alongside the associated store.
|
|
10
|
+
*
|
|
11
|
+
* @param store - The store to create actions for.
|
|
12
|
+
* @param bundledMethods - Optional extra methods to bundle into the actions object.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* export const useCounterStore = createRainbowStore(set => ({
|
|
16
|
+
* count: 0,
|
|
17
|
+
* increment: () => set(state => ({ count: state.count + 1 })),
|
|
18
|
+
* }));
|
|
19
|
+
*
|
|
20
|
+
* export const exampleActions = createStoreActions(useExampleStore);
|
|
21
|
+
* exampleActions.increment();
|
|
22
|
+
*/
|
|
23
|
+
export declare function createStoreActions<Store extends StoreApi<unknown>>(store: Store): StoreActions<Store>;
|
|
24
|
+
export declare function createStoreActions<Store extends StoreApi<unknown>, Bundled extends ObjectMethods>(store: Store, bundledMethods: NoOverlap<InferStoreState<Store>, Bundled>): StoreActions<Store> & Bundled;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type DebounceOptions = {
|
|
2
|
+
leading?: boolean;
|
|
3
|
+
maxWait?: number;
|
|
4
|
+
trailing?: boolean;
|
|
5
|
+
};
|
|
6
|
+
export type DebouncedFunction<F extends (...args: Parameters<F>) => ReturnType<F>> = {
|
|
7
|
+
(...args: Parameters<F>): ReturnType<F> | undefined;
|
|
8
|
+
cancel: () => void;
|
|
9
|
+
flush: () => ReturnType<F> | undefined;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Creates a debounced function that delays invoking `func` until after `wait` milliseconds
|
|
13
|
+
* have elapsed since the last time the debounced function was invoked.
|
|
14
|
+
* Copied and adapted from lodash@4.17.21.
|
|
15
|
+
*
|
|
16
|
+
* @param func The function to debounce.
|
|
17
|
+
* @param wait The number of milliseconds to delay.
|
|
18
|
+
* @param options The options object.
|
|
19
|
+
* @returns Returns the new debounced function.
|
|
20
|
+
*/
|
|
21
|
+
export declare function debounce<F extends (...args: Parameters<F>) => ReturnType<F>>(func: F, wait?: number, options?: DebounceOptions): DebouncedFunction<F>;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* **Worklet compatible**
|
|
3
|
+
*
|
|
4
|
+
* Performs a deep equality check between two values.
|
|
5
|
+
*
|
|
6
|
+
* This function recursively compares two values, checking if they are equal. For objects,
|
|
7
|
+
* it validates that both have the same keys and that each corresponding value is deeply equal.
|
|
8
|
+
* For non-objects, it uses strict equality.
|
|
9
|
+
*
|
|
10
|
+
* @param obj1 - The first value to compare.
|
|
11
|
+
* @param obj2 - The second value to compare.
|
|
12
|
+
* @returns `true` if the values are deeply equal; otherwise, `false`.
|
|
13
|
+
*/
|
|
14
|
+
export declare function deepEqual<U>(obj1: U, obj2: U): boolean;
|
|
15
|
+
/**
|
|
16
|
+
* **Worklet compatible**
|
|
17
|
+
*
|
|
18
|
+
* Performs a shallow equality check between two values.
|
|
19
|
+
*
|
|
20
|
+
* For non-objects, uses strict equality (===). For objects, checks that both have identical
|
|
21
|
+
* keys and compares their values using `Object.is` without recursing into nested objects.
|
|
22
|
+
*
|
|
23
|
+
* Ideal for preventing re-renders due to top-level object recreation.
|
|
24
|
+
*
|
|
25
|
+
* @param obj1 - The first value to compare.
|
|
26
|
+
* @param obj2 - The second value to compare.
|
|
27
|
+
* @returns `true` if the values are shallowly equal; otherwise, `false`.
|
|
28
|
+
*/
|
|
29
|
+
export declare function shallowEqual<U>(obj1: U, obj2: U): boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Performs a deep equality check between two values, supporting objects, arrays, maps, sets, dates, regexps, array buffers, and more.
|
|
32
|
+
* Copied from dequal@2.0.3 and fully typed for TypeScript.
|
|
33
|
+
* @param foo - The first value to compare.
|
|
34
|
+
* @param bar - The second value to compare.
|
|
35
|
+
* @returns `true` if the values are deeply equal; otherwise, `false`.
|
|
36
|
+
*/
|
|
37
|
+
export declare function dequal(foo: unknown, bar: unknown): boolean;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { InferStoreState, OptionallyPersistedStore, SubscribeArgs, UnsubscribeFn } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* #### `⚙️ createStoreFactoryUtils ⚙️`
|
|
4
|
+
*
|
|
5
|
+
* Produces strongly-typed helpers for store factory setups. Assumes:
|
|
6
|
+
* - A factory-capable "router" store exists whose properties map to a single underlying active store (via `getStore`).
|
|
7
|
+
* - The factory setup enforces a single active store instance.
|
|
8
|
+
* - `getStore` is a function that returns the concrete store instance.
|
|
9
|
+
*
|
|
10
|
+
* @param getStore – A function that returns the concrete store instance.
|
|
11
|
+
*
|
|
12
|
+
* @returns
|
|
13
|
+
* - `persist`: A persist object that attaches to the active store. Attach this to the factory-capable store.
|
|
14
|
+
* - `portableSubscribe`: A function for subscribing to store changes. Use as the factory-capable store's `subscribe` method.
|
|
15
|
+
* - `rebindSubscriptions`: Rebinds subscriptions on store change. Call this when the factory-capable store's underlying store is updated.
|
|
16
|
+
*/
|
|
17
|
+
export declare function createStoreFactoryUtils<Store extends OptionallyPersistedStore<InferStoreState<Store>, PersistedState>, PersistedState>(getStore: () => Store): {
|
|
18
|
+
persist: Store['persist'];
|
|
19
|
+
portableSubscribe: (...args: SubscribeArgs<InferStoreState<Store>>) => UnsubscribeFn;
|
|
20
|
+
rebindSubscriptions: (oldStore: Store, newStore: Store) => void;
|
|
21
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type HydrationCoordinator = {
|
|
2
|
+
readonly complete: () => void;
|
|
3
|
+
readonly fail: (error: Error) => void;
|
|
4
|
+
readonly isHydrated: () => boolean;
|
|
5
|
+
readonly promise: Promise<void>;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Creates a coordinator for tracking async storage hydration lifecycle.
|
|
9
|
+
* Provides a promise that resolves once hydration completes and methods
|
|
10
|
+
* to signal completion or failure.
|
|
11
|
+
*/
|
|
12
|
+
export declare function createHydrationCoordinator(): HydrationCoordinator;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { StorageValue } from '../storage/storageTypes';
|
|
2
|
+
/**
|
|
3
|
+
* Default partialize function if none is provided. It omits top-level store
|
|
4
|
+
* methods and keeps all other state.
|
|
5
|
+
*/
|
|
6
|
+
export declare function omitStoreMethods<S, PersistedState extends Partial<S>>(state: S): PersistedState;
|
|
7
|
+
export declare function defaultSerializeState<PersistedState>(storageValue: StorageValue<PersistedState>, shouldUseReplacer: boolean): string;
|
|
8
|
+
export declare function defaultDeserializeState<PersistedState>(serializedState: string, shouldUseReviver: boolean): StorageValue<PersistedState>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isPromiseLike<T>(value: T | Promise<T> | void): value is Promise<T>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
type SerializedMap = {
|
|
2
|
+
__type: 'Map';
|
|
3
|
+
entries: [unknown, unknown][];
|
|
4
|
+
};
|
|
5
|
+
export declare function isSerializedMap(value: unknown): value is SerializedMap;
|
|
6
|
+
type SerializedSet = {
|
|
7
|
+
__type: 'Set';
|
|
8
|
+
values: unknown[];
|
|
9
|
+
};
|
|
10
|
+
export declare function isSerializedSet(value: unknown): value is SerializedSet;
|
|
11
|
+
export declare function replacer(_: string, value: unknown): unknown;
|
|
12
|
+
export declare function reviver(_: string, value: unknown): unknown;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { StoreApi } from 'zustand';
|
|
2
|
+
import { BaseStore, DerivedStore, PersistedStore, SetStateArgs, WithGetSnapshot } from '../types';
|
|
3
|
+
import { QueryStore, QueryStoreState } from 'src/queryStore/types';
|
|
4
|
+
export declare enum StoreTags {
|
|
5
|
+
QueryStore = "_queryStore",
|
|
6
|
+
VirtualStore = "_virtualStore"
|
|
7
|
+
}
|
|
8
|
+
export declare function assignStoreTag<S>(store: BaseStore<S>, tag: StoreTags): BaseStore<S>;
|
|
9
|
+
/**
|
|
10
|
+
* Helper that applies a `setState` update to the provided state.
|
|
11
|
+
* Handles the `setState` discriminated union types internally.
|
|
12
|
+
*/
|
|
13
|
+
export declare function applyStateUpdate<S>(state: S, ...setArgs: SetStateArgs<S>): S;
|
|
14
|
+
/**
|
|
15
|
+
* Calls the appropriate reset or destroy method on the store, if available.
|
|
16
|
+
* Handles both `DerivedStore` and `QueryStore`.
|
|
17
|
+
*/
|
|
18
|
+
export declare function destroyStore(store: BaseStore<unknown> | StoreApi<unknown> | QueryStore<unknown, Record<string, unknown>, unknown>, options?: {
|
|
19
|
+
clearQueryCache?: boolean;
|
|
20
|
+
}): void;
|
|
21
|
+
export declare function destroyStores(stores: Partial<Record<string, BaseStore<unknown> | StoreApi<unknown>>> | (BaseStore<unknown> | StoreApi<unknown> | undefined)[], options?: {
|
|
22
|
+
clearQueryCache?: boolean;
|
|
23
|
+
skipDestroy?: (store: BaseStore<unknown> | StoreApi<unknown>) => boolean;
|
|
24
|
+
}): void;
|
|
25
|
+
/**
|
|
26
|
+
* Returns the name of the store, either from the `persist` options or the store itself.
|
|
27
|
+
*/
|
|
28
|
+
export declare function getStoreName(store: BaseStore<unknown>): string;
|
|
29
|
+
/**
|
|
30
|
+
* Checks if a store has a `destroy` method.
|
|
31
|
+
*/
|
|
32
|
+
export declare function hasDestroy<S>(store: StoreApi<S>): store is StoreApi<S> & {
|
|
33
|
+
destroy: () => void;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Checks if a store is a `DerivedStore` and reveals its internal `getSnapshot` method.
|
|
37
|
+
*/
|
|
38
|
+
export declare function hasGetSnapshot<S>(store: BaseStore<S> | StoreApi<S>): store is WithGetSnapshot<DerivedStore<S>>;
|
|
39
|
+
/**
|
|
40
|
+
* Checks if a store is a `QueryStore`.
|
|
41
|
+
*/
|
|
42
|
+
export declare function isQueryStore<S>(store: StoreApi<S> | BaseStore<S> | QueryStore<S, Record<string, unknown>, S>): store is BaseStore<QueryStoreState<S, Record<string, unknown>, S>>;
|
|
43
|
+
/**
|
|
44
|
+
* Checks if a store is a `DerivedStore`.
|
|
45
|
+
*/
|
|
46
|
+
export declare function isDerivedStore<S>(store: BaseStore<S> | StoreApi<S>): store is DerivedStore<S>;
|
|
47
|
+
/**
|
|
48
|
+
* Checks if a store is persisted.
|
|
49
|
+
*/
|
|
50
|
+
export declare function isPersistedStore<S>(store: BaseStore<S>): store is PersistedStore<S>;
|
|
51
|
+
/**
|
|
52
|
+
* Checks if a store is a virtual store.
|
|
53
|
+
*/
|
|
54
|
+
export declare function isVirtualStore<S>(store: BaseStore<S> | StoreApi<S>): store is BaseStore<S> & {
|
|
55
|
+
[StoreTags.VirtualStore]: true;
|
|
56
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
type TimeInMs = number;
|
|
2
|
+
type TimeUtils = {
|
|
3
|
+
/** Returns the input value unchanged, in milliseconds */
|
|
4
|
+
ms: (ms: number) => TimeInMs;
|
|
5
|
+
/** Converts seconds to milliseconds */
|
|
6
|
+
seconds: (seconds: number) => TimeInMs;
|
|
7
|
+
/** Converts minutes to milliseconds */
|
|
8
|
+
minutes: (minutes: number) => TimeInMs;
|
|
9
|
+
/** Converts hours to milliseconds */
|
|
10
|
+
hours: (hours: number) => TimeInMs;
|
|
11
|
+
/** Converts days to milliseconds */
|
|
12
|
+
days: (days: number) => TimeInMs;
|
|
13
|
+
/** Converts weeks to milliseconds */
|
|
14
|
+
weeks: (weeks: number) => TimeInMs;
|
|
15
|
+
/** Represents infinite time */
|
|
16
|
+
infinity: typeof Infinity;
|
|
17
|
+
/** Represents zero time */
|
|
18
|
+
zero: 0;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Worklet-compatible utility object for defining time values.
|
|
22
|
+
*
|
|
23
|
+
* All methods convert the input unit to milliseconds.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* time.seconds(5) // 5 seconds
|
|
27
|
+
* time.minutes(2) // 2 minutes
|
|
28
|
+
* time.hours(1) // 1 hour
|
|
29
|
+
* time.days(5) // 5 days
|
|
30
|
+
* time.weeks(2) // 2 weeks
|
|
31
|
+
* ––
|
|
32
|
+
* time.infinity // Infinity
|
|
33
|
+
* time.zero // 0
|
|
34
|
+
*/
|
|
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
|
+
* @example
|
|
43
|
+
* await waitForMicrotask();
|
|
44
|
+
*/
|
|
45
|
+
export declare function waitForMicrotask(): Promise<void>;
|
|
46
|
+
export {};
|