Line data Source code
1 : import * as React from 'react';
2 :
3 : import { useConnectionDispatch, useConnectionRegistryStore } from './connection_registry.js';
4 : import { DELETE_CONNECTION } from './connection_state.js';
5 : import { useHyperLogMetrics } from './hyper/hyper_log_metrics_provider.js';
6 : import { useCancelQuery } from './query_executor.js';
7 :
8 : /// Delete a connection
9 : export type DeleteConnection = (connectionId: string) => Promise<void>;
10 :
11 : /// Delete a connection with its folder.
12 : ///
13 : /// The connection is shut down first: its running queries are cancelled, which also stops the session executions
14 : /// that wait for them, and its log monitor closes the monitoring database in its folder. The registry refuses to
15 : /// delete a connection that sessions still use.
16 1 : export function useDeleteConnection(): DeleteConnection {
17 1 : const registryStore = useConnectionRegistryStore();
18 1 : const connectionDispatch = useConnectionDispatch();
19 1 : const cancelQuery = useCancelQuery();
20 1 : const logMetrics = useHyperLogMetrics();
21 :
22 1 : return React.useCallback(async (connectionId: string) => {
23 1 : const database = registryStore.current.connections.get(connectionId);
24 1 : if (database == null) return;
25 1 : await Promise.all(database.queriesActiveOrdered.map(queryId => cancelQuery(connectionId, queryId)));
26 1 : await logMetrics?.forget(connectionId);
27 1 : connectionDispatch(connectionId, { type: DELETE_CONNECTION, value: null });
28 1 : }, [cancelQuery, connectionDispatch, logMetrics, registryStore]);
29 1 : }
|