@povio/openapi-codegen-cli 2.0.7 → 2.0.8-rc.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/dist/generator.js +35 -35
- package/dist/generators/const/deps.const.d.ts +1 -0
- package/dist/lib/config/queryConfig.context.d.ts +4 -1
- package/dist/lib/config/queryConfig.context.mjs +8 -2
- package/dist/sh.js +121 -121
- package/package.json +1 -1
- package/src/assets/useCrossTabQueryInvalidation.ts +40 -0
- package/src/assets/useMutationEffects.ts +27 -3
package/package.json
CHANGED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { QueryClient, QueryKey } from "@tanstack/react-query";
|
|
2
|
+
|
|
3
|
+
const CROSS_TAB_INVALIDATE_KEY = "__rq_invalidate__";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Broadcasts a query invalidation event to all other open tabs via localStorage.
|
|
7
|
+
*
|
|
8
|
+
* @param queryKeys - An array of query keys to invalidate (array of arrays).
|
|
9
|
+
*
|
|
10
|
+
* NOTE: The `storage` event only fires in *other* tabs — the calling tab
|
|
11
|
+
* must invalidate its own queryClient separately if needed.
|
|
12
|
+
*/
|
|
13
|
+
export const broadcastQueryInvalidation = (queryKeys: QueryKey[]) => {
|
|
14
|
+
localStorage.setItem(CROSS_TAB_INVALIDATE_KEY, JSON.stringify({ keys: queryKeys, timestamp: Date.now() }));
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Registers a one-time global `storage` event listener that reacts to
|
|
19
|
+
* cross-tab invalidation broadcasts. Safe to call from multiple hooks —
|
|
20
|
+
* only the first call sets up the listener.
|
|
21
|
+
*/
|
|
22
|
+
let isListenerSetUp = false;
|
|
23
|
+
|
|
24
|
+
export const setupCrossTabListener = (queryClient: QueryClient) => {
|
|
25
|
+
if (isListenerSetUp) return;
|
|
26
|
+
isListenerSetUp = true;
|
|
27
|
+
|
|
28
|
+
window.addEventListener("storage", (e: StorageEvent) => {
|
|
29
|
+
if (e.key !== CROSS_TAB_INVALIDATE_KEY || !e.newValue) return;
|
|
30
|
+
|
|
31
|
+
try {
|
|
32
|
+
const { keys } = JSON.parse(e.newValue) as { keys: QueryKey[] };
|
|
33
|
+
for (const queryKey of keys) {
|
|
34
|
+
queryClient.invalidateQueries({ queryKey });
|
|
35
|
+
}
|
|
36
|
+
} catch {
|
|
37
|
+
// Ignore malformed payloads
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
};
|
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
import { useCallback } from "react";
|
|
1
|
+
import { useCallback, useEffect } from "react";
|
|
2
2
|
|
|
3
3
|
import { OpenApiQueryConfig } from "@povio/openapi-codegen-cli";
|
|
4
4
|
import { QueryKey, useQueryClient } from "@tanstack/react-query";
|
|
5
5
|
|
|
6
6
|
import { QueryModule } from "./queryModules";
|
|
7
|
+
import { broadcastQueryInvalidation, setupCrossTabListener } from "./useCrossTabQueryInvalidation";
|
|
7
8
|
|
|
8
9
|
export interface MutationEffectsOptions {
|
|
9
10
|
invalidateCurrentModule?: boolean;
|
|
11
|
+
crossTabInvalidation?: boolean;
|
|
12
|
+
invalidationMap?: Record<string, (context: Record<string, string>) => QueryKey[]>;
|
|
10
13
|
invalidateModules?: QueryModule[];
|
|
11
14
|
invalidateKeys?: QueryKey[];
|
|
12
15
|
preferUpdate?: boolean;
|
|
@@ -20,6 +23,11 @@ export function useMutationEffects({ currentModule }: UseMutationEffectsProps) {
|
|
|
20
23
|
const queryClient = useQueryClient();
|
|
21
24
|
const config = OpenApiQueryConfig.useConfig();
|
|
22
25
|
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
if (!config.crossTabInvalidation) return;
|
|
28
|
+
setupCrossTabListener(queryClient);
|
|
29
|
+
}, [queryClient, config.crossTabInvalidation]);
|
|
30
|
+
|
|
23
31
|
const runMutationEffects = useCallback(
|
|
24
32
|
async <TData>(data: TData, options: MutationEffectsOptions = {}, updateKeys?: QueryKey[]) => {
|
|
25
33
|
const { invalidateCurrentModule = true, invalidateModules, invalidateKeys, preferUpdate } = options;
|
|
@@ -30,6 +38,8 @@ export function useMutationEffects({ currentModule }: UseMutationEffectsProps) {
|
|
|
30
38
|
const isQueryKeyEqual = (keyA: QueryKey, keyB: QueryKey) =>
|
|
31
39
|
keyA.length === keyB.length && keyA.every((item, index) => item === keyB[index]);
|
|
32
40
|
|
|
41
|
+
const invalidatedQueryKeys: QueryKey[] = [];
|
|
42
|
+
|
|
33
43
|
queryClient.invalidateQueries({
|
|
34
44
|
predicate: ({ queryKey }) => {
|
|
35
45
|
const isUpdateKey = updateKeys?.some((key) => isQueryKeyEqual(queryKey, key));
|
|
@@ -40,15 +50,29 @@ export function useMutationEffects({ currentModule }: UseMutationEffectsProps) {
|
|
|
40
50
|
const isCurrentModule = shouldInvalidateCurrentModule && queryKey[0] === currentModule;
|
|
41
51
|
const isInvalidateModule = !!invalidateModules && invalidateModules.some((module) => queryKey[0] === module);
|
|
42
52
|
const isInvalidateKey = !!invalidateKeys && invalidateKeys.some((key) => isQueryKeyEqual(queryKey, key));
|
|
43
|
-
|
|
53
|
+
|
|
54
|
+
const map = config.invalidationMap?.[currentModule]?.(data);
|
|
55
|
+
const isMappedKey = !!map && map.some((key) => isQueryKeyEqual(queryKey, key));
|
|
56
|
+
|
|
57
|
+
const shouldInvalidate = isCurrentModule || isInvalidateModule || isInvalidateKey || isMappedKey;
|
|
58
|
+
|
|
59
|
+
if (shouldInvalidate && config.crossTabInvalidation) {
|
|
60
|
+
invalidatedQueryKeys.push([...queryKey]);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return shouldInvalidate;
|
|
44
64
|
},
|
|
45
65
|
});
|
|
46
66
|
|
|
67
|
+
if (config.crossTabInvalidation && invalidatedQueryKeys.length > 0) {
|
|
68
|
+
broadcastQueryInvalidation(invalidatedQueryKeys);
|
|
69
|
+
}
|
|
70
|
+
|
|
47
71
|
if (shouldUpdate && updateKeys) {
|
|
48
72
|
updateKeys.map((queryKey) => queryClient.setQueryData(queryKey, data));
|
|
49
73
|
}
|
|
50
74
|
},
|
|
51
|
-
[queryClient, currentModule, config.preferUpdate],
|
|
75
|
+
[queryClient, currentModule, config.preferUpdate, config.invalidationMap, config.crossTabInvalidation],
|
|
52
76
|
);
|
|
53
77
|
|
|
54
78
|
return { runMutationEffects };
|