yuustate 3.10.2 → 3.11.1
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-query.d.mts +43 -3
- package/esm/react.mjs +40 -9
- package/package.json +1 -1
- package/react/create-query.d.ts +43 -3
- package/react.js +40 -9
|
@@ -72,7 +72,11 @@ type Internal<TData, TError> = {
|
|
|
72
72
|
garbageCollectionTimeoutId?: number;
|
|
73
73
|
rollbackData?: TData | undefined;
|
|
74
74
|
};
|
|
75
|
-
type AdditionalStoreApi<TData, TError> = {
|
|
75
|
+
type AdditionalStoreApi<TData, TError, TVariable extends StoreKey = never> = {
|
|
76
|
+
/**
|
|
77
|
+
* The original query variable used to create this store.
|
|
78
|
+
*/
|
|
79
|
+
variable: TVariable;
|
|
76
80
|
/**
|
|
77
81
|
* A deterministic hash string derived from the query variable.
|
|
78
82
|
*
|
|
@@ -205,7 +209,7 @@ type AdditionalStoreApi<TData, TError> = {
|
|
|
205
209
|
* @remarks
|
|
206
210
|
* Controls caching, retry behavior, lifecycle, and side effects of an async operation.
|
|
207
211
|
*/
|
|
208
|
-
export type QueryOptions<TData, TVariable extends StoreKey, TError = Error> = InitStoreOptions<QueryState<TData, TError>, AdditionalStoreApi<TData, TError>> & {
|
|
212
|
+
export type QueryOptions<TData, TVariable extends StoreKey, TError = Error> = InitStoreOptions<QueryState<TData, TError>, AdditionalStoreApi<TData, TError, TVariable>> & {
|
|
209
213
|
/**
|
|
210
214
|
* Time (in milliseconds) that data is considered fresh.
|
|
211
215
|
*
|
|
@@ -357,6 +361,10 @@ export declare const createQuery: <TData, TVariable extends StoreKey = never, TE
|
|
|
357
361
|
getState: () => QueryState<TData, TError>;
|
|
358
362
|
subscribe: (subscriber: import("../vanilla.d.mts").Subscriber<QueryState<TData, TError>>) => () => void;
|
|
359
363
|
getSubscriberCount: () => number;
|
|
364
|
+
/**
|
|
365
|
+
* The original query variable used to create this store.
|
|
366
|
+
*/
|
|
367
|
+
variable: TVariable;
|
|
360
368
|
/**
|
|
361
369
|
* A deterministic hash string derived from the query variable.
|
|
362
370
|
*
|
|
@@ -490,6 +498,15 @@ export declare const createQuery: <TData, TVariable extends StoreKey = never, TE
|
|
|
490
498
|
* - Useful for bulk refetching.
|
|
491
499
|
*/
|
|
492
500
|
executeAll: (options?: {
|
|
501
|
+
/**
|
|
502
|
+
* Optional predicate to select which query to execute.
|
|
503
|
+
*/
|
|
504
|
+
filter?: (variable: TVariable) => boolean;
|
|
505
|
+
/**
|
|
506
|
+
* Whether to start a new execution instead of reusing an ongoing one.
|
|
507
|
+
*
|
|
508
|
+
* @default true
|
|
509
|
+
*/
|
|
493
510
|
overwriteOngoingExecution?: boolean;
|
|
494
511
|
}) => void;
|
|
495
512
|
/**
|
|
@@ -499,6 +516,15 @@ export declare const createQuery: <TData, TVariable extends StoreKey = never, TE
|
|
|
499
516
|
* - Only re-fetches stale queries.
|
|
500
517
|
*/
|
|
501
518
|
revalidateAll: (options?: {
|
|
519
|
+
/**
|
|
520
|
+
* Optional predicate to select which query to revalidate.
|
|
521
|
+
*/
|
|
522
|
+
filter?: (variable: TVariable) => boolean;
|
|
523
|
+
/**
|
|
524
|
+
* Whether to start a new execution instead of reusing an ongoing one.
|
|
525
|
+
*
|
|
526
|
+
* @default true
|
|
527
|
+
*/
|
|
502
528
|
overwriteOngoingExecution?: boolean;
|
|
503
529
|
}) => void;
|
|
504
530
|
/**
|
|
@@ -509,11 +535,25 @@ export declare const createQuery: <TData, TVariable extends StoreKey = never, TE
|
|
|
509
535
|
* - Invalidated queries bypass `staleTime` until successfully executed again.
|
|
510
536
|
*/
|
|
511
537
|
invalidateAll: (options?: {
|
|
538
|
+
/**
|
|
539
|
+
* Optional predicate to select which query to invalidate.
|
|
540
|
+
*/
|
|
541
|
+
filter?: (variable: TVariable) => boolean;
|
|
542
|
+
/**
|
|
543
|
+
* Whether to start a new execution instead of reusing an ongoing one.
|
|
544
|
+
*
|
|
545
|
+
* @default true
|
|
546
|
+
*/
|
|
512
547
|
overwriteOngoingExecution?: boolean;
|
|
513
548
|
}) => void;
|
|
514
549
|
/**
|
|
515
550
|
* Resets all query instances.
|
|
516
551
|
*/
|
|
517
|
-
resetAll: (
|
|
552
|
+
resetAll: (options?: {
|
|
553
|
+
/**
|
|
554
|
+
* Optional predicate to select which query to reset.
|
|
555
|
+
*/
|
|
556
|
+
filter?: (variable: TVariable) => boolean;
|
|
557
|
+
}) => void;
|
|
518
558
|
};
|
|
519
559
|
export {};
|
package/esm/react.mjs
CHANGED
|
@@ -3,7 +3,15 @@ import { isClient, initStore, getHash, noop } from 'yuustate/vanilla';
|
|
|
3
3
|
|
|
4
4
|
const useIsomorphicLayoutEffect = isClient ? useLayoutEffect : useEffect;
|
|
5
5
|
|
|
6
|
-
const
|
|
6
|
+
const DATE_METHOD_REGEX = /^(?:to|get)[A-Z]/;
|
|
7
|
+
const getValueByPath = (obj, path) => {
|
|
8
|
+
return path.reduce((acc, key) => {
|
|
9
|
+
if (acc instanceof Date && DATE_METHOD_REGEX.test(key)) {
|
|
10
|
+
return acc[key]();
|
|
11
|
+
}
|
|
12
|
+
return acc == null ? void 0 : acc[key];
|
|
13
|
+
}, obj);
|
|
14
|
+
};
|
|
7
15
|
const isPrefixPath = (candidatePrefix, targetPath) => {
|
|
8
16
|
if (candidatePrefix.length >= targetPath.length) return false;
|
|
9
17
|
for (let i = 0; i < candidatePrefix.length; i++) {
|
|
@@ -39,6 +47,9 @@ const useStoreStateProxy = (storeState) => {
|
|
|
39
47
|
const newPath = [...path, key];
|
|
40
48
|
track(newPath);
|
|
41
49
|
const value = obj[key];
|
|
50
|
+
if (target instanceof Date && typeof value === "function" && DATE_METHOD_REGEX.test(key)) {
|
|
51
|
+
return value.bind(target);
|
|
52
|
+
}
|
|
42
53
|
return createDeepProxy(value, newPath);
|
|
43
54
|
}
|
|
44
55
|
});
|
|
@@ -118,6 +129,7 @@ const createStores = (initialState, options) => {
|
|
|
118
129
|
let store;
|
|
119
130
|
if (stores.has(keyHash)) {
|
|
120
131
|
store = stores.get(keyHash);
|
|
132
|
+
store.key = key;
|
|
121
133
|
} else {
|
|
122
134
|
store = initStore(
|
|
123
135
|
initialState,
|
|
@@ -417,12 +429,14 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
417
429
|
let store;
|
|
418
430
|
if (stores.has(variableHash)) {
|
|
419
431
|
store = stores.get(variableHash);
|
|
432
|
+
store.variable = variable;
|
|
420
433
|
} else {
|
|
421
434
|
store = initStore(
|
|
422
435
|
initialState,
|
|
423
436
|
configureStoreEvents(variableHash)
|
|
424
437
|
// Intentionally using as any: don't want to add generic on `initStore`
|
|
425
438
|
);
|
|
439
|
+
store.variable = variable;
|
|
426
440
|
store.variableHash = variableHash;
|
|
427
441
|
stores.set(variableHash, store);
|
|
428
442
|
const apis = getApis(store, variable);
|
|
@@ -522,6 +536,7 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
522
536
|
}
|
|
523
537
|
});
|
|
524
538
|
};
|
|
539
|
+
const shouldApplyToStore = (store, filter) => filter ? filter(store.variable) : true;
|
|
525
540
|
return Object.assign(getStore, {
|
|
526
541
|
/**
|
|
527
542
|
* Executes all query instances.
|
|
@@ -529,8 +544,12 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
529
544
|
* @remarks
|
|
530
545
|
* - Useful for bulk refetching.
|
|
531
546
|
*/
|
|
532
|
-
executeAll: (options2) => {
|
|
533
|
-
|
|
547
|
+
executeAll: (options2 = {}) => {
|
|
548
|
+
const { filter, ...restOptions } = options2;
|
|
549
|
+
stores.forEach((store) => {
|
|
550
|
+
if (!shouldApplyToStore(store, filter)) return;
|
|
551
|
+
store.execute(restOptions);
|
|
552
|
+
});
|
|
534
553
|
},
|
|
535
554
|
/**
|
|
536
555
|
* Revalidates all query instances.
|
|
@@ -538,8 +557,12 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
538
557
|
* @remarks
|
|
539
558
|
* - Only re-fetches stale queries.
|
|
540
559
|
*/
|
|
541
|
-
revalidateAll: (options2) => {
|
|
542
|
-
|
|
560
|
+
revalidateAll: (options2 = {}) => {
|
|
561
|
+
const { filter, ...restOptions } = options2;
|
|
562
|
+
stores.forEach((store) => {
|
|
563
|
+
if (!shouldApplyToStore(store, filter)) return;
|
|
564
|
+
store.revalidate(restOptions);
|
|
565
|
+
});
|
|
543
566
|
},
|
|
544
567
|
/**
|
|
545
568
|
* Invalidates all query instances.
|
|
@@ -548,14 +571,22 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
548
571
|
* - Marks all queries as invalidated and triggers revalidation if active.
|
|
549
572
|
* - Invalidated queries bypass `staleTime` until successfully executed again.
|
|
550
573
|
*/
|
|
551
|
-
invalidateAll: (options2) => {
|
|
552
|
-
|
|
574
|
+
invalidateAll: (options2 = {}) => {
|
|
575
|
+
const { filter, ...restOptions } = options2;
|
|
576
|
+
stores.forEach((store) => {
|
|
577
|
+
if (!shouldApplyToStore(store, filter)) return;
|
|
578
|
+
store.invalidate(restOptions);
|
|
579
|
+
});
|
|
553
580
|
},
|
|
554
581
|
/**
|
|
555
582
|
* Resets all query instances.
|
|
556
583
|
*/
|
|
557
|
-
resetAll: () => {
|
|
558
|
-
|
|
584
|
+
resetAll: (options2 = {}) => {
|
|
585
|
+
const { filter } = options2;
|
|
586
|
+
stores.forEach((store) => {
|
|
587
|
+
if (!shouldApplyToStore(store, filter)) return;
|
|
588
|
+
store.reset();
|
|
589
|
+
});
|
|
559
590
|
}
|
|
560
591
|
});
|
|
561
592
|
};
|
package/package.json
CHANGED
package/react/create-query.d.ts
CHANGED
|
@@ -72,7 +72,11 @@ type Internal<TData, TError> = {
|
|
|
72
72
|
garbageCollectionTimeoutId?: number;
|
|
73
73
|
rollbackData?: TData | undefined;
|
|
74
74
|
};
|
|
75
|
-
type AdditionalStoreApi<TData, TError> = {
|
|
75
|
+
type AdditionalStoreApi<TData, TError, TVariable extends StoreKey = never> = {
|
|
76
|
+
/**
|
|
77
|
+
* The original query variable used to create this store.
|
|
78
|
+
*/
|
|
79
|
+
variable: TVariable;
|
|
76
80
|
/**
|
|
77
81
|
* A deterministic hash string derived from the query variable.
|
|
78
82
|
*
|
|
@@ -205,7 +209,7 @@ type AdditionalStoreApi<TData, TError> = {
|
|
|
205
209
|
* @remarks
|
|
206
210
|
* Controls caching, retry behavior, lifecycle, and side effects of an async operation.
|
|
207
211
|
*/
|
|
208
|
-
export type QueryOptions<TData, TVariable extends StoreKey, TError = Error> = InitStoreOptions<QueryState<TData, TError>, AdditionalStoreApi<TData, TError>> & {
|
|
212
|
+
export type QueryOptions<TData, TVariable extends StoreKey, TError = Error> = InitStoreOptions<QueryState<TData, TError>, AdditionalStoreApi<TData, TError, TVariable>> & {
|
|
209
213
|
/**
|
|
210
214
|
* Time (in milliseconds) that data is considered fresh.
|
|
211
215
|
*
|
|
@@ -357,6 +361,10 @@ export declare const createQuery: <TData, TVariable extends StoreKey = never, TE
|
|
|
357
361
|
getState: () => QueryState<TData, TError>;
|
|
358
362
|
subscribe: (subscriber: import("../vanilla.ts").Subscriber<QueryState<TData, TError>>) => () => void;
|
|
359
363
|
getSubscriberCount: () => number;
|
|
364
|
+
/**
|
|
365
|
+
* The original query variable used to create this store.
|
|
366
|
+
*/
|
|
367
|
+
variable: TVariable;
|
|
360
368
|
/**
|
|
361
369
|
* A deterministic hash string derived from the query variable.
|
|
362
370
|
*
|
|
@@ -490,6 +498,15 @@ export declare const createQuery: <TData, TVariable extends StoreKey = never, TE
|
|
|
490
498
|
* - Useful for bulk refetching.
|
|
491
499
|
*/
|
|
492
500
|
executeAll: (options?: {
|
|
501
|
+
/**
|
|
502
|
+
* Optional predicate to select which query to execute.
|
|
503
|
+
*/
|
|
504
|
+
filter?: (variable: TVariable) => boolean;
|
|
505
|
+
/**
|
|
506
|
+
* Whether to start a new execution instead of reusing an ongoing one.
|
|
507
|
+
*
|
|
508
|
+
* @default true
|
|
509
|
+
*/
|
|
493
510
|
overwriteOngoingExecution?: boolean;
|
|
494
511
|
}) => void;
|
|
495
512
|
/**
|
|
@@ -499,6 +516,15 @@ export declare const createQuery: <TData, TVariable extends StoreKey = never, TE
|
|
|
499
516
|
* - Only re-fetches stale queries.
|
|
500
517
|
*/
|
|
501
518
|
revalidateAll: (options?: {
|
|
519
|
+
/**
|
|
520
|
+
* Optional predicate to select which query to revalidate.
|
|
521
|
+
*/
|
|
522
|
+
filter?: (variable: TVariable) => boolean;
|
|
523
|
+
/**
|
|
524
|
+
* Whether to start a new execution instead of reusing an ongoing one.
|
|
525
|
+
*
|
|
526
|
+
* @default true
|
|
527
|
+
*/
|
|
502
528
|
overwriteOngoingExecution?: boolean;
|
|
503
529
|
}) => void;
|
|
504
530
|
/**
|
|
@@ -509,11 +535,25 @@ export declare const createQuery: <TData, TVariable extends StoreKey = never, TE
|
|
|
509
535
|
* - Invalidated queries bypass `staleTime` until successfully executed again.
|
|
510
536
|
*/
|
|
511
537
|
invalidateAll: (options?: {
|
|
538
|
+
/**
|
|
539
|
+
* Optional predicate to select which query to invalidate.
|
|
540
|
+
*/
|
|
541
|
+
filter?: (variable: TVariable) => boolean;
|
|
542
|
+
/**
|
|
543
|
+
* Whether to start a new execution instead of reusing an ongoing one.
|
|
544
|
+
*
|
|
545
|
+
* @default true
|
|
546
|
+
*/
|
|
512
547
|
overwriteOngoingExecution?: boolean;
|
|
513
548
|
}) => void;
|
|
514
549
|
/**
|
|
515
550
|
* Resets all query instances.
|
|
516
551
|
*/
|
|
517
|
-
resetAll: (
|
|
552
|
+
resetAll: (options?: {
|
|
553
|
+
/**
|
|
554
|
+
* Optional predicate to select which query to reset.
|
|
555
|
+
*/
|
|
556
|
+
filter?: (variable: TVariable) => boolean;
|
|
557
|
+
}) => void;
|
|
518
558
|
};
|
|
519
559
|
export {};
|
package/react.js
CHANGED
|
@@ -5,7 +5,15 @@ var vanilla = require('yuustate/vanilla');
|
|
|
5
5
|
|
|
6
6
|
const useIsomorphicLayoutEffect = vanilla.isClient ? react.useLayoutEffect : react.useEffect;
|
|
7
7
|
|
|
8
|
-
const
|
|
8
|
+
const DATE_METHOD_REGEX = /^(?:to|get)[A-Z]/;
|
|
9
|
+
const getValueByPath = (obj, path) => {
|
|
10
|
+
return path.reduce((acc, key) => {
|
|
11
|
+
if (acc instanceof Date && DATE_METHOD_REGEX.test(key)) {
|
|
12
|
+
return acc[key]();
|
|
13
|
+
}
|
|
14
|
+
return acc == null ? void 0 : acc[key];
|
|
15
|
+
}, obj);
|
|
16
|
+
};
|
|
9
17
|
const isPrefixPath = (candidatePrefix, targetPath) => {
|
|
10
18
|
if (candidatePrefix.length >= targetPath.length) return false;
|
|
11
19
|
for (let i = 0; i < candidatePrefix.length; i++) {
|
|
@@ -41,6 +49,9 @@ const useStoreStateProxy = (storeState) => {
|
|
|
41
49
|
const newPath = [...path, key];
|
|
42
50
|
track(newPath);
|
|
43
51
|
const value = obj[key];
|
|
52
|
+
if (target instanceof Date && typeof value === "function" && DATE_METHOD_REGEX.test(key)) {
|
|
53
|
+
return value.bind(target);
|
|
54
|
+
}
|
|
44
55
|
return createDeepProxy(value, newPath);
|
|
45
56
|
}
|
|
46
57
|
});
|
|
@@ -120,6 +131,7 @@ const createStores = (initialState, options) => {
|
|
|
120
131
|
let store;
|
|
121
132
|
if (stores.has(keyHash)) {
|
|
122
133
|
store = stores.get(keyHash);
|
|
134
|
+
store.key = key;
|
|
123
135
|
} else {
|
|
124
136
|
store = vanilla.initStore(
|
|
125
137
|
initialState,
|
|
@@ -419,12 +431,14 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
419
431
|
let store;
|
|
420
432
|
if (stores.has(variableHash)) {
|
|
421
433
|
store = stores.get(variableHash);
|
|
434
|
+
store.variable = variable;
|
|
422
435
|
} else {
|
|
423
436
|
store = vanilla.initStore(
|
|
424
437
|
initialState,
|
|
425
438
|
configureStoreEvents(variableHash)
|
|
426
439
|
// Intentionally using as any: don't want to add generic on `initStore`
|
|
427
440
|
);
|
|
441
|
+
store.variable = variable;
|
|
428
442
|
store.variableHash = variableHash;
|
|
429
443
|
stores.set(variableHash, store);
|
|
430
444
|
const apis = getApis(store, variable);
|
|
@@ -524,6 +538,7 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
524
538
|
}
|
|
525
539
|
});
|
|
526
540
|
};
|
|
541
|
+
const shouldApplyToStore = (store, filter) => filter ? filter(store.variable) : true;
|
|
527
542
|
return Object.assign(getStore, {
|
|
528
543
|
/**
|
|
529
544
|
* Executes all query instances.
|
|
@@ -531,8 +546,12 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
531
546
|
* @remarks
|
|
532
547
|
* - Useful for bulk refetching.
|
|
533
548
|
*/
|
|
534
|
-
executeAll: (options2) => {
|
|
535
|
-
|
|
549
|
+
executeAll: (options2 = {}) => {
|
|
550
|
+
const { filter, ...restOptions } = options2;
|
|
551
|
+
stores.forEach((store) => {
|
|
552
|
+
if (!shouldApplyToStore(store, filter)) return;
|
|
553
|
+
store.execute(restOptions);
|
|
554
|
+
});
|
|
536
555
|
},
|
|
537
556
|
/**
|
|
538
557
|
* Revalidates all query instances.
|
|
@@ -540,8 +559,12 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
540
559
|
* @remarks
|
|
541
560
|
* - Only re-fetches stale queries.
|
|
542
561
|
*/
|
|
543
|
-
revalidateAll: (options2) => {
|
|
544
|
-
|
|
562
|
+
revalidateAll: (options2 = {}) => {
|
|
563
|
+
const { filter, ...restOptions } = options2;
|
|
564
|
+
stores.forEach((store) => {
|
|
565
|
+
if (!shouldApplyToStore(store, filter)) return;
|
|
566
|
+
store.revalidate(restOptions);
|
|
567
|
+
});
|
|
545
568
|
},
|
|
546
569
|
/**
|
|
547
570
|
* Invalidates all query instances.
|
|
@@ -550,14 +573,22 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
550
573
|
* - Marks all queries as invalidated and triggers revalidation if active.
|
|
551
574
|
* - Invalidated queries bypass `staleTime` until successfully executed again.
|
|
552
575
|
*/
|
|
553
|
-
invalidateAll: (options2) => {
|
|
554
|
-
|
|
576
|
+
invalidateAll: (options2 = {}) => {
|
|
577
|
+
const { filter, ...restOptions } = options2;
|
|
578
|
+
stores.forEach((store) => {
|
|
579
|
+
if (!shouldApplyToStore(store, filter)) return;
|
|
580
|
+
store.invalidate(restOptions);
|
|
581
|
+
});
|
|
555
582
|
},
|
|
556
583
|
/**
|
|
557
584
|
* Resets all query instances.
|
|
558
585
|
*/
|
|
559
|
-
resetAll: () => {
|
|
560
|
-
|
|
586
|
+
resetAll: (options2 = {}) => {
|
|
587
|
+
const { filter } = options2;
|
|
588
|
+
stores.forEach((store) => {
|
|
589
|
+
if (!shouldApplyToStore(store, filter)) return;
|
|
590
|
+
store.reset();
|
|
591
|
+
});
|
|
561
592
|
}
|
|
562
593
|
});
|
|
563
594
|
};
|