@povio/openapi-codegen-cli 2.0.8-rc.1 → 2.0.8-rc.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.
@@ -0,0 +1,23 @@
1
+ const CROSS_TAB_INVALIDATE_KEY = "__rq_invalidate__";
2
+ const broadcastQueryInvalidation = (queryKeys) => {
3
+ localStorage.setItem(CROSS_TAB_INVALIDATE_KEY, JSON.stringify({ keys: queryKeys, timestamp: Date.now() }));
4
+ };
5
+ let isListenerSetUp = false;
6
+ const setupCrossTabListener = (queryClient) => {
7
+ if (isListenerSetUp) return;
8
+ isListenerSetUp = true;
9
+ window.addEventListener("storage", (e) => {
10
+ if (e.key !== CROSS_TAB_INVALIDATE_KEY || !e.newValue) return;
11
+ try {
12
+ const { keys } = JSON.parse(e.newValue);
13
+ for (const queryKey of keys) {
14
+ queryClient.invalidateQueries({ queryKey });
15
+ }
16
+ } catch {
17
+ }
18
+ });
19
+ };
20
+ export {
21
+ broadcastQueryInvalidation,
22
+ setupCrossTabListener
23
+ };
@@ -0,0 +1,56 @@
1
+ import { useEffect, useCallback } from "react";
2
+ import { useQueryClient } from "@tanstack/react-query";
3
+ import { OpenApiQueryConfig } from "../lib/config/queryConfig.context.mjs";
4
+ import { setupCrossTabListener, broadcastQueryInvalidation } from "./useCrossTabQueryInvalidation.mjs";
5
+ function useMutationEffects({ currentModule }) {
6
+ const queryClient = useQueryClient();
7
+ const config = OpenApiQueryConfig.useConfig();
8
+ useEffect(() => {
9
+ if (!config.crossTabInvalidation) return;
10
+ setupCrossTabListener(queryClient);
11
+ }, [queryClient, config.crossTabInvalidation]);
12
+ const runMutationEffects = useCallback(
13
+ async (data, variables, options = {}, updateKeys) => {
14
+ const {
15
+ invalidateCurrentModule = true,
16
+ invalidateModules,
17
+ invalidateKeys,
18
+ preferUpdate,
19
+ invalidationMap
20
+ } = options;
21
+ const shouldUpdate = preferUpdate || preferUpdate === void 0 && config.preferUpdate;
22
+ const shouldInvalidateCurrentModule = invalidateCurrentModule || invalidateCurrentModule === void 0 && config.invalidateCurrentModule;
23
+ const isQueryKeyEqual = (keyA, keyB) => keyA.length === keyB.length && keyA.every((item, index) => item === keyB[index]);
24
+ const invalidatedQueryKeys = [];
25
+ queryClient.invalidateQueries({
26
+ predicate: ({ queryKey }) => {
27
+ const isUpdateKey = updateKeys?.some((key) => isQueryKeyEqual(queryKey, key));
28
+ if (shouldUpdate && isUpdateKey) {
29
+ return false;
30
+ }
31
+ const isCurrentModule = shouldInvalidateCurrentModule && queryKey[0] === currentModule;
32
+ const isInvalidateModule = !!invalidateModules && invalidateModules.some((module) => queryKey[0] === module);
33
+ const isInvalidateKey = !!invalidateKeys && invalidateKeys.some((key) => isQueryKeyEqual(queryKey, key));
34
+ const map = (invalidationMap || config.invalidationMap)?.[currentModule]?.(data, variables);
35
+ const isMappedKey = !!map && map.some((key) => isQueryKeyEqual(queryKey, key));
36
+ const shouldInvalidate = isCurrentModule || isInvalidateModule || isInvalidateKey || isMappedKey;
37
+ if (shouldInvalidate && config.crossTabInvalidation) {
38
+ invalidatedQueryKeys.push([...queryKey]);
39
+ }
40
+ return shouldInvalidate;
41
+ }
42
+ });
43
+ if (config.crossTabInvalidation && invalidatedQueryKeys.length > 0) {
44
+ broadcastQueryInvalidation(invalidatedQueryKeys);
45
+ }
46
+ if (shouldUpdate && updateKeys) {
47
+ updateKeys.map((queryKey) => queryClient.setQueryData(queryKey, data));
48
+ }
49
+ },
50
+ [queryClient, currentModule, config.preferUpdate, config.invalidationMap, config.crossTabInvalidation]
51
+ );
52
+ return { runMutationEffects };
53
+ }
54
+ export {
55
+ useMutationEffects
56
+ };