From 449dead2e591df1e8cbe5a1a5fd94d4c6a57d3bf Mon Sep 17 00:00:00 2001 From: Sidharth Vinod Date: Wed, 19 Oct 2022 00:27:36 +0530 Subject: [PATCH] fix: Race condition with async queue. --- package.json | 2 + src/lib/components/view.svelte | 46 +++++++++++--------- src/lib/util/mermaid.ts | 47 +++++++++++++++----- src/lib/util/state.ts | 79 ++++++++++++++-------------------- yarn.lock | 10 +++++ 5 files changed, 107 insertions(+), 77 deletions(-) diff --git a/package.json b/package.json index 59dcf517..f45e189a 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "@sveltejs/kit": "1.0.0-next.516", "@testing-library/jest-dom": "5.16.5", "@testing-library/svelte": "3.2.2", + "@types/async": "^3.2.15", "@types/pako": "2.0.0", "@types/uuid": "8.3.4", "@typescript-eslint/eslint-plugin": "5.40.1", @@ -67,6 +68,7 @@ "dependencies": { "analytics": "0.8.1", "analytics-plugin-plausible": "0.0.6", + "async": "^3.2.4", "daisyui": "2.31.0", "js-base64": "3.7.2", "mermaid": "9.2.0-rc6", diff --git a/src/lib/components/view.svelte b/src/lib/components/view.svelte index a8063cd8..8135b170 100644 --- a/src/lib/components/view.svelte +++ b/src/lib/components/view.svelte @@ -5,8 +5,8 @@ import type { State, ValidatedState } from '$lib/types'; import { logEvent } from '$lib/util/stats'; import { cmdKey } from '$lib/util/util'; - import { render as renderDiagram } from '$lib/util/mermaid'; - + import { render as renderDiagram, init as mermaidInit } from '$lib/util/mermaid'; + const init = mermaidInit(); let code = ''; let config = ''; let container: HTMLDivElement; @@ -50,7 +50,7 @@ }); }; - const handleStateChange = async (state: ValidatedState) => { + const handleStateChange = (state: ValidatedState) => { if (state.error !== undefined) { error = true; return; @@ -72,11 +72,11 @@ panZoomEnabled = state.panZoom; const scroll = view.parentElement.scrollTop; delete container.dataset.processed; - await renderDiagram( - Object.assign({}, JSON.parse(state.mermaid)), + renderDiagram({ + config: Object.assign({}, JSON.parse(state.mermaid)), code, - 'graph-div', - (svgCode, bindFunctions) => { + id: 'graph-div', + callback: (svgCode, bindFunctions) => { if (svgCode.length > 0) { handlePanZoom(state); container.innerHTML = svgCode; @@ -88,7 +88,7 @@ } } } - ); + }); view.parentElement.scrollTop = scroll; error = false; } else if (manualUpdate) { @@ -103,8 +103,8 @@ }; onMount(() => { - stateStore.subscribe(async (state) => { - await handleStateChange(state); + stateStore.subscribe((state) => { + handleStateChange(state); }); window.addEventListener('resize', () => { if ($stateStore.panZoom && pzoom) { @@ -114,19 +114,23 @@ }); -{#if error && $stateStore.error instanceof Error} -
{$stateStore.error}
-{/if} +{#await init} + Loading... +{:then} + {#if error && $stateStore.error instanceof Error} +
{$stateStore.error}
+ {/if} -{#if outOfSync} -
- Diagram out of sync.
- Press (Sync button) or {cmdKey} + Enter to sync. + {#if outOfSync} +
+ Diagram out of sync.
+ Press (Sync button) or {cmdKey} + Enter to sync. +
+ {/if} +
+
-{/if} -
-
-
+{/await}