11import "webextension-polyfill" ;
22import { tabScopedStorageManager } from "../storage/impl/tab-scoped-manager" ;
3- import type { QueryActionResult , IconUpdateMessage } from "../types/messages" ;
3+ import type {
4+ QueryActionResult ,
5+ IconUpdateMessage ,
6+ QueryActionMessage ,
7+ BulkQueryActionMessage ,
8+ } from "../types/messages" ;
49
510// Track preservation flags per tab
611const preserveArtificialStatesForTab = new Map < number , boolean > ( ) ;
@@ -9,7 +14,12 @@ const preserveArtificialStatesForTab = new Map<number, boolean>();
914type BackgroundMessage =
1015 | QueryActionResult
1116 | IconUpdateMessage
12- | { type : "GET_TAB_ID" } ;
17+ | { type : "GET_TAB_ID" }
18+ | {
19+ type : "QUERY_ACTION" ;
20+ tabId : number ;
21+ action : QueryActionMessage | BulkQueryActionMessage ; // Correct type
22+ } ;
1323
1424// Handle all essential messages - simplified for minimal background script
1525chrome . runtime . onMessage . addListener (
@@ -21,6 +31,27 @@ chrome.runtime.onMessage.addListener(
2131 return ;
2232 }
2333
34+ // Handle QUERY_ACTION messages from DevTools/Popup - forward to content script
35+ if ( message ?. type === "QUERY_ACTION" ) {
36+ const { tabId, action } = message ;
37+
38+ chrome . tabs
39+ . sendMessage ( tabId , {
40+ type : "QUERY_ACTION" ,
41+ action,
42+ source : "tanstack-query-devtools-background" ,
43+ } )
44+ . then ( ( ) => {
45+ sendResponse ( { success : true } ) ;
46+ } )
47+ . catch ( ( error ) => {
48+ console . warn ( `Failed to send action to tab ${ tabId } :` , error ) ;
49+ sendResponse ( { success : false , error : error . message } ) ;
50+ } ) ;
51+
52+ return true ; // Keep channel open for async response
53+ }
54+
2455 // Handle Content Script messages (have sender.tab.id)
2556 const tabId = sender . tab ?. id ;
2657 if ( tabId ) {
@@ -34,49 +65,19 @@ chrome.runtime.onMessage.addListener(
3465 return true ;
3566 }
3667
37- // Handle action results from content scripts - update artificial states in storage
68+ // Handle action results from content scripts
69+ // Note: Artificial state updates are handled directly by React components
70+ // via artificialStateManager, so no need to process them here
3871 if ( message . type === "QUERY_ACTION_RESULT" ) {
39- // Update artificial states in storage for all artificial state actions
40- if (
41- message . success &&
42- ( message . action === "TRIGGER_LOADING" ||
43- message . action === "TRIGGER_ERROR" ||
44- message . action === "CANCEL_LOADING" ||
45- message . action === "CANCEL_ERROR" ) &&
46- message . queryHash
47- ) {
48- ( async ( ) => {
49- try {
50- const tabStorage =
51- tabScopedStorageManager . getStorageForTab ( tabId ) ;
52- const queryHash = message . queryHash as string ;
53-
54- // Get current artificial states
55- const currentData = await tabStorage . get ( ) ;
56- const artificialStates = { ...currentData . artificialStates } ;
57-
58- if ( message . action === "TRIGGER_LOADING" ) {
59- // Start loading state
60- artificialStates [ queryHash ] = "loading" ;
61- } else if ( message . action === "CANCEL_LOADING" ) {
62- // Cancel loading state
63- delete artificialStates [ queryHash ] ;
64- } else if ( message . action === "TRIGGER_ERROR" ) {
65- // Start error state
66- artificialStates [ queryHash ] = "error" ;
67- } else if ( message . action === "CANCEL_ERROR" ) {
68- // Cancel error state
69- delete artificialStates [ queryHash ] ;
70- }
71-
72- // Update storage with new artificial states
73- await tabStorage . updateArtificialStates ( artificialStates ) ;
74- } catch ( error ) {
75- console . error ( "Failed to update artificial states:" , error ) ;
76- }
77- } ) ( ) ;
72+ // Log errors for debugging (optional - can be removed if not needed)
73+ if ( ! message . success ) {
74+ console . error (
75+ `Action ${ message . action } failed for query ${ message . queryHash } :` ,
76+ message . error ,
77+ ) ;
7878 }
7979
80+ // Just acknowledge receipt - no processing needed
8081 sendResponse ( { received : true } ) ;
8182 return true ;
8283 }
0 commit comments