mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-22 08:46:00 +00:00
refactor - handle cloud listeners
This commit is contained in:
48
packages/web/src/utility/cloudListeners.ts
Normal file
48
packages/web/src/utility/cloudListeners.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { derived } from 'svelte/store';
|
||||
import {
|
||||
cloudConnectionsStore,
|
||||
currentDatabase,
|
||||
getCloudConnectionsStore,
|
||||
openedConnections,
|
||||
openedSingleDatabaseConnections,
|
||||
} from '../stores';
|
||||
import { apiCall, apiOn } from './api';
|
||||
import _ from 'lodash';
|
||||
|
||||
export const possibleCloudConnectionSources = derived(
|
||||
[currentDatabase, openedSingleDatabaseConnections, openedConnections],
|
||||
([$currentDatabase, $openedSingleDatabaseConnections, $openedConnections]) => {
|
||||
const conids = new Set<string>();
|
||||
if ($currentDatabase?.connection?._id) {
|
||||
conids.add($currentDatabase.connection._id);
|
||||
}
|
||||
$openedSingleDatabaseConnections.forEach(x => conids.add(x));
|
||||
$openedConnections.forEach(x => conids.add(x));
|
||||
return Array.from(conids).filter(x => x?.startsWith('cloud://'));
|
||||
}
|
||||
);
|
||||
|
||||
async function loadCloudConnection(conid) {
|
||||
const conn = await apiCall('connections/get', { conid });
|
||||
cloudConnectionsStore.update(store => ({
|
||||
...store,
|
||||
[conid]: conn,
|
||||
}));
|
||||
}
|
||||
|
||||
function ensureCloudConnectionsLoaded(...conids) {
|
||||
const conns = getCloudConnectionsStore();
|
||||
_.uniq(conids).forEach(conid => {
|
||||
if (!conns[conid]) {
|
||||
loadCloudConnection(conid);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function installCloudListeners() {
|
||||
possibleCloudConnectionSources.subscribe(conids => {
|
||||
ensureCloudConnectionsLoaded(...conids);
|
||||
});
|
||||
|
||||
apiOn('cloud-content-updated', () => cloudConnectionsStore.set({}));
|
||||
}
|
||||
Reference in New Issue
Block a user