floppy-disk 2.1.0-beta.1 → 2.1.0-beta.3
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/preact/create-query.d.ts +2 -1
- package/esm/preact/create-query.js +12 -9
- package/esm/preact/create-stores.d.ts +4 -0
- package/esm/preact/create-stores.js +2 -1
- package/esm/react/create-query.d.ts +2 -1
- package/esm/react/create-query.js +12 -9
- package/esm/react/create-stores.d.ts +4 -0
- package/esm/react/create-stores.js +2 -1
- package/esm/utils/index.d.ts +2 -1
- package/esm/utils/index.js +2 -1
- package/lib/preact/create-query.d.ts +2 -1
- package/lib/preact/create-query.js +11 -8
- package/lib/preact/create-stores.d.ts +4 -0
- package/lib/preact/create-stores.js +2 -1
- package/lib/react/create-query.d.ts +2 -1
- package/lib/react/create-query.js +11 -8
- package/lib/react/create-stores.d.ts +4 -0
- package/lib/react/create-stores.js +2 -1
- package/lib/utils/index.d.ts +2 -1
- package/lib/utils/index.js +4 -2
- package/package.json +1 -1
|
@@ -103,7 +103,7 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
|
|
|
103
103
|
isError: false;
|
|
104
104
|
data: TData;
|
|
105
105
|
response: TResponse;
|
|
106
|
-
responseUpdatedAt: number;
|
|
106
|
+
responseUpdatedAt: number | null;
|
|
107
107
|
} | {
|
|
108
108
|
status: 'error';
|
|
109
109
|
isLoading: false;
|
|
@@ -186,6 +186,7 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
|
|
|
186
186
|
setInitialResponse: (options: {
|
|
187
187
|
key?: TKey;
|
|
188
188
|
response: TResponse;
|
|
189
|
+
skipRevalidation?: boolean;
|
|
189
190
|
}) => void;
|
|
190
191
|
/**
|
|
191
192
|
* Set query state (data, error, etc) to initial state.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useState } from 'preact/hooks';
|
|
2
|
-
import { identityFn, noop } from '../utils';
|
|
2
|
+
import { hasValue, identityFn, noop } from '../utils';
|
|
3
3
|
import { createStores } from './create-stores';
|
|
4
4
|
const getDecision = (value, param, { ifTrue, ifAlways }) => {
|
|
5
5
|
if (value === true || (typeof value === 'function' && value(param) === true)) {
|
|
@@ -84,7 +84,7 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
84
84
|
responseAllPages.push(response);
|
|
85
85
|
const newPageParam = getNextPageParam(response, responseAllPages.length);
|
|
86
86
|
newPageParams.push(newPageParam);
|
|
87
|
-
if (newPageParam
|
|
87
|
+
if (hasValue(newPageParam) && newPageParams.length < pageParams.length) {
|
|
88
88
|
pageParam = newPageParam;
|
|
89
89
|
callQuery();
|
|
90
90
|
return;
|
|
@@ -109,7 +109,7 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
109
109
|
retryCount: 0,
|
|
110
110
|
pageParam: newPageParam,
|
|
111
111
|
pageParams: newPageParams,
|
|
112
|
-
hasNextPage: newPageParam
|
|
112
|
+
hasNextPage: hasValue(newPageParam),
|
|
113
113
|
});
|
|
114
114
|
onSuccess(response, stateBeforeCallQuery);
|
|
115
115
|
})
|
|
@@ -131,7 +131,7 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
131
131
|
errorUpdatedAt,
|
|
132
132
|
isGoingToRetry: shouldRetry,
|
|
133
133
|
pageParam,
|
|
134
|
-
hasNextPage: pageParam
|
|
134
|
+
hasNextPage: hasValue(pageParam),
|
|
135
135
|
}
|
|
136
136
|
: {
|
|
137
137
|
isWaiting: false,
|
|
@@ -142,7 +142,7 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
142
142
|
errorUpdatedAt,
|
|
143
143
|
isGoingToRetry: shouldRetry,
|
|
144
144
|
pageParam,
|
|
145
|
-
hasNextPage: pageParam
|
|
145
|
+
hasNextPage: hasValue(pageParam),
|
|
146
146
|
});
|
|
147
147
|
if (shouldRetry) {
|
|
148
148
|
retryTimeoutId.set(keyHash, window.setTimeout(() => {
|
|
@@ -166,6 +166,9 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
166
166
|
forceFetch();
|
|
167
167
|
};
|
|
168
168
|
const fetchNextPage = () => {
|
|
169
|
+
if (typeof options.getNextPageParam !== 'function') {
|
|
170
|
+
return console.warn('fetchNextPage with invalid getNextPageParam option');
|
|
171
|
+
}
|
|
169
172
|
const state = get();
|
|
170
173
|
const { isLoading, isWaitingNextPage, data, hasNextPage, pageParam, pageParams } = state;
|
|
171
174
|
if (isLoading)
|
|
@@ -184,7 +187,7 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
184
187
|
data: select(response, { key, data }),
|
|
185
188
|
pageParam: newPageParam,
|
|
186
189
|
pageParams: pageParams.concat(newPageParam),
|
|
187
|
-
hasNextPage: newPageParam
|
|
190
|
+
hasNextPage: hasValue(newPageParam),
|
|
188
191
|
});
|
|
189
192
|
})
|
|
190
193
|
.catch((error) => {
|
|
@@ -271,7 +274,7 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
271
274
|
},
|
|
272
275
|
};
|
|
273
276
|
})());
|
|
274
|
-
useQuery.setInitialResponse = ({ key, response }) => {
|
|
277
|
+
useQuery.setInitialResponse = ({ key, response, skipRevalidation }) => {
|
|
275
278
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
276
279
|
useState(() => {
|
|
277
280
|
if (response === undefined || useQuery.get(key).data)
|
|
@@ -283,11 +286,11 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
283
286
|
isSuccess: true,
|
|
284
287
|
isError: false,
|
|
285
288
|
response,
|
|
286
|
-
responseUpdatedAt: Date.now(),
|
|
289
|
+
responseUpdatedAt: skipRevalidation ? Date.now() : null,
|
|
287
290
|
data: select(response, { key: key, data: null }),
|
|
288
291
|
pageParam: newPageParam,
|
|
289
292
|
pageParams: [undefined, newPageParam],
|
|
290
|
-
hasNextPage: newPageParam
|
|
293
|
+
hasNextPage: hasValue(newPageParam),
|
|
291
294
|
});
|
|
292
295
|
});
|
|
293
296
|
};
|
|
@@ -39,6 +39,10 @@ export type UseStores<TKey extends StoreKey = StoreKey, T extends StoreData = St
|
|
|
39
39
|
};
|
|
40
40
|
export type CreateStoresOptions<TKey extends StoreKey = StoreKey, T extends StoreData = StoreData> = InitStoreOptions<T> & {
|
|
41
41
|
onBeforeChangeKey?: (nextKey: TKey, prevKey: TKey) => void;
|
|
42
|
+
/**
|
|
43
|
+
* Will be triggered when a single store with a specific key was initialized.
|
|
44
|
+
*/
|
|
45
|
+
onStoreInitialized?: (key: TKey, keyHash: string) => void;
|
|
42
46
|
defaultDeps?: SelectDeps<T>;
|
|
43
47
|
hashKeyFn?: (obj: TKey) => string;
|
|
44
48
|
};
|
|
@@ -2,13 +2,14 @@ import { useEffect, useMemo, useRef, useState } from 'preact/hooks';
|
|
|
2
2
|
import { hashStoreKey, noop } from '../utils';
|
|
3
3
|
import { initStore, } from '../vanilla';
|
|
4
4
|
export const createStores = (initializer, options = {}) => {
|
|
5
|
-
const { onBeforeChangeKey = noop, defaultDeps, hashKeyFn = hashStoreKey } = options;
|
|
5
|
+
const { onBeforeChangeKey = noop, onStoreInitialized = noop, defaultDeps, hashKeyFn = hashStoreKey, } = options;
|
|
6
6
|
const stores = new Map();
|
|
7
7
|
const getStore = (_key) => {
|
|
8
8
|
const key = _key || {};
|
|
9
9
|
const keyHash = hashKeyFn(key);
|
|
10
10
|
if (!stores.has(keyHash)) {
|
|
11
11
|
stores.set(keyHash, initStore((api) => initializer({ key, keyHash, ...api }), options));
|
|
12
|
+
onStoreInitialized(key, keyHash);
|
|
12
13
|
}
|
|
13
14
|
return stores.get(keyHash);
|
|
14
15
|
};
|
|
@@ -103,7 +103,7 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
|
|
|
103
103
|
isError: false;
|
|
104
104
|
data: TData;
|
|
105
105
|
response: TResponse;
|
|
106
|
-
responseUpdatedAt: number;
|
|
106
|
+
responseUpdatedAt: number | null;
|
|
107
107
|
} | {
|
|
108
108
|
status: 'error';
|
|
109
109
|
isLoading: false;
|
|
@@ -186,6 +186,7 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
|
|
|
186
186
|
setInitialResponse: (options: {
|
|
187
187
|
key?: TKey;
|
|
188
188
|
response: TResponse;
|
|
189
|
+
skipRevalidation?: boolean;
|
|
189
190
|
}) => void;
|
|
190
191
|
/**
|
|
191
192
|
* Set query state (data, error, etc) to initial state.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useState } from 'react';
|
|
2
|
-
import { identityFn, noop } from '../utils';
|
|
2
|
+
import { hasValue, identityFn, noop } from '../utils';
|
|
3
3
|
import { createStores } from './create-stores';
|
|
4
4
|
const getDecision = (value, param, { ifTrue, ifAlways }) => {
|
|
5
5
|
if (value === true || (typeof value === 'function' && value(param) === true)) {
|
|
@@ -84,7 +84,7 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
84
84
|
responseAllPages.push(response);
|
|
85
85
|
const newPageParam = getNextPageParam(response, responseAllPages.length);
|
|
86
86
|
newPageParams.push(newPageParam);
|
|
87
|
-
if (newPageParam
|
|
87
|
+
if (hasValue(newPageParam) && newPageParams.length < pageParams.length) {
|
|
88
88
|
pageParam = newPageParam;
|
|
89
89
|
callQuery();
|
|
90
90
|
return;
|
|
@@ -109,7 +109,7 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
109
109
|
retryCount: 0,
|
|
110
110
|
pageParam: newPageParam,
|
|
111
111
|
pageParams: newPageParams,
|
|
112
|
-
hasNextPage: newPageParam
|
|
112
|
+
hasNextPage: hasValue(newPageParam),
|
|
113
113
|
});
|
|
114
114
|
onSuccess(response, stateBeforeCallQuery);
|
|
115
115
|
})
|
|
@@ -131,7 +131,7 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
131
131
|
errorUpdatedAt,
|
|
132
132
|
isGoingToRetry: shouldRetry,
|
|
133
133
|
pageParam,
|
|
134
|
-
hasNextPage: pageParam
|
|
134
|
+
hasNextPage: hasValue(pageParam),
|
|
135
135
|
}
|
|
136
136
|
: {
|
|
137
137
|
isWaiting: false,
|
|
@@ -142,7 +142,7 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
142
142
|
errorUpdatedAt,
|
|
143
143
|
isGoingToRetry: shouldRetry,
|
|
144
144
|
pageParam,
|
|
145
|
-
hasNextPage: pageParam
|
|
145
|
+
hasNextPage: hasValue(pageParam),
|
|
146
146
|
});
|
|
147
147
|
if (shouldRetry) {
|
|
148
148
|
retryTimeoutId.set(keyHash, window.setTimeout(() => {
|
|
@@ -166,6 +166,9 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
166
166
|
forceFetch();
|
|
167
167
|
};
|
|
168
168
|
const fetchNextPage = () => {
|
|
169
|
+
if (typeof options.getNextPageParam !== 'function') {
|
|
170
|
+
return console.warn('fetchNextPage with invalid getNextPageParam option');
|
|
171
|
+
}
|
|
169
172
|
const state = get();
|
|
170
173
|
const { isLoading, isWaitingNextPage, data, hasNextPage, pageParam, pageParams } = state;
|
|
171
174
|
if (isLoading)
|
|
@@ -184,7 +187,7 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
184
187
|
data: select(response, { key, data }),
|
|
185
188
|
pageParam: newPageParam,
|
|
186
189
|
pageParams: pageParams.concat(newPageParam),
|
|
187
|
-
hasNextPage: newPageParam
|
|
190
|
+
hasNextPage: hasValue(newPageParam),
|
|
188
191
|
});
|
|
189
192
|
})
|
|
190
193
|
.catch((error) => {
|
|
@@ -271,7 +274,7 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
271
274
|
},
|
|
272
275
|
};
|
|
273
276
|
})());
|
|
274
|
-
useQuery.setInitialResponse = ({ key, response }) => {
|
|
277
|
+
useQuery.setInitialResponse = ({ key, response, skipRevalidation }) => {
|
|
275
278
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
276
279
|
useState(() => {
|
|
277
280
|
if (response === undefined || useQuery.get(key).data)
|
|
@@ -283,11 +286,11 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
283
286
|
isSuccess: true,
|
|
284
287
|
isError: false,
|
|
285
288
|
response,
|
|
286
|
-
responseUpdatedAt: Date.now(),
|
|
289
|
+
responseUpdatedAt: skipRevalidation ? Date.now() : null,
|
|
287
290
|
data: select(response, { key: key, data: null }),
|
|
288
291
|
pageParam: newPageParam,
|
|
289
292
|
pageParams: [undefined, newPageParam],
|
|
290
|
-
hasNextPage: newPageParam
|
|
293
|
+
hasNextPage: hasValue(newPageParam),
|
|
291
294
|
});
|
|
292
295
|
});
|
|
293
296
|
};
|
|
@@ -39,6 +39,10 @@ export type UseStores<TKey extends StoreKey = StoreKey, T extends StoreData = St
|
|
|
39
39
|
};
|
|
40
40
|
export type CreateStoresOptions<TKey extends StoreKey = StoreKey, T extends StoreData = StoreData> = InitStoreOptions<T> & {
|
|
41
41
|
onBeforeChangeKey?: (nextKey: TKey, prevKey: TKey) => void;
|
|
42
|
+
/**
|
|
43
|
+
* Will be triggered when a single store with a specific key was initialized.
|
|
44
|
+
*/
|
|
45
|
+
onStoreInitialized?: (key: TKey, keyHash: string) => void;
|
|
42
46
|
defaultDeps?: SelectDeps<T>;
|
|
43
47
|
hashKeyFn?: (obj: TKey) => string;
|
|
44
48
|
};
|
|
@@ -2,13 +2,14 @@ import { useEffect, useMemo, useRef, useState } from 'react';
|
|
|
2
2
|
import { hashStoreKey, noop } from '../utils';
|
|
3
3
|
import { initStore, } from '../vanilla';
|
|
4
4
|
export const createStores = (initializer, options = {}) => {
|
|
5
|
-
const { onBeforeChangeKey = noop, defaultDeps, hashKeyFn = hashStoreKey } = options;
|
|
5
|
+
const { onBeforeChangeKey = noop, onStoreInitialized = noop, defaultDeps, hashKeyFn = hashStoreKey, } = options;
|
|
6
6
|
const stores = new Map();
|
|
7
7
|
const getStore = (_key) => {
|
|
8
8
|
const key = _key || {};
|
|
9
9
|
const keyHash = hashKeyFn(key);
|
|
10
10
|
if (!stores.has(keyHash)) {
|
|
11
11
|
stores.set(keyHash, initStore((api) => initializer({ key, keyHash, ...api }), options));
|
|
12
|
+
onStoreInitialized(key, keyHash);
|
|
12
13
|
}
|
|
13
14
|
return stores.get(keyHash);
|
|
14
15
|
};
|
package/esm/utils/index.d.ts
CHANGED
package/esm/utils/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export const noop = () => { };
|
|
2
|
-
export const identityFn = (
|
|
2
|
+
export const identityFn = (value) => value;
|
|
3
|
+
export const hasValue = (value) => value !== undefined && value !== null;
|
|
3
4
|
export const hashStoreKey = (obj) => JSON.stringify(obj, Object.keys(obj).sort());
|
|
@@ -103,7 +103,7 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
|
|
|
103
103
|
isError: false;
|
|
104
104
|
data: TData;
|
|
105
105
|
response: TResponse;
|
|
106
|
-
responseUpdatedAt: number;
|
|
106
|
+
responseUpdatedAt: number | null;
|
|
107
107
|
} | {
|
|
108
108
|
status: 'error';
|
|
109
109
|
isLoading: false;
|
|
@@ -186,6 +186,7 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
|
|
|
186
186
|
setInitialResponse: (options: {
|
|
187
187
|
key?: TKey;
|
|
188
188
|
response: TResponse;
|
|
189
|
+
skipRevalidation?: boolean;
|
|
189
190
|
}) => void;
|
|
190
191
|
/**
|
|
191
192
|
* Set query state (data, error, etc) to initial state.
|
|
@@ -87,7 +87,7 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
87
87
|
responseAllPages.push(response);
|
|
88
88
|
const newPageParam = getNextPageParam(response, responseAllPages.length);
|
|
89
89
|
newPageParams.push(newPageParam);
|
|
90
|
-
if (newPageParam
|
|
90
|
+
if ((0, utils_1.hasValue)(newPageParam) && newPageParams.length < pageParams.length) {
|
|
91
91
|
pageParam = newPageParam;
|
|
92
92
|
callQuery();
|
|
93
93
|
return;
|
|
@@ -112,7 +112,7 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
112
112
|
retryCount: 0,
|
|
113
113
|
pageParam: newPageParam,
|
|
114
114
|
pageParams: newPageParams,
|
|
115
|
-
hasNextPage: newPageParam
|
|
115
|
+
hasNextPage: (0, utils_1.hasValue)(newPageParam),
|
|
116
116
|
});
|
|
117
117
|
onSuccess(response, stateBeforeCallQuery);
|
|
118
118
|
})
|
|
@@ -134,7 +134,7 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
134
134
|
errorUpdatedAt,
|
|
135
135
|
isGoingToRetry: shouldRetry,
|
|
136
136
|
pageParam,
|
|
137
|
-
hasNextPage: pageParam
|
|
137
|
+
hasNextPage: (0, utils_1.hasValue)(pageParam),
|
|
138
138
|
}
|
|
139
139
|
: {
|
|
140
140
|
isWaiting: false,
|
|
@@ -145,7 +145,7 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
145
145
|
errorUpdatedAt,
|
|
146
146
|
isGoingToRetry: shouldRetry,
|
|
147
147
|
pageParam,
|
|
148
|
-
hasNextPage: pageParam
|
|
148
|
+
hasNextPage: (0, utils_1.hasValue)(pageParam),
|
|
149
149
|
});
|
|
150
150
|
if (shouldRetry) {
|
|
151
151
|
retryTimeoutId.set(keyHash, window.setTimeout(() => {
|
|
@@ -169,6 +169,9 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
169
169
|
forceFetch();
|
|
170
170
|
};
|
|
171
171
|
const fetchNextPage = () => {
|
|
172
|
+
if (typeof options.getNextPageParam !== 'function') {
|
|
173
|
+
return console.warn('fetchNextPage with invalid getNextPageParam option');
|
|
174
|
+
}
|
|
172
175
|
const state = get();
|
|
173
176
|
const { isLoading, isWaitingNextPage, data, hasNextPage, pageParam, pageParams } = state;
|
|
174
177
|
if (isLoading)
|
|
@@ -187,7 +190,7 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
187
190
|
data: select(response, { key, data }),
|
|
188
191
|
pageParam: newPageParam,
|
|
189
192
|
pageParams: pageParams.concat(newPageParam),
|
|
190
|
-
hasNextPage: newPageParam
|
|
193
|
+
hasNextPage: (0, utils_1.hasValue)(newPageParam),
|
|
191
194
|
});
|
|
192
195
|
})
|
|
193
196
|
.catch((error) => {
|
|
@@ -274,7 +277,7 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
274
277
|
},
|
|
275
278
|
};
|
|
276
279
|
})());
|
|
277
|
-
useQuery.setInitialResponse = ({ key, response }) => {
|
|
280
|
+
useQuery.setInitialResponse = ({ key, response, skipRevalidation }) => {
|
|
278
281
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
279
282
|
(0, hooks_1.useState)(() => {
|
|
280
283
|
if (response === undefined || useQuery.get(key).data)
|
|
@@ -286,11 +289,11 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
286
289
|
isSuccess: true,
|
|
287
290
|
isError: false,
|
|
288
291
|
response,
|
|
289
|
-
responseUpdatedAt: Date.now(),
|
|
292
|
+
responseUpdatedAt: skipRevalidation ? Date.now() : null,
|
|
290
293
|
data: select(response, { key: key, data: null }),
|
|
291
294
|
pageParam: newPageParam,
|
|
292
295
|
pageParams: [undefined, newPageParam],
|
|
293
|
-
hasNextPage: newPageParam
|
|
296
|
+
hasNextPage: (0, utils_1.hasValue)(newPageParam),
|
|
294
297
|
});
|
|
295
298
|
});
|
|
296
299
|
};
|
|
@@ -39,6 +39,10 @@ export type UseStores<TKey extends StoreKey = StoreKey, T extends StoreData = St
|
|
|
39
39
|
};
|
|
40
40
|
export type CreateStoresOptions<TKey extends StoreKey = StoreKey, T extends StoreData = StoreData> = InitStoreOptions<T> & {
|
|
41
41
|
onBeforeChangeKey?: (nextKey: TKey, prevKey: TKey) => void;
|
|
42
|
+
/**
|
|
43
|
+
* Will be triggered when a single store with a specific key was initialized.
|
|
44
|
+
*/
|
|
45
|
+
onStoreInitialized?: (key: TKey, keyHash: string) => void;
|
|
42
46
|
defaultDeps?: SelectDeps<T>;
|
|
43
47
|
hashKeyFn?: (obj: TKey) => string;
|
|
44
48
|
};
|
|
@@ -5,13 +5,14 @@ const hooks_1 = require("preact/hooks");
|
|
|
5
5
|
const utils_1 = require("../utils");
|
|
6
6
|
const vanilla_1 = require("../vanilla");
|
|
7
7
|
const createStores = (initializer, options = {}) => {
|
|
8
|
-
const { onBeforeChangeKey = utils_1.noop, defaultDeps, hashKeyFn = utils_1.hashStoreKey } = options;
|
|
8
|
+
const { onBeforeChangeKey = utils_1.noop, onStoreInitialized = utils_1.noop, defaultDeps, hashKeyFn = utils_1.hashStoreKey, } = options;
|
|
9
9
|
const stores = new Map();
|
|
10
10
|
const getStore = (_key) => {
|
|
11
11
|
const key = _key || {};
|
|
12
12
|
const keyHash = hashKeyFn(key);
|
|
13
13
|
if (!stores.has(keyHash)) {
|
|
14
14
|
stores.set(keyHash, (0, vanilla_1.initStore)((api) => initializer({ key, keyHash, ...api }), options));
|
|
15
|
+
onStoreInitialized(key, keyHash);
|
|
15
16
|
}
|
|
16
17
|
return stores.get(keyHash);
|
|
17
18
|
};
|
|
@@ -103,7 +103,7 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
|
|
|
103
103
|
isError: false;
|
|
104
104
|
data: TData;
|
|
105
105
|
response: TResponse;
|
|
106
|
-
responseUpdatedAt: number;
|
|
106
|
+
responseUpdatedAt: number | null;
|
|
107
107
|
} | {
|
|
108
108
|
status: 'error';
|
|
109
109
|
isLoading: false;
|
|
@@ -186,6 +186,7 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
|
|
|
186
186
|
setInitialResponse: (options: {
|
|
187
187
|
key?: TKey;
|
|
188
188
|
response: TResponse;
|
|
189
|
+
skipRevalidation?: boolean;
|
|
189
190
|
}) => void;
|
|
190
191
|
/**
|
|
191
192
|
* Set query state (data, error, etc) to initial state.
|
|
@@ -87,7 +87,7 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
87
87
|
responseAllPages.push(response);
|
|
88
88
|
const newPageParam = getNextPageParam(response, responseAllPages.length);
|
|
89
89
|
newPageParams.push(newPageParam);
|
|
90
|
-
if (newPageParam
|
|
90
|
+
if ((0, utils_1.hasValue)(newPageParam) && newPageParams.length < pageParams.length) {
|
|
91
91
|
pageParam = newPageParam;
|
|
92
92
|
callQuery();
|
|
93
93
|
return;
|
|
@@ -112,7 +112,7 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
112
112
|
retryCount: 0,
|
|
113
113
|
pageParam: newPageParam,
|
|
114
114
|
pageParams: newPageParams,
|
|
115
|
-
hasNextPage: newPageParam
|
|
115
|
+
hasNextPage: (0, utils_1.hasValue)(newPageParam),
|
|
116
116
|
});
|
|
117
117
|
onSuccess(response, stateBeforeCallQuery);
|
|
118
118
|
})
|
|
@@ -134,7 +134,7 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
134
134
|
errorUpdatedAt,
|
|
135
135
|
isGoingToRetry: shouldRetry,
|
|
136
136
|
pageParam,
|
|
137
|
-
hasNextPage: pageParam
|
|
137
|
+
hasNextPage: (0, utils_1.hasValue)(pageParam),
|
|
138
138
|
}
|
|
139
139
|
: {
|
|
140
140
|
isWaiting: false,
|
|
@@ -145,7 +145,7 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
145
145
|
errorUpdatedAt,
|
|
146
146
|
isGoingToRetry: shouldRetry,
|
|
147
147
|
pageParam,
|
|
148
|
-
hasNextPage: pageParam
|
|
148
|
+
hasNextPage: (0, utils_1.hasValue)(pageParam),
|
|
149
149
|
});
|
|
150
150
|
if (shouldRetry) {
|
|
151
151
|
retryTimeoutId.set(keyHash, window.setTimeout(() => {
|
|
@@ -169,6 +169,9 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
169
169
|
forceFetch();
|
|
170
170
|
};
|
|
171
171
|
const fetchNextPage = () => {
|
|
172
|
+
if (typeof options.getNextPageParam !== 'function') {
|
|
173
|
+
return console.warn('fetchNextPage with invalid getNextPageParam option');
|
|
174
|
+
}
|
|
172
175
|
const state = get();
|
|
173
176
|
const { isLoading, isWaitingNextPage, data, hasNextPage, pageParam, pageParams } = state;
|
|
174
177
|
if (isLoading)
|
|
@@ -187,7 +190,7 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
187
190
|
data: select(response, { key, data }),
|
|
188
191
|
pageParam: newPageParam,
|
|
189
192
|
pageParams: pageParams.concat(newPageParam),
|
|
190
|
-
hasNextPage: newPageParam
|
|
193
|
+
hasNextPage: (0, utils_1.hasValue)(newPageParam),
|
|
191
194
|
});
|
|
192
195
|
})
|
|
193
196
|
.catch((error) => {
|
|
@@ -274,7 +277,7 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
274
277
|
},
|
|
275
278
|
};
|
|
276
279
|
})());
|
|
277
|
-
useQuery.setInitialResponse = ({ key, response }) => {
|
|
280
|
+
useQuery.setInitialResponse = ({ key, response, skipRevalidation }) => {
|
|
278
281
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
279
282
|
(0, react_1.useState)(() => {
|
|
280
283
|
if (response === undefined || useQuery.get(key).data)
|
|
@@ -286,11 +289,11 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
286
289
|
isSuccess: true,
|
|
287
290
|
isError: false,
|
|
288
291
|
response,
|
|
289
|
-
responseUpdatedAt: Date.now(),
|
|
292
|
+
responseUpdatedAt: skipRevalidation ? Date.now() : null,
|
|
290
293
|
data: select(response, { key: key, data: null }),
|
|
291
294
|
pageParam: newPageParam,
|
|
292
295
|
pageParams: [undefined, newPageParam],
|
|
293
|
-
hasNextPage: newPageParam
|
|
296
|
+
hasNextPage: (0, utils_1.hasValue)(newPageParam),
|
|
294
297
|
});
|
|
295
298
|
});
|
|
296
299
|
};
|
|
@@ -39,6 +39,10 @@ export type UseStores<TKey extends StoreKey = StoreKey, T extends StoreData = St
|
|
|
39
39
|
};
|
|
40
40
|
export type CreateStoresOptions<TKey extends StoreKey = StoreKey, T extends StoreData = StoreData> = InitStoreOptions<T> & {
|
|
41
41
|
onBeforeChangeKey?: (nextKey: TKey, prevKey: TKey) => void;
|
|
42
|
+
/**
|
|
43
|
+
* Will be triggered when a single store with a specific key was initialized.
|
|
44
|
+
*/
|
|
45
|
+
onStoreInitialized?: (key: TKey, keyHash: string) => void;
|
|
42
46
|
defaultDeps?: SelectDeps<T>;
|
|
43
47
|
hashKeyFn?: (obj: TKey) => string;
|
|
44
48
|
};
|
|
@@ -5,13 +5,14 @@ const react_1 = require("react");
|
|
|
5
5
|
const utils_1 = require("../utils");
|
|
6
6
|
const vanilla_1 = require("../vanilla");
|
|
7
7
|
const createStores = (initializer, options = {}) => {
|
|
8
|
-
const { onBeforeChangeKey = utils_1.noop, defaultDeps, hashKeyFn = utils_1.hashStoreKey } = options;
|
|
8
|
+
const { onBeforeChangeKey = utils_1.noop, onStoreInitialized = utils_1.noop, defaultDeps, hashKeyFn = utils_1.hashStoreKey, } = options;
|
|
9
9
|
const stores = new Map();
|
|
10
10
|
const getStore = (_key) => {
|
|
11
11
|
const key = _key || {};
|
|
12
12
|
const keyHash = hashKeyFn(key);
|
|
13
13
|
if (!stores.has(keyHash)) {
|
|
14
14
|
stores.set(keyHash, (0, vanilla_1.initStore)((api) => initializer({ key, keyHash, ...api }), options));
|
|
15
|
+
onStoreInitialized(key, keyHash);
|
|
15
16
|
}
|
|
16
17
|
return stores.get(keyHash);
|
|
17
18
|
};
|
package/lib/utils/index.d.ts
CHANGED
package/lib/utils/index.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.hashStoreKey = exports.identityFn = exports.noop = void 0;
|
|
3
|
+
exports.hashStoreKey = exports.hasValue = exports.identityFn = exports.noop = void 0;
|
|
4
4
|
const noop = () => { };
|
|
5
5
|
exports.noop = noop;
|
|
6
|
-
const identityFn = (
|
|
6
|
+
const identityFn = (value) => value;
|
|
7
7
|
exports.identityFn = identityFn;
|
|
8
|
+
const hasValue = (value) => value !== undefined && value !== null;
|
|
9
|
+
exports.hasValue = hasValue;
|
|
8
10
|
const hashStoreKey = (obj) => JSON.stringify(obj, Object.keys(obj).sort());
|
|
9
11
|
exports.hashStoreKey = hashStoreKey;
|