Fix route changing on every hass change (#2988)

This commit is contained in:
Paulus Schoutsen 2019-03-21 12:22:32 -07:00 committed by GitHub
parent 48aa9a2ad7
commit 06b70e2653
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,6 +2,7 @@ import { UpdatingElement, property, PropertyValues } from "lit-element";
import "./hass-error-screen";
import { Route } from "../types";
import { navigate } from "../common/navigate";
import memoizeOne from "memoize-one";
const extractPage = (path: string, defaultPage: string) => {
if (path === "") {
@ -47,6 +48,18 @@ export class HassRouterPage extends UpdatingElement {
private _currentPage = "";
private _currentLoadProm?: Promise<void>;
private _cache = {};
private _computeTail = memoizeOne((route: Route) => {
const dividerPos = route.path.indexOf("/", 1);
return dividerPos === -1
? {
prefix: route.prefix + route.path,
path: "",
}
: {
prefix: route.prefix + route.path.substr(0, dividerPos),
path: route.path.substr(dividerPos),
};
});
protected update(changedProps: PropertyValues) {
super.update(changedProps);
@ -192,6 +205,10 @@ export class HassRouterPage extends UpdatingElement {
// default we do nothing
}
protected get routeTail(): Route {
return this._computeTail(this.route!);
}
private _createPanel(
routerOptions: RouterOptions,
page: string,
@ -210,18 +227,4 @@ export class HassRouterPage extends UpdatingElement {
this._cache[page] = panelEl;
}
}
protected get routeTail(): Route {
const route = this.route!;
const dividerPos = route.path.indexOf("/", 1);
return dividerPos === -1
? {
prefix: route.prefix + route.path,
path: "",
}
: {
prefix: route.prefix + route.path.substr(0, dividerPos),
path: route.path.substr(dividerPos),
};
}
}