Cleanup view switching code a bit

This commit is contained in:
Danila Fedorin 2021-08-30 11:08:58 -07:00
parent 878663ca07
commit 628e99ba2d

View File

@ -35,10 +35,13 @@ export class RootViewModel extends ViewModel {
});
}
_updateChildVMs(oldLink) {
if (!oldLink || !oldLink.equals(this.link)) {
_updateChildVMs(newLink, oldLink) {
this.link = newLink;
if (!newLink) {
this.openLinkViewModel = null;
} else if (!oldLink || !oldLink.equals(newLink)) {
this.openLinkViewModel = new OpenLinkViewModel(this.childOptions({
link: this.link,
link: newLink,
clients: createClients(),
}));
}
@ -51,28 +54,27 @@ export class RootViewModel extends ViewModel {
}
updateHash(hash) {
// All view models except openLink are re-created anyway.
// Might as well clear them to avoid having to
// manually reset (n-1)/n view models in every case.
// All view models except openLink are re-created anyway. Might as well
// clear them to avoid having to manually reset (n-1)/n view models in every case.
// That just doesn't scale well when we add new views.
const oldLink = this.link;
this.link = null;
this.showDisclaimer = false;
this.loadServerPolicyViewModel = null;
this.createLinkViewModel = null;
let newLink;
if (hash.startsWith("#/policy/")) {
const server = hash.substr(9);
this.openLinkViewModel = null;
this._updateChildVMs(null, oldLink);
this.loadServerPolicyViewModel = new LoadServerPolicyViewModel(this.childOptions({server}));
this.loadServerPolicyViewModel.load();
} else if (hash.startsWith("#/disclaimer/")) {
this.openLinkViewModel = null;
this._updateChildVMs(null, oldLink);
this.showDisclaimer = true;
} else if (hash === "" || hash === "#" || hash === "#/") {
this.openLinkViewModel = null;
this._updateChildVMs(null, oldLink);
this.createLinkViewModel = new CreateLinkViewModel(this.childOptions());
} else if (this.link = Link.parse(hash)) {
this.link = Link.parse(hash);
this._updateChildVMs(oldLink);
} else if (newLink = Link.parse(hash)) {
this._updateChildVMs(newLink, oldLink);
}
this.emitChange();
}