Skip to content

Commit 74d8915

Browse files
committed
fix(dev-log): apply instance context log caching
Signed-off-by: piggggggggy <[email protected]>
1 parent 004b3d9 commit 74d8915

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

Diff for: apps/web/src/query/composables/use-scoped-query.ts

+38-10
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import type { MaybeRef } from '@vueuse/core';
3434
import { toValue } from '@vueuse/core';
3535
import type { ComputedRef } from 'vue';
36-
import { computed } from 'vue';
36+
import { computed, getCurrentInstance } from 'vue';
3737

3838
import {
3939
useQuery, type UseQueryOptions, type UseQueryReturnType,
@@ -104,8 +104,17 @@ const _warnMissingRequiredScopes = (scopes: GrantScope[]) => {
104104
}
105105
};
106106

107-
// Logs a warning once per queryKey when the current scope is invalid for this query
108-
const _warnedKeys = new Set<string>();
107+
/**
108+
* Logs a warning when a query is not executed due to invalid scope,
109+
* but only once per queryKey during development.
110+
*
111+
* Conditions to trigger warning:
112+
* - `enabled` is true
113+
* - app is ready (not loading)
114+
* - currentScope is defined
115+
* - currentScope NOT included in requiredScopes
116+
*/
117+
const _warnedKeysPerInstance = new WeakMap<object, Set<string>>();
109118
const _warnInvalidScopeOnce = (params: {
110119
queryKey: QueryKeyArray;
111120
enabled: boolean;
@@ -115,20 +124,39 @@ const _warnInvalidScopeOnce = (params: {
115124
}) => {
116125
if (!import.meta.env.DEV) return;
117126

127+
// Get the current Vue component instance (used to scope the warning cache)
128+
const instance = getCurrentInstance();
129+
if (!instance) return;
130+
118131
const {
119132
queryKey, enabled, currentScope, requiredScopes, isAppReady,
120133
} = params;
121-
if (!enabled || !isAppReady || !currentScope) return;
122134

123-
if (requiredScopes.includes(currentScope)) return;
135+
if (!isAppReady || !currentScope) return;
124136

125-
const key = Array.isArray(queryKey)
126-
? queryKey.join(':')
127-
: String(queryKey);
137+
const isValidScope = requiredScopes.includes(currentScope);
128138

129-
if (_warnedKeys.has(key)) return;
139+
if (!enabled && isValidScope) return;
130140

131-
_warnedKeys.add(key);
141+
if (isValidScope) return;
142+
143+
// Safely serialize the queryKey (even if it contains objects)
144+
const key = (() => {
145+
try {
146+
return JSON.stringify(queryKey);
147+
} catch {
148+
return Array.isArray(queryKey) ? queryKey.join(':') : String(queryKey);
149+
}
150+
})();
151+
152+
// Cache per component instance to prevent duplicate logs
153+
let keySet = _warnedKeysPerInstance.get(instance);
154+
if (!keySet) {
155+
keySet = new Set();
156+
_warnedKeysPerInstance.set(instance, keySet);
157+
}
158+
if (keySet.has(key)) return;
159+
keySet.add(key);
132160

133161
console.warn('[useScopedQuery] Query not executed due to invalid grant scope.', {
134162
queryKey,

0 commit comments

Comments
 (0)