core-api/routing: make RouteRefs immutable + move out some types

This commit is contained in:
Patrik Oldsberg
2020-09-19 15:16:24 +02:00
parent 558ae5593e
commit 988ebe7139
5 changed files with 19 additions and 27 deletions
+8 -13
View File
@@ -14,30 +14,25 @@
* limitations under the License.
*/
import type { RouteRefConfig, RouteRefOverrideConfig } from './types';
export class MutableRouteRef {
private effectiveConfig: RouteRefConfig = this.config;
import type { RouteRefConfig } from './types';
export class AbsoluteRouteRef {
constructor(private readonly config: RouteRefConfig) {}
override(overrideConfig: RouteRefOverrideConfig) {
this.effectiveConfig = { ...this.config, ...overrideConfig };
}
get icon() {
return this.effectiveConfig.icon;
return this.config.icon;
}
// TODO(Rugvip): Remove this, routes are looked up via the registry instead
get path() {
return this.effectiveConfig.path;
return this.config.path;
}
get title() {
return this.effectiveConfig.title;
return this.config.title;
}
}
export function createRouteRef(config: RouteRefConfig): MutableRouteRef {
return new MutableRouteRef(config);
export function createRouteRef(config: RouteRefConfig): AbsoluteRouteRef {
return new AbsoluteRouteRef(config);
}
@@ -14,7 +14,8 @@
* limitations under the License.
*/
import { RouteRefRegistry, resolveRoute } from './RouteRefRegistry';
import { RouteRefRegistry } from './RouteRefRegistry';
import { resolveRoute } from './types';
const ref1 = { [resolveRoute]: (path: string) => path };
const ref11 = { [resolveRoute]: (path: string) => path };
@@ -14,11 +14,7 @@
* limitations under the License.
*/
export const resolveRoute = Symbol('resolve-route');
type ConcreteRoute = {
[resolveRoute](path: string): string;
};
import { ConcreteRoute, resolveRoute } from './types';
const rootRoute: ConcreteRoute = {
[resolveRoute]: () => '',
+1 -2
View File
@@ -14,6 +14,5 @@
* limitations under the License.
*/
export * from './types';
export type { RouteRef, RouteRefConfig, ConcreteRoute } from './types';
export { createRouteRef } from './RouteRef';
export type { MutableRouteRef } from './RouteRef';
+7 -6
View File
@@ -16,7 +16,14 @@
import { IconComponent } from '../icons';
export const resolveRoute = Symbol('resolve-route');
export type ConcreteRoute = {
[resolveRoute](path: string): string;
};
export type RouteRef = {
// TODO(Rugvip): Remove path, look up via registry instead
path: string;
icon?: IconComponent;
title: string;
@@ -27,9 +34,3 @@ export type RouteRefConfig = {
icon?: IconComponent;
title: string;
};
export type RouteRefOverrideConfig = {
path?: string;
icon?: IconComponent;
title?: string;
};