From 79efceff759041b3a2e0799034a0abb0466c2af2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=90=E8=97=A4=E6=98=AD=E6=96=87?= Date: Thu, 14 Sep 2023 21:23:18 +0900 Subject: [PATCH] fix UrlStore's save syncedURL --- packages/location-state-core/src/stores/url-store.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/location-state-core/src/stores/url-store.ts b/packages/location-state-core/src/stores/url-store.ts index e70bf365..da651e0b 100644 --- a/packages/location-state-core/src/stores/url-store.ts +++ b/packages/location-state-core/src/stores/url-store.ts @@ -36,7 +36,7 @@ export const defaultSearchParamEncoder = searchParamEncoder( export class URLStore implements Store { private state: Record = {}; - private prevUrl: string | undefined; + private syncedURL: string | undefined; private readonly listeners: Map> = new Map(); constructor( @@ -85,9 +85,8 @@ export class URLStore implements Store { try { // save to url - const url = this.urlEncoder.encode(location.href, this.state); - this.prevUrl = url; - this.syncer.updateURL(url); + this.syncedURL = this.urlEncoder.encode(location.href, this.state); + this.syncer.updateURL(this.syncedURL); } catch (e) { console.error(e); } @@ -97,16 +96,18 @@ export class URLStore implements Store { load() { const currentURL = location.href; - if (currentURL === this.prevUrl) return; + if (currentURL === this.syncedURL) return; try { this.state = this.urlEncoder.decode(currentURL); + this.syncedURL = currentURL; } catch (e) { console.error(e); this.state = {}; // remove invalid state from url. const url = this.urlEncoder.encode(currentURL); this.syncer.updateURL(url); + this.syncedURL = currentURL; } queueMicrotask(() => this.notifyAll());