From f2dd3c180c839467240b50b0ef68134f757f7df3 Mon Sep 17 00:00:00 2001 From: "Alessandro (Ale) Segala" <43508+ItalyPaleAle@users.noreply.github.com> Date: Wed, 16 Jun 2021 14:55:14 +0000 Subject: [PATCH] One less allocation if restoreScrollState isn't used --- Router.svelte | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/Router.svelte b/Router.svelte index d13af44..d10dc38 100644 --- a/Router.svelte +++ b/Router.svelte @@ -453,20 +453,20 @@ let previousScrollState = null // Update history.scrollRestoration depending on restoreScrollState $: history.scrollRestoration = restoreScrollState ? 'manual' : 'auto' - -const popStateChanged = (event) => { - // If this event was from our history.replaceState, event.state will contain - // our scroll history. Otherwise, event.state will be null (like on forward - // navigation) - if (event.state && event.state.__svelte_spa_router_scrollY) { - previousScrollState = event.state - } - else { - previousScrollState = null - } -} - +let popStateChanged = null if (restoreScrollState) { + popStateChanged = (event) => { + // If this event was from our history.replaceState, event.state will contain + // our scroll history. Otherwise, event.state will be null (like on forward + // navigation) + if (event.state && event.state.__svelte_spa_router_scrollY) { + previousScrollState = event.state + } + else { + previousScrollState = null + } + } + // This is removed in the destroy() invocation below window.addEventListener('popstate', popStateChanged) afterUpdate(() => { @@ -518,7 +518,7 @@ const unsubscribeLoc = loc.subscribe(async (newLoc) => { dispatchNextTick('conditionsFailed', detail) return } - + // Trigger an event to alert that we're loading the route // We need to clone the object on every event invocation so we don't risk the object to be modified in the next tick dispatchNextTick('routeLoading', Object.assign({}, detail)) @@ -590,6 +590,6 @@ const unsubscribeLoc = loc.subscribe(async (newLoc) => { onDestroy(() => { unsubscribeLoc() - window.removeEventListener('popstate', popStateChanged) + popStateChanged && window.removeEventListener('popstate', popStateChanged) })