floppy-disk 3.0.0-beta.1 → 3.0.0-beta.2
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/esm/react/create-mutation.d.mts +13 -13
- package/esm/react/create-query.d.mts +58 -57
- package/esm/react/create-store.d.mts +7 -4
- package/esm/react/create-stores.d.mts +11 -4
- package/esm/react/use-store.d.mts +18 -8
- package/esm/react.d.mts +1 -1
- package/esm/react.mjs +108 -44
- package/esm/vanilla/basic.d.mts +0 -8
- package/esm/vanilla/store.d.mts +17 -8
- package/esm/vanilla.d.mts +0 -1
- package/esm/vanilla.mjs +6 -40
- package/package.json +2 -2
- package/react/create-mutation.d.ts +13 -13
- package/react/create-query.d.ts +58 -57
- package/react/create-store.d.ts +7 -4
- package/react/create-stores.d.ts +11 -4
- package/react/use-store.d.ts +18 -8
- package/react.d.ts +1 -1
- package/react.js +105 -42
- package/vanilla/basic.d.ts +0 -8
- package/vanilla/store.d.ts +17 -8
- package/vanilla.d.ts +0 -1
- package/vanilla.js +5 -41
- package/esm/vanilla/shallow.d.mts +0 -6
- package/vanilla/shallow.d.ts +0 -6
package/esm/vanilla/store.d.mts
CHANGED
|
@@ -11,18 +11,21 @@ export type SetState<TState> = Partial<TState> | ((state: TState) => Partial<TSt
|
|
|
11
11
|
*
|
|
12
12
|
* @param state - The latest state
|
|
13
13
|
* @param prevState - The previous state before the update
|
|
14
|
+
* @param changedKeys - The top-level keys that changed (shallow diff)
|
|
14
15
|
*
|
|
15
16
|
* @remarks
|
|
16
|
-
* - Subscribers are only called when
|
|
17
|
+
* - Subscribers are only called when at least one field changes.
|
|
17
18
|
* - Change detection is performed per key using `Object.is`.
|
|
19
|
+
* - `changedKeys` only includes top-level keys; nested changes must be inferred by the consumer.
|
|
18
20
|
*/
|
|
19
|
-
export type Subscriber<TState> = (state: TState, prevState: TState) => void;
|
|
21
|
+
export type Subscriber<TState> = (state: TState, prevState: TState, changedKeys: Array<keyof TState>) => void;
|
|
20
22
|
/**
|
|
21
23
|
* Core store API for managing state.
|
|
22
24
|
*
|
|
23
25
|
* @remarks
|
|
24
26
|
* - The store performs **shallow change detection per key** before notifying subscribers.
|
|
25
27
|
* - Subscribers are only notified when at least one field changes.
|
|
28
|
+
* - State is treated as **immutable**. Mutating nested values directly will not trigger updates.
|
|
26
29
|
* - Designed to be framework-agnostic (React bindings are built separately).
|
|
27
30
|
* - By default, `setState` is **disabled on the server** to prevent accidental shared state between requests.
|
|
28
31
|
*/
|
|
@@ -48,13 +51,17 @@ export type InitStoreOptions<TState extends Record<string, any>> = {
|
|
|
48
51
|
onSubscribe?: (state: TState, store: StoreApi<TState>) => void;
|
|
49
52
|
onUnsubscribe?: (state: TState, store: StoreApi<TState>) => void;
|
|
50
53
|
onLastUnsubscribe?: (state: TState, store: StoreApi<TState>) => void;
|
|
54
|
+
/**
|
|
55
|
+
* By default, calling `setState` on the server is disallowed to prevent shared state across requests.
|
|
56
|
+
* Set this to `true` only if you explicitly intend to mutate state during server execution.
|
|
57
|
+
*/
|
|
51
58
|
allowSetStateServerSide?: boolean;
|
|
52
59
|
};
|
|
53
60
|
/**
|
|
54
61
|
* Creates a vanilla store with pub-sub capabilities.
|
|
55
62
|
*
|
|
56
|
-
* The store state
|
|
57
|
-
* Updates are applied as
|
|
63
|
+
* The store state must be an **object**.\
|
|
64
|
+
* Updates are applied as shallow merges, so non-object states are not supported.
|
|
58
65
|
*
|
|
59
66
|
* @param initialState - The initial state of the store
|
|
60
67
|
* @param options - Optional lifecycle hooks
|
|
@@ -64,11 +71,13 @@ export type InitStoreOptions<TState extends Record<string, any>> = {
|
|
|
64
71
|
* @remarks
|
|
65
72
|
* - State updates are **shallowly compared per key** before notifying subscribers.
|
|
66
73
|
* - Subscribers are only notified when at least one updated field changes (using `Object.is` comparison).
|
|
67
|
-
* - Subscribers receive
|
|
74
|
+
* - Subscribers receive the new state, previous state, and changed top-level keys.
|
|
75
|
+
* - State is expected to be treated as **immutable**.
|
|
76
|
+
* - Mutating nested values directly will not trigger updates.
|
|
68
77
|
* - Lifecycle hooks allow side-effect management tied to subscription count.
|
|
69
|
-
* - By default, `setState` is **
|
|
70
|
-
* - This
|
|
71
|
-
* -
|
|
78
|
+
* - By default, `setState` is **not allowed on the server** to prevent accidental shared state between requests.
|
|
79
|
+
* - This helps avoid leaking data between users in server environments.
|
|
80
|
+
* - If you intentionally want to allow this behavior, set `allowSetStateServerSide: true`.
|
|
72
81
|
*
|
|
73
82
|
* @example
|
|
74
83
|
* const store = initStore({ count: 0 });
|
package/esm/vanilla.d.mts
CHANGED
package/esm/vanilla.mjs
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
const isClient = typeof window !== "undefined" && !("Deno" in window);
|
|
2
2
|
const noop = () => {
|
|
3
3
|
};
|
|
4
|
-
const identity = (value) => value;
|
|
5
4
|
const getValue = (valueOrComputeValueFn, ...params) => {
|
|
6
5
|
if (typeof valueOrComputeValueFn === "function") {
|
|
7
6
|
return valueOrComputeValueFn(...params);
|
|
@@ -9,41 +8,6 @@ const getValue = (valueOrComputeValueFn, ...params) => {
|
|
|
9
8
|
return valueOrComputeValueFn;
|
|
10
9
|
};
|
|
11
10
|
|
|
12
|
-
const shallow = (a, b) => {
|
|
13
|
-
if (Object.is(a, b)) {
|
|
14
|
-
return true;
|
|
15
|
-
}
|
|
16
|
-
if (typeof a !== "object" || a === null || typeof b !== "object" || b === null) {
|
|
17
|
-
return false;
|
|
18
|
-
}
|
|
19
|
-
if (a instanceof Map && b instanceof Map) {
|
|
20
|
-
if (a.size !== b.size) return false;
|
|
21
|
-
for (const [key, value] of a) {
|
|
22
|
-
if (!Object.is(value, b.get(key))) {
|
|
23
|
-
return false;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
return true;
|
|
27
|
-
}
|
|
28
|
-
if (a instanceof Set && b instanceof Set) {
|
|
29
|
-
if (a.size !== b.size) return false;
|
|
30
|
-
for (const value of a) {
|
|
31
|
-
if (!b.has(value)) return false;
|
|
32
|
-
}
|
|
33
|
-
return true;
|
|
34
|
-
}
|
|
35
|
-
const keysA = Object.keys(a);
|
|
36
|
-
if (keysA.length !== Object.keys(b).length) {
|
|
37
|
-
return false;
|
|
38
|
-
}
|
|
39
|
-
for (let i = 0; i < keysA.length; i++) {
|
|
40
|
-
if (!Object.prototype.hasOwnProperty.call(b, keysA[i]) || !Object.is(a[keysA[i]], b[keysA[i]])) {
|
|
41
|
-
return false;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
return true;
|
|
45
|
-
};
|
|
46
|
-
|
|
47
11
|
const hasObjectPrototype = (value) => {
|
|
48
12
|
return Object.prototype.toString.call(value) === "[object Object]";
|
|
49
13
|
};
|
|
@@ -96,13 +60,15 @@ const initStore = (initialState, options = {}) => {
|
|
|
96
60
|
}
|
|
97
61
|
const prevState = state;
|
|
98
62
|
const newValue = getValue(value, state);
|
|
63
|
+
const changedKeys = [];
|
|
99
64
|
for (const key in newValue) {
|
|
100
65
|
if (!Object.is(prevState[key], newValue[key])) {
|
|
101
|
-
|
|
102
|
-
[...subscribers].forEach((subscriber) => subscriber(state, prevState));
|
|
103
|
-
return;
|
|
66
|
+
changedKeys.push(key);
|
|
104
67
|
}
|
|
105
68
|
}
|
|
69
|
+
if (changedKeys.length === 0) return;
|
|
70
|
+
state = { ...prevState, ...newValue };
|
|
71
|
+
[...subscribers].forEach((subscriber) => subscriber(state, prevState, changedKeys));
|
|
106
72
|
};
|
|
107
73
|
const storeApi = {
|
|
108
74
|
getState,
|
|
@@ -113,4 +79,4 @@ const initStore = (initialState, options = {}) => {
|
|
|
113
79
|
return storeApi;
|
|
114
80
|
};
|
|
115
81
|
|
|
116
|
-
export { getHash, getValue,
|
|
82
|
+
export { getHash, getValue, initStore, isClient, isPlainObject, noop };
|
package/package.json
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
"name": "floppy-disk",
|
|
3
3
|
"description": "Lightweight, simple, and powerful state management library",
|
|
4
4
|
"private": false,
|
|
5
|
-
"version": "3.0.0-beta.
|
|
5
|
+
"version": "3.0.0-beta.2",
|
|
6
6
|
"publishConfig": {
|
|
7
|
-
"tag": "
|
|
7
|
+
"tag": "beta"
|
|
8
8
|
},
|
|
9
9
|
"keywords": [
|
|
10
10
|
"utilities",
|
|
@@ -14,7 +14,7 @@ import { type InitStoreOptions, type SetState } from 'floppy-disk/vanilla';
|
|
|
14
14
|
* - No retry mechanism
|
|
15
15
|
* - No caching across executions
|
|
16
16
|
*/
|
|
17
|
-
export type MutationState<TData, TVariable> = {
|
|
17
|
+
export type MutationState<TData, TVariable, TError> = {
|
|
18
18
|
isPending: boolean;
|
|
19
19
|
} & ({
|
|
20
20
|
state: 'INITIAL';
|
|
@@ -41,7 +41,7 @@ export type MutationState<TData, TVariable> = {
|
|
|
41
41
|
variable: TVariable;
|
|
42
42
|
data: undefined;
|
|
43
43
|
dataUpdatedAt: undefined;
|
|
44
|
-
error:
|
|
44
|
+
error: TError;
|
|
45
45
|
errorUpdatedAt: number;
|
|
46
46
|
});
|
|
47
47
|
/**
|
|
@@ -50,19 +50,19 @@ export type MutationState<TData, TVariable> = {
|
|
|
50
50
|
* @remarks
|
|
51
51
|
* Lifecycle callbacks are triggered for each execution.
|
|
52
52
|
*/
|
|
53
|
-
export type MutationOptions<TData, TVariable> = InitStoreOptions<MutationState<TData, TVariable>> & {
|
|
53
|
+
export type MutationOptions<TData, TVariable, TError = Error> = InitStoreOptions<MutationState<TData, TVariable, TError>> & {
|
|
54
54
|
/**
|
|
55
55
|
* Called when the mutation succeeds.
|
|
56
56
|
*/
|
|
57
|
-
onSuccess?: (data: TData, variable: TVariable, stateBeforeExecute: MutationState<TData, TVariable>) => void;
|
|
57
|
+
onSuccess?: (data: TData, variable: TVariable, stateBeforeExecute: MutationState<TData, TVariable, TError>) => void;
|
|
58
58
|
/**
|
|
59
59
|
* Called when the mutation fails.
|
|
60
60
|
*/
|
|
61
|
-
onError?: (error:
|
|
61
|
+
onError?: (error: TError, variable: TVariable, stateBeforeExecute: MutationState<TData, TVariable, TError>) => void;
|
|
62
62
|
/**
|
|
63
63
|
* Called after the mutation settles (either success or error).
|
|
64
64
|
*/
|
|
65
|
-
onSettled?: (variable: TVariable, stateBeforeExecute: MutationState<TData, TVariable>) => void;
|
|
65
|
+
onSettled?: (variable: TVariable, stateBeforeExecute: MutationState<TData, TVariable, TError>) => void;
|
|
66
66
|
};
|
|
67
67
|
/**
|
|
68
68
|
* Creates a mutation store for handling async operations that modify data.
|
|
@@ -89,10 +89,10 @@ export type MutationOptions<TData, TVariable> = InitStoreOptions<MutationState<T
|
|
|
89
89
|
* const { isPending } = useCreateUser();
|
|
90
90
|
* const result = await useCreateUser.execute({ name: 'John' });
|
|
91
91
|
*/
|
|
92
|
-
export declare const createMutation: <TData, TVariable = undefined>(mutationFn: (variable: TVariable, stateBeforeExecute: MutationState<TData, TVariable>) => Promise<TData>, options?: MutationOptions<TData, TVariable>) => (
|
|
93
|
-
subscribe: (subscriber: import("../vanilla.ts").Subscriber<MutationState<TData, TVariable>>) => () => void;
|
|
94
|
-
getSubscribers: () => Set<import("../vanilla.ts").Subscriber<MutationState<TData, TVariable>>>;
|
|
95
|
-
getState: () => MutationState<TData, TVariable>;
|
|
92
|
+
export declare const createMutation: <TData, TVariable = undefined, TError = Error>(mutationFn: (variable: TVariable, stateBeforeExecute: MutationState<TData, TVariable, TError>) => Promise<TData>, options?: MutationOptions<TData, TVariable, TError>) => (() => MutationState<TData, TVariable, TError>) & {
|
|
93
|
+
subscribe: (subscriber: import("../vanilla.ts").Subscriber<MutationState<TData, TVariable, TError>>) => () => void;
|
|
94
|
+
getSubscribers: () => Set<import("../vanilla.ts").Subscriber<MutationState<TData, TVariable, TError>>>;
|
|
95
|
+
getState: () => MutationState<TData, TVariable, TError>;
|
|
96
96
|
/**
|
|
97
97
|
* Manually updates the mutation state.
|
|
98
98
|
*
|
|
@@ -100,7 +100,7 @@ export declare const createMutation: <TData, TVariable = undefined>(mutationFn:
|
|
|
100
100
|
* - Intended for advanced use cases.
|
|
101
101
|
* - Prefer using provided mutation actions (`execute`, `reset`) instead.
|
|
102
102
|
*/
|
|
103
|
-
setState: (value: SetState<MutationState<TData, TVariable>>) => void;
|
|
103
|
+
setState: (value: SetState<MutationState<TData, TVariable, TError>>) => void;
|
|
104
104
|
/**
|
|
105
105
|
* Executes the mutation.
|
|
106
106
|
*
|
|
@@ -118,11 +118,11 @@ export declare const createMutation: <TData, TVariable = undefined>(mutationFn:
|
|
|
118
118
|
execute: TVariable extends undefined ? () => Promise<{
|
|
119
119
|
variable: undefined;
|
|
120
120
|
data?: TData;
|
|
121
|
-
error?:
|
|
121
|
+
error?: TError;
|
|
122
122
|
}> : (variable: TVariable) => Promise<{
|
|
123
123
|
variable: TVariable;
|
|
124
124
|
data?: TData;
|
|
125
|
-
error?:
|
|
125
|
+
error?: TError;
|
|
126
126
|
}>;
|
|
127
127
|
/**
|
|
128
128
|
* Resets the mutation state back to its initial state.
|
package/react/create-query.d.ts
CHANGED
|
@@ -19,7 +19,7 @@ import { type InitStoreOptions, type SetState } from 'floppy-disk/vanilla';
|
|
|
19
19
|
* @remarks
|
|
20
20
|
* - Data and error are mutually exclusive except in `SUCCESS_BUT_REVALIDATION_ERROR`.
|
|
21
21
|
*/
|
|
22
|
-
export type QueryState<TData> = {
|
|
22
|
+
export type QueryState<TData, TError> = {
|
|
23
23
|
isPending: boolean;
|
|
24
24
|
isRevalidating: boolean;
|
|
25
25
|
isRetrying: boolean;
|
|
@@ -46,7 +46,7 @@ export type QueryState<TData> = {
|
|
|
46
46
|
isError: true;
|
|
47
47
|
data: undefined;
|
|
48
48
|
dataUpdatedAt: undefined;
|
|
49
|
-
error:
|
|
49
|
+
error: TError;
|
|
50
50
|
errorUpdatedAt: number;
|
|
51
51
|
} | {
|
|
52
52
|
state: 'SUCCESS_BUT_REVALIDATION_ERROR';
|
|
@@ -54,7 +54,7 @@ export type QueryState<TData> = {
|
|
|
54
54
|
isError: false;
|
|
55
55
|
data: TData;
|
|
56
56
|
dataUpdatedAt: number;
|
|
57
|
-
error:
|
|
57
|
+
error: TError;
|
|
58
58
|
errorUpdatedAt: number;
|
|
59
59
|
});
|
|
60
60
|
/**
|
|
@@ -63,13 +63,13 @@ export type QueryState<TData> = {
|
|
|
63
63
|
* @remarks
|
|
64
64
|
* Controls caching, retry behavior, lifecycle, and side effects of an async operation.
|
|
65
65
|
*/
|
|
66
|
-
export type QueryOptions<TData, TVariable extends Record<string, any
|
|
66
|
+
export type QueryOptions<TData, TVariable extends Record<string, any>, TError = Error> = InitStoreOptions<QueryState<TData, TError>> & {
|
|
67
67
|
/**
|
|
68
68
|
* Time (in milliseconds) that data is considered fresh.
|
|
69
69
|
*
|
|
70
|
-
* While fresh, revalidation will be skipped.
|
|
70
|
+
* While fresh, revalidation will be skipped unless explicitly invalidated.
|
|
71
71
|
*
|
|
72
|
-
* @default 2500 ms (2.5
|
|
72
|
+
* @default 2500 ms (2.5 seconds)
|
|
73
73
|
*/
|
|
74
74
|
staleTime?: number;
|
|
75
75
|
/**
|
|
@@ -95,15 +95,15 @@ export type QueryOptions<TData, TVariable extends Record<string, any>> = InitSto
|
|
|
95
95
|
/**
|
|
96
96
|
* Called when the query succeeds.
|
|
97
97
|
*/
|
|
98
|
-
onSuccess?: (data: TData, variable: TVariable, stateBeforeExecute: QueryState<TData>) => void;
|
|
98
|
+
onSuccess?: (data: TData, variable: TVariable, stateBeforeExecute: QueryState<TData, TError>) => void;
|
|
99
99
|
/**
|
|
100
100
|
* Called when the query fails and will not retry.
|
|
101
101
|
*/
|
|
102
|
-
onError?: (error:
|
|
102
|
+
onError?: (error: TError, variable: TVariable, stateBeforeExecute: QueryState<TData, TError>) => void;
|
|
103
103
|
/**
|
|
104
104
|
* Called after the query settles (success or final failure).
|
|
105
105
|
*/
|
|
106
|
-
onSettled?: (variable: TVariable, stateBeforeExecute: QueryState<TData>) => void;
|
|
106
|
+
onSettled?: (variable: TVariable, stateBeforeExecute: QueryState<TData, TError>) => void;
|
|
107
107
|
/**
|
|
108
108
|
* Determines whether a failed query should retry.
|
|
109
109
|
*
|
|
@@ -120,7 +120,7 @@ export type QueryOptions<TData, TVariable extends Record<string, any>> = InitSto
|
|
|
120
120
|
* return [false];
|
|
121
121
|
* }
|
|
122
122
|
*/
|
|
123
|
-
shouldRetry?: (error:
|
|
123
|
+
shouldRetry?: (error: TError, currentState: QueryState<TData, TError>) => [true, number] | [false];
|
|
124
124
|
};
|
|
125
125
|
/**
|
|
126
126
|
* Creates a query factory for managing cached async operations.
|
|
@@ -131,16 +131,20 @@ export type QueryOptions<TData, TVariable extends Record<string, any>> = InitSto
|
|
|
131
131
|
* @returns A function to retrieve or create a query instance by variable
|
|
132
132
|
*
|
|
133
133
|
* @remarks
|
|
134
|
-
* - Queries are
|
|
134
|
+
* - Queries are **keyed by variable** (via deterministic hashing).
|
|
135
135
|
* - Each unique variable maps to its own store instance.
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
*
|
|
142
|
-
* -
|
|
143
|
-
*
|
|
136
|
+
*
|
|
137
|
+
* Core features:
|
|
138
|
+
* - Caching via `staleTime`
|
|
139
|
+
* - Explicit invalidation (independent of freshness)
|
|
140
|
+
* - Retry logic via `shouldRetry`
|
|
141
|
+
* - Background revalidation (focus / reconnect)
|
|
142
|
+
* - Garbage collection via `gcTime`
|
|
143
|
+
*
|
|
144
|
+
* Execution behavior:
|
|
145
|
+
* - By default, executions **overwrite ongoing executions**
|
|
146
|
+
* - Set `overwriteOngoingExecution: false` to enable deduplication
|
|
147
|
+
* - Internal revalidation (focus/reconnect) uses deduplication by default
|
|
144
148
|
*
|
|
145
149
|
* @example
|
|
146
150
|
* const userQuery = createQuery<UserDetail, { id: string }>(async ({ id }) => {
|
|
@@ -153,39 +157,36 @@ export type QueryOptions<TData, TVariable extends Record<string, any>> = InitSto
|
|
|
153
157
|
* // ...
|
|
154
158
|
* }
|
|
155
159
|
*/
|
|
156
|
-
export declare const createQuery: <TData, TVariable extends Record<string, any> = never>(queryFn: (variable: TVariable, currentState: QueryState<TData>) => Promise<TData>, options?: QueryOptions<TData, TVariable>) => ((variable?: TVariable) => {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
}, selector?: (state: QueryState<TData>) => TStateSlice): TStateSlice;
|
|
181
|
-
<TStateSlice = QueryState<TData>>(selector?: (state: QueryState<TData>) => TStateSlice): TStateSlice;
|
|
182
|
-
} & {
|
|
160
|
+
export declare const createQuery: <TData, TVariable extends Record<string, any> = never, TError = Error>(queryFn: (variable: TVariable, currentState: QueryState<TData, TError>) => Promise<TData>, options?: QueryOptions<TData, TVariable, TError>) => ((variable?: TVariable) => ((options?: {
|
|
161
|
+
/**
|
|
162
|
+
* Whether the query should execute automatically on mount.
|
|
163
|
+
*
|
|
164
|
+
* @default true
|
|
165
|
+
*/
|
|
166
|
+
enabled?: boolean;
|
|
167
|
+
/**
|
|
168
|
+
* Whether to keep previous successful data while a new variable is loading.
|
|
169
|
+
*
|
|
170
|
+
* @remarks
|
|
171
|
+
* - Only applies when the query is in the `INITIAL` state (no data & no error).
|
|
172
|
+
* - Intended for variable changes:
|
|
173
|
+
* when switching from one variable to another, the previous data is temporarily shown
|
|
174
|
+
* while the new execution is in progress.
|
|
175
|
+
* - Once the new execution resolves (success or error), the previous data is no longer used.
|
|
176
|
+
* - Prevents UI flicker (e.g. empty/loading state) during transitions.
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* // Switching from userId=1 → userId=2
|
|
180
|
+
* // While loading userId=2, still show userId=1 data
|
|
181
|
+
* useQuery({ id: userId }, { keepPreviousData: true });
|
|
182
|
+
*/ keepPreviousData?: boolean;
|
|
183
|
+
}) => QueryState<TData, TError>) & {
|
|
183
184
|
metadata: {
|
|
184
185
|
isInvalidated?: boolean;
|
|
185
|
-
promise?: Promise<QueryState<TData>> | undefined;
|
|
186
|
-
promiseResolver?: ((value: QueryState<TData> | PromiseLike<QueryState<TData>>) => void) | undefined;
|
|
186
|
+
promise?: Promise<QueryState<TData, TError>> | undefined;
|
|
187
|
+
promiseResolver?: ((value: QueryState<TData, TError> | PromiseLike<QueryState<TData, TError>>) => void) | undefined;
|
|
187
188
|
retryTimeoutId?: number;
|
|
188
|
-
retryResolver?: ((value: QueryState<TData> | PromiseLike<QueryState<TData>>) => void) | undefined;
|
|
189
|
+
retryResolver?: ((value: QueryState<TData, TError> | PromiseLike<QueryState<TData, TError>>) => void) | undefined;
|
|
189
190
|
garbageCollectionTimeoutId?: number;
|
|
190
191
|
rollbackData?: TData | undefined;
|
|
191
192
|
};
|
|
@@ -211,7 +212,7 @@ export declare const createQuery: <TData, TVariable extends Record<string, any>
|
|
|
211
212
|
* @returns A promise resolving to the latest query state
|
|
212
213
|
*
|
|
213
214
|
* @remarks
|
|
214
|
-
* - By default, each call starts a new execution even if one is already in progress.
|
|
215
|
+
* - By default, each call **starts a new execution** even if one is already in progress.
|
|
215
216
|
* - Set `overwriteOngoingExecution: false` to reuse an ongoing execution (deduplication).
|
|
216
217
|
* - Handles:
|
|
217
218
|
* - Pending state
|
|
@@ -221,7 +222,7 @@ export declare const createQuery: <TData, TVariable extends Record<string, any>
|
|
|
221
222
|
*/
|
|
222
223
|
execute: (options?: {
|
|
223
224
|
overwriteOngoingExecution?: boolean;
|
|
224
|
-
}) => Promise<QueryState<TData>>;
|
|
225
|
+
}) => Promise<QueryState<TData, TError>>;
|
|
225
226
|
/**
|
|
226
227
|
* Re-executes the query if needed based on freshness or invalidation.
|
|
227
228
|
*
|
|
@@ -237,7 +238,7 @@ export declare const createQuery: <TData, TVariable extends Record<string, any>
|
|
|
237
238
|
*/
|
|
238
239
|
revalidate: (options?: {
|
|
239
240
|
overwriteOngoingExecution?: boolean;
|
|
240
|
-
}) => Promise<QueryState<TData>>;
|
|
241
|
+
}) => Promise<QueryState<TData, TError>>;
|
|
241
242
|
/**
|
|
242
243
|
* Marks the query as invalidated and optionally triggers re-execution.
|
|
243
244
|
*
|
|
@@ -289,7 +290,7 @@ export declare const createQuery: <TData, TVariable extends Record<string, any>
|
|
|
289
290
|
* const { rollback, revalidate } = query.optimisticUpdate(newData);
|
|
290
291
|
*/
|
|
291
292
|
optimisticUpdate: (data: TData) => {
|
|
292
|
-
revalidate: () => Promise<QueryState<TData>>;
|
|
293
|
+
revalidate: () => Promise<QueryState<TData, TError>>;
|
|
293
294
|
rollback: () => TData;
|
|
294
295
|
};
|
|
295
296
|
/**
|
|
@@ -301,10 +302,10 @@ export declare const createQuery: <TData, TVariable extends Record<string, any>
|
|
|
301
302
|
* - Should be used if an optimistic update fails.
|
|
302
303
|
*/
|
|
303
304
|
rollbackOptimisticUpdate: () => TData;
|
|
304
|
-
subscribe: (subscriber: import("../vanilla.ts").Subscriber<QueryState<TData>>) => () => void;
|
|
305
|
-
getSubscribers: () => Set<import("../vanilla.ts").Subscriber<QueryState<TData>>>;
|
|
306
|
-
getState: () => QueryState<TData>;
|
|
307
|
-
setState: (value: SetState<QueryState<TData>>) => void;
|
|
305
|
+
subscribe: (subscriber: import("../vanilla.ts").Subscriber<QueryState<TData, TError>>) => () => void;
|
|
306
|
+
getSubscribers: () => Set<import("../vanilla.ts").Subscriber<QueryState<TData, TError>>>;
|
|
307
|
+
getState: () => QueryState<TData, TError>;
|
|
308
|
+
setState: (value: SetState<QueryState<TData, TError>>) => void;
|
|
308
309
|
}) & {
|
|
309
310
|
/**
|
|
310
311
|
* Executes all query instances.
|
package/react/create-store.d.ts
CHANGED
|
@@ -12,14 +12,17 @@ import { type InitStoreOptions } from 'floppy-disk/vanilla';
|
|
|
12
12
|
* @remarks
|
|
13
13
|
* - Combines the vanilla store with React integration.
|
|
14
14
|
* - The returned function can be used directly as a hook.
|
|
15
|
+
* - The hook uses Proxy-based tracking to automatically detect which state fields are used.
|
|
16
|
+
* - Components will only re-render when the accessed values change.
|
|
15
17
|
*
|
|
16
18
|
* @example
|
|
17
|
-
* const
|
|
19
|
+
* const useMyStore = createStore({ foo: 1, bar: 2 });
|
|
18
20
|
*
|
|
19
21
|
* function Component() {
|
|
20
|
-
* const
|
|
22
|
+
* const state = useMyStore();
|
|
23
|
+
* return <div>{state.foo}</div>;
|
|
21
24
|
* }
|
|
22
25
|
*
|
|
23
|
-
*
|
|
26
|
+
* useMyStore.setState({ foo: 2 }); // only components using foo will re-render
|
|
24
27
|
*/
|
|
25
|
-
export declare const createStore: <TState extends Record<string, any>>(initialState: TState, options?: InitStoreOptions<TState>) => (
|
|
28
|
+
export declare const createStore: <TState extends Record<string, any>>(initialState: TState, options?: InitStoreOptions<TState>) => (() => TState) & import("../vanilla.ts").StoreApi<TState>;
|
package/react/create-stores.d.ts
CHANGED
|
@@ -12,18 +12,25 @@ import { type InitStoreOptions } from 'floppy-disk/vanilla';
|
|
|
12
12
|
* - Keys are deterministically hashed, ensuring stable identity.
|
|
13
13
|
* - Stores are lazily created and cached.
|
|
14
14
|
* - Each store has its own state, subscribers, and lifecycle.
|
|
15
|
+
* - Each returned store includes:
|
|
16
|
+
* - React hook (with Proxy-based tracking)
|
|
17
|
+
* - Store API methods
|
|
18
|
+
* - `delete()` for manual cleanup
|
|
15
19
|
* - Useful for scenarios like:
|
|
16
20
|
* - Query caches
|
|
17
21
|
* - Entity-based state
|
|
18
22
|
* - Dynamic instances
|
|
19
23
|
*
|
|
20
24
|
* @example
|
|
21
|
-
* const
|
|
25
|
+
* const userStore = createStores<{ name: string }, { id: number }>({ name: '' });
|
|
22
26
|
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
27
|
+
* function Component() {
|
|
28
|
+
* const useUserStore = userStore({ id: 1 });
|
|
29
|
+
* const state = useUserStore();
|
|
30
|
+
* return <div>{state.name}</div>;
|
|
31
|
+
* }
|
|
25
32
|
*/
|
|
26
|
-
export declare const createStores: <TState extends Record<string, any>, TKey extends Record<string, any>>(initialState: TState, options?: InitStoreOptions<TState>) => (key?: TKey) => (
|
|
33
|
+
export declare const createStores: <TState extends Record<string, any>, TKey extends Record<string, any>>(initialState: TState, options?: InitStoreOptions<TState>) => (key?: TKey) => (() => TState) & {
|
|
27
34
|
delete: () => boolean;
|
|
28
35
|
setState: (value: import("../vanilla.ts").SetState<TState>) => void;
|
|
29
36
|
getState: () => TState;
|
package/react/use-store.d.ts
CHANGED
|
@@ -1,18 +1,28 @@
|
|
|
1
1
|
import { type StoreApi } from 'floppy-disk/vanilla';
|
|
2
|
-
|
|
2
|
+
type Path = Array<string | number | symbol>;
|
|
3
|
+
export declare const getValueByPath: (obj: any, path: Path) => any;
|
|
4
|
+
export declare const isPrefixPath: (candidatePrefix: Path, targetPath: Path) => boolean;
|
|
5
|
+
export declare const compressPaths: (paths: Path[]) => Path[];
|
|
6
|
+
export declare const useStoreStateProxy: <TState extends Record<string, any>>(storeState: TState) => readonly [TState, import("react").RefObject<Path[]>];
|
|
3
7
|
/**
|
|
4
|
-
* React hook for subscribing to a store
|
|
8
|
+
* React hook for subscribing to a store using automatic dependency tracking.
|
|
5
9
|
*
|
|
6
10
|
* @param store - The store instance to subscribe to
|
|
7
|
-
* @param selector - Optional selector to derive a slice of state
|
|
8
11
|
*
|
|
9
|
-
* @returns
|
|
12
|
+
* @returns A proxied version of the store state
|
|
10
13
|
*
|
|
11
14
|
* @remarks
|
|
12
|
-
* -
|
|
13
|
-
* - The
|
|
15
|
+
* - This hook uses a **Proxy-based tracking mechanism** to detect which parts of the state are accessed during render.
|
|
16
|
+
* - The component will only re-render when the **accessed values actually change**.
|
|
17
|
+
* - State must be treated as **immutable**:
|
|
18
|
+
* - Updates must replace objects rather than mutate them
|
|
19
|
+
* - Otherwise, changes may not be detected
|
|
20
|
+
* - No selector or memoization is needed.
|
|
14
21
|
*
|
|
15
22
|
* @example
|
|
16
|
-
* const
|
|
23
|
+
* const state = useStoreState(store);
|
|
24
|
+
* return <div>{state.user.name}</div>;
|
|
25
|
+
* // Component will only re-render if `user.name` changes
|
|
17
26
|
*/
|
|
18
|
-
export declare const useStoreState: <TState extends Record<string, any
|
|
27
|
+
export declare const useStoreState: <TState extends Record<string, any>>(storeState: TState, subscribe: StoreApi<TState>["subscribe"]) => TState;
|
|
28
|
+
export {};
|
package/react.d.ts
CHANGED