Skip to content

Commit c66fc40

Browse files
committed
Extract shared refreshConnectionStatus helper and add layout listener cleanup
1 parent d08eac6 commit c66fc40

File tree

1 file changed

+23
-36
lines changed

1 file changed

+23
-36
lines changed

web/pgadmin/tools/sqleditor/static/js/components/QueryToolComponent.jsx

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -193,24 +193,24 @@ export default function QueryToolComponent({params, pgWindow, pgAdmin, selectedN
193193
eventBus.current.fireEvent(QUERY_TOOL_EVENTS.CHANGE_EOL, lineSep);
194194
}, []);
195195

196-
useInterval(async ()=>{
196+
const refreshConnectionStatus = useCallback(async (transId) => {
197197
try {
198-
let {data: respData} = await fetchConnectionStatus(api, qtState.params.trans_id);
198+
let {data: respData} = await fetchConnectionStatus(api, transId);
199199
if(respData.data) {
200200
setQtStatePartial({
201201
connected: true,
202202
connection_status: respData.data.status,
203203
});
204+
if(respData.data.notifies) {
205+
eventBus.current.fireEvent(QUERY_TOOL_EVENTS.PUSH_NOTICE, respData.data.notifies);
206+
}
204207
} else {
205208
setQtStatePartial({
206209
connected: false,
207210
connection_status: null,
208211
connection_status_msg: gettext('An unexpected error occurred - ensure you are logged into the application.')
209212
});
210213
}
211-
if(respData.data.notifies) {
212-
eventBus.current.fireEvent(QUERY_TOOL_EVENTS.PUSH_NOTICE, respData.data.notifies);
213-
}
214214
} catch (error) {
215215
console.error(error);
216216
setQtStatePartial({
@@ -219,6 +219,10 @@ export default function QueryToolComponent({params, pgWindow, pgAdmin, selectedN
219219
connection_status_msg: parseApiError(error),
220220
});
221221
}
222+
}, [api]);
223+
224+
useInterval(()=>{
225+
refreshConnectionStatus(qtState.params.trans_id);
222226
}, pollTime);
223227

224228

@@ -454,13 +458,14 @@ export default function QueryToolComponent({params, pgWindow, pgAdmin, selectedN
454458
forceClose();
455459
});
456460

457-
qtPanelDocker.eventBus.registerListener(LAYOUT_EVENTS.CLOSING, (id)=>{
461+
const onLayoutClosing = (id)=>{
458462
if(qtPanelId == id) {
459463
eventBus.current.fireEvent(QUERY_TOOL_EVENTS.WARN_SAVE_DATA_CLOSE);
460464
}
461-
});
465+
};
466+
qtPanelDocker.eventBus.registerListener(LAYOUT_EVENTS.CLOSING, onLayoutClosing);
462467

463-
qtPanelDocker.eventBus.registerListener(LAYOUT_EVENTS.ACTIVE, _.debounce((currentTabId)=>{
468+
const onLayoutActive = _.debounce((currentTabId)=>{
464469
/* Focus the appropriate panel on visible */
465470
if(qtPanelId == currentTabId) {
466471
setQtStatePartial({is_visible: true});
@@ -475,7 +480,8 @@ export default function QueryToolComponent({params, pgWindow, pgAdmin, selectedN
475480
} else {
476481
setQtStatePartial({is_visible: false});
477482
}
478-
}, 100));
483+
}, 100);
484+
qtPanelDocker.eventBus.registerListener(LAYOUT_EVENTS.ACTIVE, onLayoutActive);
479485

480486
/* If the tab or window is not visible, applicable for open in new tab */
481487
const onVisibilityChange = function() {
@@ -490,37 +496,18 @@ export default function QueryToolComponent({params, pgWindow, pgAdmin, selectedN
490496
// while the tab was hidden.
491497
const {params, connected_once} = qtStateRef.current;
492498
if(params?.trans_id && connected_once) {
493-
fetchConnectionStatus(api, params.trans_id)
494-
.then(({data: respData}) => {
495-
if(respData.data) {
496-
setQtStatePartial({
497-
connected: true,
498-
connection_status: respData.data.status,
499-
});
500-
if(respData.data.notifies) {
501-
eventBus.current.fireEvent(QUERY_TOOL_EVENTS.PUSH_NOTICE, respData.data.notifies);
502-
}
503-
} else {
504-
setQtStatePartial({
505-
connected: false,
506-
connection_status: null,
507-
connection_status_msg: gettext('An unexpected error occurred - ensure you are logged into the application.')
508-
});
509-
}
510-
})
511-
.catch((error) => {
512-
console.error(error);
513-
setQtStatePartial({
514-
connected: false,
515-
connection_status: null,
516-
connection_status_msg: parseApiError(error),
517-
});
518-
});
499+
refreshConnectionStatus(params.trans_id);
519500
}
520501
}
521502
};
522503
document.addEventListener('visibilitychange', onVisibilityChange);
523-
return ()=>document.removeEventListener('visibilitychange', onVisibilityChange);
504+
return ()=>{
505+
document.removeEventListener('visibilitychange', onVisibilityChange);
506+
if(qtPanelDocker?.eventBus) {
507+
qtPanelDocker.eventBus.deregisterListener(LAYOUT_EVENTS.CLOSING, onLayoutClosing);
508+
qtPanelDocker.eventBus.deregisterListener(LAYOUT_EVENTS.ACTIVE, onLayoutActive);
509+
}
510+
};
524511
}, []);
525512

526513
useEffect(() => { qtStateRef.current = qtState; }, [qtState]);

0 commit comments

Comments
 (0)