boring-router-react
Advanced tools
Comparing version 0.4.0 to 0.4.7
@@ -42,2 +42,3 @@ import { AbstractHistory, HistorySnapshot } from 'boring-router'; | ||
private completeRestoration; | ||
private _push; | ||
private pushEntry; | ||
@@ -44,0 +45,0 @@ private replaceEntry; |
@@ -17,5 +17,12 @@ "use strict"; | ||
this.lastUsedId = 0; | ||
this.onPopState = async (event) => { | ||
this.onPopState = (event) => { | ||
let { entries: trackedEntries } = this.tracked; | ||
let { id, data } = event.state; | ||
let state = event.state; | ||
// When using hash mode, entering a new hash directly in the browser will | ||
// also trigger popstate. And in that case state is null. | ||
if (!state) { | ||
void this._push(this.getRefByHRef(location.href), undefined, false); | ||
return; | ||
} | ||
let { id, data } = state; | ||
if (id > this.lastUsedId) { | ||
@@ -106,14 +113,3 @@ this.lastUsedId = id; | ||
async push(ref, data) { | ||
if (ref === this.ref) { | ||
return this.replace(ref, data); | ||
} | ||
await this.restoringPromise; | ||
let snapshot = this.pushEntry({ | ||
id: this.getNextId(), | ||
ref, | ||
data, | ||
}); | ||
debug('push', snapshot); | ||
this.snapshot = snapshot; | ||
this.emitChange(snapshot); | ||
return this._push(ref, data, true); | ||
} | ||
@@ -231,3 +227,3 @@ async replace(ref, data) { | ||
for (let entry of expectedEntries.slice(trackedActiveIndex + 1)) { | ||
this.pushEntry(entry); | ||
this.pushEntry(entry, true); | ||
} | ||
@@ -259,3 +255,17 @@ this.restoreActive(); | ||
} | ||
pushEntry({ id, ref, data, }) { | ||
async _push(ref, data, toPushState) { | ||
if (ref === this.ref) { | ||
return this.replace(ref, data); | ||
} | ||
await this.restoringPromise; | ||
let snapshot = this.pushEntry({ | ||
id: this.getNextId(), | ||
ref, | ||
data, | ||
}, toPushState); | ||
debug('push', snapshot); | ||
this.snapshot = snapshot; | ||
this.emitChange(snapshot); | ||
} | ||
pushEntry({ id, ref, data }, toPushState) { | ||
let tracked = this.tracked; | ||
@@ -269,9 +279,11 @@ let { entries } = tracked; | ||
this.tracked = snapshot; | ||
let href = this.getHRefByRef(ref); | ||
try { | ||
history.pushState({ id, data }, '', href); | ||
if (toPushState) { | ||
let href = this.getHRefByRef(ref); | ||
try { | ||
history.pushState({ id, data }, '', href); | ||
} | ||
catch (error) { | ||
history.pushState({ id }, '', href); | ||
} | ||
} | ||
catch (error) { | ||
history.pushState({ id }, '', href); | ||
} | ||
return snapshot; | ||
@@ -278,0 +290,0 @@ } |
{ | ||
"name": "boring-router-react", | ||
"version": "0.4.0", | ||
"version": "0.4.7", | ||
"description": "A type-safe MobX router with parallel routing support.", | ||
@@ -36,3 +36,3 @@ "repository": { | ||
}, | ||
"gitHead": "4d4772f9f24a1c9719302fc490877e11520e412f" | ||
"gitHead": "88c7eade23aac2c02b83e46207f72f337e3d8cab" | ||
} |
@@ -138,19 +138,3 @@ import { | ||
async push(ref: string, data?: TData): Promise<void> { | ||
if (ref === this.ref) { | ||
return this.replace(ref, data); | ||
} | ||
await this.restoringPromise; | ||
let snapshot = this.pushEntry({ | ||
id: this.getNextId(), | ||
ref, | ||
data, | ||
}); | ||
debug('push', snapshot); | ||
this.snapshot = snapshot; | ||
this.emitChange(snapshot); | ||
return this._push(ref, data, true); | ||
} | ||
@@ -234,7 +218,16 @@ | ||
private onPopState = async (event: PopStateEvent): Promise<void> => { | ||
private onPopState = (event: PopStateEvent): void => { | ||
let {entries: trackedEntries} = this.tracked; | ||
let {id, data} = event.state as BrowserHistoryState<TData>; | ||
let state = event.state as BrowserHistoryState<TData> | null; | ||
// When using hash mode, entering a new hash directly in the browser will | ||
// also trigger popstate. And in that case state is null. | ||
if (!state) { | ||
void this._push(this.getRefByHRef(location.href), undefined, false); | ||
return; | ||
} | ||
let {id, data} = state; | ||
if (id > this.lastUsedId) { | ||
@@ -363,3 +356,3 @@ this.lastUsedId = id; | ||
for (let entry of expectedEntries.slice(trackedActiveIndex + 1)) { | ||
this.pushEntry(entry); | ||
this.pushEntry(entry, true); | ||
} | ||
@@ -397,7 +390,33 @@ | ||
private pushEntry({ | ||
id, | ||
ref, | ||
data, | ||
}: BrowserHistoryEntry<TData>): BrowserHistorySnapshot<TData> { | ||
private async _push( | ||
ref: string, | ||
data: TData | undefined, | ||
toPushState: boolean, | ||
): Promise<void> { | ||
if (ref === this.ref) { | ||
return this.replace(ref, data); | ||
} | ||
await this.restoringPromise; | ||
let snapshot = this.pushEntry( | ||
{ | ||
id: this.getNextId(), | ||
ref, | ||
data, | ||
}, | ||
toPushState, | ||
); | ||
debug('push', snapshot); | ||
this.snapshot = snapshot; | ||
this.emitChange(snapshot); | ||
} | ||
private pushEntry( | ||
{id, ref, data}: BrowserHistoryEntry<TData>, | ||
toPushState: boolean, | ||
): BrowserHistorySnapshot<TData> { | ||
let tracked = this.tracked; | ||
@@ -416,8 +435,10 @@ | ||
let href = this.getHRefByRef(ref); | ||
if (toPushState) { | ||
let href = this.getHRefByRef(ref); | ||
try { | ||
history.pushState({id, data}, '', href); | ||
} catch (error) { | ||
history.pushState({id}, '', href); | ||
try { | ||
history.pushState({id, data}, '', href); | ||
} catch (error) { | ||
history.pushState({id}, '', href); | ||
} | ||
} | ||
@@ -424,0 +445,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
55546
1156