Skip to content

Commit

Permalink
fix UrlStore's save syncedURL
Browse files Browse the repository at this point in the history
  • Loading branch information
AkifumiSato committed Sep 14, 2023
1 parent 6f0a06e commit 79efcef
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions packages/location-state-core/src/stores/url-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const defaultSearchParamEncoder = searchParamEncoder(

export class URLStore implements Store {
private state: Record<string, unknown> = {};
private prevUrl: string | undefined;
private syncedURL: string | undefined;
private readonly listeners: Map<string, Set<Listener>> = new Map();

constructor(
Expand Down Expand Up @@ -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);
}
Expand All @@ -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());
Expand Down

0 comments on commit 79efcef

Please sign in to comment.