Skip to content

Commit

Permalink
Canvas reset or trim svelthreeStores array on destruction, closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
Vatroslav Vrbanic committed Aug 30, 2022
1 parent 57896e7 commit 6847ddb
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/components/Canvas.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -850,8 +850,43 @@ This is a **svelthree** _Canvas_ Component.
// any newly added canvas will create a new store at the next highest index
// the value of 'sti' is completely irrelevant to the user, doesn't need to be handled.
$svelthreeStores[sti] = null
// if all stores are `null` we can safely reset the `svelthreeStores` array, e.g. when switching pages / routes.
if (all_stores_are_null()) {
$svelthreeStores = []
} else {
// trim `svelthreeStores` array's tail
trim_stores_array()
}
})
function all_stores_are_null(): boolean {
for (let i = 0; i < $svelthreeStores.length; i++) {
if ($svelthreeStores[i] !== null) {
return false
}
}
return true
}
function trim_stores_array(): void {
let start_index: number
for (let i = $svelthreeStores.length - 1; i > 0; i--) {
if ($svelthreeStores[i] === null && $svelthreeStores[i - 1] !== null) {
start_index = i
break
} else if ($svelthreeStores[i] !== null) {
break
}
}
if (start_index) {
const trim_length: number = $svelthreeStores.length - start_index
$svelthreeStores.splice(start_index, trim_length)
}
}
beforeUpdate(async () => {
if (verbose && log_lc && (log_lc.all || log_lc.bu)) console.info(...c_lc(c_name, "beforeUpdate"))
})
Expand Down

0 comments on commit 6847ddb

Please sign in to comment.