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

@@ -28,6 +28,7 @@
import { handleAuthOnStartup } from './clientAuth';
import { initializeAppUpdates } from './utility/appUpdate';
import { _t } from './translations';
import { installCloudListeners } from './utility/cloudListeners';
export let isAdminPage = false;
@@ -58,6 +59,7 @@
installNewVolatileConnectionListener();
installNewCloudTokenListener();
initializeAppUpdates();
installCloudListeners();
}
refreshPublicCloudFiles();

View File

@@ -457,4 +457,10 @@ focusedTreeDbKey.subscribe(value => {
});
export const getFocusedTreeDbKey = () => focusedTreeDbKeyValue;
let cloudConnectionsStoreValue = {};
cloudConnectionsStore.subscribe(value => {
cloudConnectionsStoreValue = value;
});
export const getCloudConnectionsStore = () => cloudConnectionsStoreValue;
window['__changeCurrentTheme'] = theme => currentTheme.set(theme);

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

View File

@@ -67,27 +67,27 @@
await apiCall('cloud/refresh-content');
}
async function loadCloudConnection(conid) {
const conn = await apiCall('connections/get', { conid });
$cloudConnectionsStore = {
...$cloudConnectionsStore,
[conid]: conn,
};
}
// async function loadCloudConnection(conid) {
// const conn = await apiCall('connections/get', { conid });
// $cloudConnectionsStore = {
// ...$cloudConnectionsStore,
// [conid]: conn,
// };
// }
function ensureCloudConnectionsLoaded(...conids) {
_.uniq(conids).forEach(conid => {
if (conid?.startsWith('cloud://') && !$cloudConnectionsStore[conid]) {
loadCloudConnection(conid);
}
});
}
// function ensureCloudConnectionsLoaded(...conids) {
// _.uniq(conids).forEach(conid => {
// if (conid?.startsWith('cloud://') && !$cloudConnectionsStore[conid]) {
// loadCloudConnection(conid);
// }
// });
// }
$: ensureCloudConnectionsLoaded(
$currentDatabase?.connection?._id,
...$openedSingleDatabaseConnections,
...$openedConnections
);
// $: ensureCloudConnectionsLoaded(
// $currentDatabase?.connection?._id,
// ...$openedSingleDatabaseConnections,
// ...$openedConnections
// );
// onMount(() => {
// const currentConid = $currentDatabase?.connection?._id;