Use explicity onDestroy calls rather than an onMount return value to ensure callbacks are actually executed to fix data provider cleanup issues

This commit is contained in:
Andrew Kingston 2021-10-27 14:24:36 +01:00
parent 2af3888d8a
commit 25bafc15d2
4 changed files with 19 additions and 21 deletions

View File

@ -1,6 +1,6 @@
<script> <script>
import Provider from "./Provider.svelte" import Provider from "./Provider.svelte"
import { onMount } from "svelte" import { onMount, onDestroy } from "svelte"
let width = window.innerWidth let width = window.innerWidth
let height = window.innerHeight let height = window.innerHeight
@ -21,12 +21,11 @@
} }
onMount(() => { onMount(() => {
const doc = document.getElementById("app-root") resizeObserver.observe(document.getElementById("app-root"))
resizeObserver.observe(doc) })
return () => { onDestroy(() => {
resizeObserver.unobserve(doc) resizeObserver.unobserve(document.getElementById("app-root"))
}
}) })
</script> </script>

View File

@ -1,5 +1,5 @@
<script> <script>
import { getContext, setContext, onMount } from "svelte" import { getContext, setContext, onDestroy } from "svelte"
import { dataSourceStore, createContextStore } from "stores" import { dataSourceStore, createContextStore } from "stores"
import { ActionTypes } from "constants" import { ActionTypes } from "constants"
import { generate } from "shortid" import { generate } from "shortid"
@ -56,9 +56,9 @@
} }
} }
onMount(() => { onDestroy(() => {
// Unregister all datasource instances when unmounting this provider // Unregister all datasource instances when unmounting this provider
return () => dataSourceStore.actions.unregisterInstance(instanceId) dataSourceStore.actions.unregisterInstance(instanceId)
}) })
</script> </script>

View File

@ -8,7 +8,7 @@
</script> </script>
<script> <script>
import { onMount } from "svelte" import { onMount, onDestroy } from "svelte"
import { get } from "svelte/store" import { get } from "svelte/store"
import IndicatorSet from "./IndicatorSet.svelte" import IndicatorSet from "./IndicatorSet.svelte"
import DNDPositionIndicator from "./DNDPositionIndicator.svelte" import DNDPositionIndicator from "./DNDPositionIndicator.svelte"
@ -209,8 +209,9 @@
document.addEventListener("dragenter", onDragEnter, false) document.addEventListener("dragenter", onDragEnter, false)
document.addEventListener("dragleave", onDragLeave, false) document.addEventListener("dragleave", onDragLeave, false)
document.addEventListener("drop", onDrop, false) document.addEventListener("drop", onDrop, false)
})
return () => { onDestroy(() => {
// Events fired on the draggable target // Events fired on the draggable target
document.removeEventListener("dragstart", onDragStart, false) document.removeEventListener("dragstart", onDragStart, false)
document.removeEventListener("dragend", onDragEnd, false) document.removeEventListener("dragend", onDragEnd, false)
@ -220,7 +221,6 @@
document.removeEventListener("dragenter", onDragEnter, false) document.removeEventListener("dragenter", onDragEnter, false)
document.removeEventListener("dragleave", onDragLeave, false) document.removeEventListener("dragleave", onDragLeave, false)
document.removeEventListener("drop", onDrop, false) document.removeEventListener("drop", onDrop, false)
}
}) })
</script> </script>

View File

@ -1,6 +1,5 @@
import { writable, get } from "svelte/store" import { writable, get } from "svelte/store"
import { fetchTableDefinition } from "../api" import { fetchTableDefinition } from "../api"
import { TableNames } from "../constants"
export const createDataSourceStore = () => { export const createDataSourceStore = () => {
const store = writable([]) const store = writable([])