Skip to content

Commit 39f1c3b

Browse files
committed
Address review: fix stale closure, add cleanup, add abstract method
1 parent 9b09e2b commit 39f1c3b

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ export default function QueryToolComponent({params, pgWindow, pgAdmin, selectedN
168168
setQtState((prev)=>({...prev,...evalFunc(null, state, prev)}));
169169
};
170170
const isDirtyRef = useRef(false); // usefull when conn change.
171+
const qtStateRef = useRef(qtState);
171172
const eventBus = useRef(eventBusObj || (new EventBus()));
172173
const docker = useRef(null);
173174
const api = useMemo(()=>getApiInstance(), []);
@@ -477,7 +478,7 @@ export default function QueryToolComponent({params, pgWindow, pgAdmin, selectedN
477478
}, 100));
478479

479480
/* If the tab or window is not visible, applicable for open in new tab */
480-
document.addEventListener('visibilitychange', function() {
481+
const onVisibilityChange = function() {
481482
if(document.hidden) {
482483
setQtStatePartial({is_visible: false});
483484
} else {
@@ -487,14 +488,18 @@ export default function QueryToolComponent({params, pgWindow, pgAdmin, selectedN
487488
// status. This ensures a dead connection is detected right away
488489
// instead of waiting for the next poll interval, which was disabled
489490
// while the tab was hidden.
490-
if(qtState.params?.trans_id && qtState.connected_once) {
491-
fetchConnectionStatus(api, qtState.params.trans_id)
491+
const {params, connected_once} = qtStateRef.current;
492+
if(params?.trans_id && connected_once) {
493+
fetchConnectionStatus(api, params.trans_id)
492494
.then(({data: respData}) => {
493495
if(respData.data) {
494496
setQtStatePartial({
495497
connected: true,
496498
connection_status: respData.data.status,
497499
});
500+
if(respData.data.notifies) {
501+
eventBus.current.fireEvent(QUERY_TOOL_EVENTS.PUSH_NOTICE, respData.data.notifies);
502+
}
498503
} else {
499504
setQtStatePartial({
500505
connected: false,
@@ -513,9 +518,13 @@ export default function QueryToolComponent({params, pgWindow, pgAdmin, selectedN
513518
});
514519
}
515520
}
516-
});
521+
};
522+
document.addEventListener('visibilitychange', onVisibilityChange);
523+
return ()=>document.removeEventListener('visibilitychange', onVisibilityChange);
517524
}, []);
518525

526+
useEffect(() => { qtStateRef.current = qtState; }, [qtState]);
527+
519528
useEffect(() => usePreferences.subscribe(
520529
state => {
521530
setQtStatePartial({preferences: {

web/pgadmin/utils/driver/abstract.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,14 @@ def async_fetchmany_2darray(self, records=-1,
207207
def connected(self):
208208
pass
209209

210+
@abstractmethod
211+
def connection_ping(self):
212+
"""
213+
Check if the connection is actually alive by sending a lightweight
214+
query to the server. Returns True if alive, False otherwise.
215+
"""
216+
pass
217+
210218
@abstractmethod
211219
def reset(self):
212220
pass

0 commit comments

Comments
 (0)