fixed race conditions when starting app

This commit is contained in:
Jan Prochazka 2021-05-17 17:42:53 +02:00
parent e44a95d723
commit 9d933d669a
4 changed files with 66 additions and 14 deletions

View File

@ -43,7 +43,7 @@ async function handleFullRefresh() {
loadingModel = false; loadingModel = false;
} }
async function handleIncrementalRefresh() { async function handleIncrementalRefresh(forceSend) {
loadingModel = true; loadingModel = true;
const driver = requireEngineDriver(storedConnection); const driver = requireEngineDriver(storedConnection);
setStatusName('checkStructure'); setStatusName('checkStructure');
@ -51,8 +51,12 @@ async function handleIncrementalRefresh() {
analysedTime = new Date().getTime(); analysedTime = new Date().getTime();
if (newStructure != null) { if (newStructure != null) {
analysedStructure = newStructure; analysedStructure = newStructure;
}
if (forceSend || newStructure != null) {
process.send({ msgtype: 'structure', structure: analysedStructure }); process.send({ msgtype: 'structure', structure: analysedStructure });
} }
process.send({ msgtype: 'structureTime', analysedTime }); process.send({ msgtype: 'structureTime', analysedTime });
setStatusName('ok'); setStatusName('ok');
loadingModel = false; loadingModel = false;
@ -91,7 +95,7 @@ async function handleConnect({ connection, structure, globalSettings }) {
readVersion(); readVersion();
if (structure) { if (structure) {
analysedStructure = structure; analysedStructure = structure;
handleIncrementalRefresh(); handleIncrementalRefresh(true);
} else { } else {
handleFullRefresh(); handleFullRefresh();
} }

View File

@ -1,25 +1,47 @@
<script lang="ts"> <script lang="ts">
import { onMount } from 'svelte';
import CommandListener from './commands/CommandListener.svelte'; import CommandListener from './commands/CommandListener.svelte';
import DataGridRowHeightMeter from './datagrid/DataGridRowHeightMeter.svelte'; import DataGridRowHeightMeter from './datagrid/DataGridRowHeightMeter.svelte';
import LoadingInfo from './elements/LoadingInfo.svelte'; import LoadingInfo from './elements/LoadingInfo.svelte';
import PluginsProvider from './plugins/PluginsProvider.svelte'; import PluginsProvider from './plugins/PluginsProvider.svelte';
import Screen from './Screen.svelte'; import Screen from './Screen.svelte';
import { setAppLoaded } from './utility/appLoadManager';
import axiosInstance from './utility/axiosInstance';
import ErrorHandler from './utility/ErrorHandler.svelte'; import ErrorHandler from './utility/ErrorHandler.svelte';
import { useSettings } from './utility/metadataLoaders';
import OpenTabsOnStartup from './utility/OpenTabsOnStartup.svelte'; import OpenTabsOnStartup from './utility/OpenTabsOnStartup.svelte';
const settings = useSettings(); let loaded = false;
async function loadSettings() {
try {
const settings = await axiosInstance.get('config/get-settings');
const connections = await axiosInstance.get('connections/list');
const config = await axiosInstance.get('config/get');
loaded = settings?.data && connections?.data && config?.data;
if (loaded) {
setAppLoaded();
}
} catch (err) {
console.log('Error loading settings, trying again in 1s');
setTimeout(loadSettings, 1000);
}
}
onMount(loadSettings);
</script> </script>
<DataGridRowHeightMeter /> <DataGridRowHeightMeter />
<ErrorHandler /> <ErrorHandler />
<PluginsProvider />
<CommandListener /> <CommandListener />
<OpenTabsOnStartup />
{#if $settings} {#if loaded}
<PluginsProvider />
<OpenTabsOnStartup />
<Screen /> <Screen />
{:else} {:else}
<LoadingInfo message="Loading settings..." wrapper /> <LoadingInfo message="Starting DbGate API..." wrapper />
{/if} {/if}

View File

@ -0,0 +1,22 @@
let appIsLoaded = false;
let onLoad = [];
export function setAppLoaded() {
appIsLoaded = true;
for (const func of onLoad) {
func();
}
onLoad = [];
}
export function getAppLoaded() {
return appIsLoaded;
}
export function callWhenAppLoaded(callback) {
if (appIsLoaded) {
callback();
} else {
onLoad.push(callback);
}
}

View File

@ -1,10 +1,11 @@
import _ from 'lodash'; import _ from 'lodash';
import { currentDatabase, openedTabs } from '../stores'; import { currentDatabase, openedTabs } from '../stores';
import { callWhenAppLoaded } from './appLoadManager';
import { getConnectionInfo } from './metadataLoaders'; import { getConnectionInfo } from './metadataLoaders';
let lastCurrentTab = null; let lastCurrentTab = null;
openedTabs.subscribe(async value => { openedTabs.subscribe(value => {
const newCurrentTab = (value || []).find(x => x.selected); const newCurrentTab = (value || []).find(x => x.selected);
if (newCurrentTab == lastCurrentTab) return; if (newCurrentTab == lastCurrentTab) return;
@ -15,11 +16,14 @@ openedTabs.subscribe(async value => {
database && database &&
(conid != _.get(lastCurrentTab, 'props.conid') || database != _.get(lastCurrentTab, 'props.database')) (conid != _.get(lastCurrentTab, 'props.conid') || database != _.get(lastCurrentTab, 'props.database'))
) { ) {
const connection = await getConnectionInfo({ conid }); const doWork = async () => {
currentDatabase.set({ const connection = await getConnectionInfo({ conid });
connection, currentDatabase.set({
name: database, connection,
}); name: database,
});
};
callWhenAppLoaded(doWork);
} }
} }