Merge pull request #167 from vector-im/bwindels/fix-redirects

Fix back button not working when changing rooms
This commit is contained in:
Bruno Windels 2020-10-19 11:01:07 +00:00 committed by GitHub
commit f07b4a6554
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 25 deletions

View file

@ -22,22 +22,13 @@ export class URLRouter {
this._stringifyPath = stringifyPath; this._stringifyPath = stringifyPath;
this._subscription = null; this._subscription = null;
this._pathSubscription = null; this._pathSubscription = null;
this._isApplyingUrl = false;
} }
attach() { attach() {
this._subscription = this._history.subscribe(url => { this._subscription = this._history.subscribe(url => this._applyUrl(url));
const redirectedUrl = this._applyUrl(url);
if (redirectedUrl !== url) {
this._history.replaceUrlSilently(redirectedUrl);
}
});
this._applyUrl(this._history.get()); this._applyUrl(this._history.get());
this._pathSubscription = this._navigation.pathObservable.subscribe(path => { this._pathSubscription = this._navigation.pathObservable.subscribe(path => this._applyNavigationPath(path));
const url = this.urlForPath(path);
if (url !== this._history.get()) {
this._history.pushUrlSilently(url);
}
});
} }
dispose() { dispose() {
@ -45,11 +36,27 @@ export class URLRouter {
this._pathSubscription = this._pathSubscription(); this._pathSubscription = this._pathSubscription();
} }
_applyNavigationPath(path) {
const url = this.urlForPath(path);
if (url !== this._history.get()) {
if (this._isApplyingUrl) {
// redirect
this._history.replaceUrlSilently(url);
} else {
this._history.pushUrlSilently(url);
}
}
}
_applyUrl(url) { _applyUrl(url) {
const urlPath = this._history.urlAsPath(url) const urlPath = this._history.urlAsPath(url)
const navPath = this._navigation.pathFrom(this._parseUrlPath(urlPath, this._navigation.path)); const navPath = this._navigation.pathFrom(this._parseUrlPath(urlPath, this._navigation.path));
// this will cause _applyNavigationPath to be called,
// so set a flag whether this request came from ourselves
// (in which case it is a redirect if the url does not match the current one)
this._isApplyingUrl = true;
this._navigation.applyPath(navPath); this._navigation.applyPath(navPath);
return this._history.pathAsUrl(this._stringifyPath(navPath)); this._isApplyingUrl = false;
} }
pushUrl(url) { pushUrl(url) {

View file

@ -17,15 +17,12 @@ limitations under the License.
import {BaseObservableValue} from "../../../observable/ObservableValue.js"; import {BaseObservableValue} from "../../../observable/ObservableValue.js";
export class History extends BaseObservableValue { export class History extends BaseObservableValue {
constructor() { handleEvent(event) {
super(); if (event.type === "hashchange") {
this._boundOnHashChange = null;
}
_onHashChange() {
this.emit(this.get()); this.emit(this.get());
this._storeHash(this.get()); this._storeHash(this.get());
} }
}
get() { get() {
return document.location.hash; return document.location.hash;
@ -60,13 +57,11 @@ export class History extends BaseObservableValue {
} }
onSubscribeFirst() { onSubscribeFirst() {
this._boundOnHashChange = this._onHashChange.bind(this); window.addEventListener('hashchange', this);
window.addEventListener('hashchange', this._boundOnHashChange);
} }
onUnsubscribeLast() { onUnsubscribeLast() {
window.removeEventListener('hashchange', this._boundOnHashChange); window.removeEventListener('hashchange', this);
this._boundOnHashChange = null;
} }
_storeHash(hash) { _storeHash(hash) {