refactor - handle cloud listeners

This commit is contained in:
SPRINX0\prochazka
2025-05-26 17:02:09 +02:00
parent afde0a7423
commit d26db7096d
6 changed files with 83 additions and 19 deletions

View 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({}));
}