Line data Source code
1 : import * as React from 'react';
2 :
3 : import { useConnectionRegistryStore } from '../connections/connection_registry.js';
4 : import { useReplaceConnection } from './session_connection_replace.js';
5 :
6 : /// Merge connections into a target connection
7 : export type ConnectionMerge = (targetId: string, sourceIds: readonly string[]) => Promise<void>;
8 :
9 : /// Merge connections that describe the same database into one.
10 : ///
11 : /// Each merged connection is replaced with the target, see useReplaceConnection: its sessions move to the
12 : /// target first, the merged connection is removed once no session uses it.
13 1 : export function useMergeConnections(): ConnectionMerge {
14 1 : const registryStore = useConnectionRegistryStore();
15 1 : const replaceConnection = useReplaceConnection();
16 :
17 1 : return React.useCallback(async (targetId: string, sourceIds: readonly string[]) => {
18 1 : for (const sourceId of sourceIds) {
19 1 : if (!registryStore.current.connections.has(targetId)) return;
20 1 : await replaceConnection(sourceId, targetId);
21 1 : }
22 1 : }, [registryStore, replaceConnection]);
23 1 : }
|