Skip to content

Commit

Permalink
refactor(all): more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
bigopon committed Apr 16, 2019
1 parent f9e9091 commit 7f2eeb3
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 68 deletions.
12 changes: 6 additions & 6 deletions src/activation-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ export const activationStrategy: ActivationStrategy = {
*/
export interface ActivationStrategy {
/**
* Reuse the existing view model, without invoking Router lifecycle hooks.
*/
* Reuse the existing view model, without invoking Router lifecycle hooks.
*/
noChange: 'no-change';
/**
* Reuse the existing view model, invoking Router lifecycle hooks.
*/
* Reuse the existing view model, invoking Router lifecycle hooks.
*/
invokeLifecycle: 'invoke-lifecycle';
/**
* Replace the existing view model, invoking Router lifecycle hooks.
*/
* Replace the existing view model, invoking Router lifecycle hooks.
*/
replace: 'replace';
}

Expand Down
40 changes: 20 additions & 20 deletions src/app-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ declare module 'aurelia-dependency-injection' {
const logger = LogManager.getLogger('app-router');

/**
* The main application router.
*/
* The main application router.
*/
export class AppRouter extends Router {

/**@internal */
Expand All @@ -41,9 +41,9 @@ export class AppRouter extends Router {
}

/**
* Fully resets the router's internal state. Primarily used internally by the framework when multiple calls to setRoot are made.
* Use with caution (actually, avoid using this). Do not use this to simply change your navigation model.
*/
* Fully resets the router's internal state. Primarily used internally by the framework when multiple calls to setRoot are made.
* Use with caution (actually, avoid using this). Do not use this to simply change your navigation model.
*/
reset(): void {
super.reset();
this.maxInstructionCount = 10;
Expand All @@ -55,10 +55,10 @@ export class AppRouter extends Router {
}

/**
* Loads the specified URL.
*
* @param url The URL fragment to load.
*/
* Loads the specified URL.
*
* @param url The URL fragment to load.
*/
loadUrl(url: string): Promise<NavigationInstruction> {
return this
._createNavigationInstruction(url)
Expand All @@ -70,11 +70,11 @@ export class AppRouter extends Router {
}

/**
* Registers a viewPort to be used as a rendering target for activated routes.
*
* @param viewPort The viewPort. This is typically a <router-view/> element in Aurelia default impl
* @param name The name of the viewPort. 'default' if unspecified.
*/
* Registers a viewPort to be used as a rendering target for activated routes.
*
* @param viewPort The viewPort. This is typically a <router-view/> element in Aurelia default impl
* @param name The name of the viewPort. 'default' if unspecified.
*/
registerViewPort(viewPort: /*ViewPort*/ any, name?: string): Promise<any> {
// having strong typing without changing public API
const $viewPort: ViewPort = viewPort;
Expand Down Expand Up @@ -119,10 +119,10 @@ export class AppRouter extends Router {
}

/**
* Activates the router. This instructs the router to begin listening for history changes and processing instructions.
*
* @params options The set of options to activate the router with.
*/
* Activates the router. This instructs the router to begin listening for history changes and processing instructions.
*
* @params options The set of options to activate the router with.
*/
activate(options?: NavigationOptions): void {
if (this.isActive) {
return;
Expand All @@ -137,8 +137,8 @@ export class AppRouter extends Router {
}

/**
* Deactivates the router.
*/
* Deactivates the router.
*/
deactivate(): void {
this.isActive = false;
this.history.deactivate();
Expand Down
86 changes: 44 additions & 42 deletions src/utilities-route-loading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export const loadNewRoute = (
routeLoader: RouteLoader,
navigationInstruction: NavigationInstruction
): Promise<any[] | void> => {
let toLoad = determineWhatToLoad(navigationInstruction);
let loadPromises = toLoad.map((loadingPlan: ILoadingPlan) => loadRoute(
let loadingPlans = determineLoadingPlans(navigationInstruction);
let loadPromises = loadingPlans.map((loadingPlan: ILoadingPlan) => loadRoute(
routeLoader,
loadingPlan.navigationInstruction,
loadingPlan.viewPortPlan
Expand All @@ -33,38 +33,38 @@ export const loadNewRoute = (
/**
* @internal Exported for unit testing
*/
export const determineWhatToLoad = (
export const determineLoadingPlans = (
navigationInstruction: NavigationInstruction,
toLoad: ILoadingPlan[] = []
loadingPlans: ILoadingPlan[] = []
): ILoadingPlan[] => {
let plan: Record<string, ViewPortPlan> = navigationInstruction.plan;
let viewPortPlans: Record<string, ViewPortPlan> = navigationInstruction.plan;

for (let viewPortName in plan) {
let viewPortPlan = plan[viewPortName];
let child_nav_instruction = viewPortPlan.childNavigationInstruction;
for (let viewPortName in viewPortPlans) {
let viewPortPlan = viewPortPlans[viewPortName];
let childNavInstruction = viewPortPlan.childNavigationInstruction;

if (viewPortPlan.strategy === InternalActivationStrategy.Replace) {
toLoad.push({ viewPortPlan, navigationInstruction } as ILoadingPlan);
loadingPlans.push({ viewPortPlan, navigationInstruction } as ILoadingPlan);

if (child_nav_instruction) {
determineWhatToLoad(child_nav_instruction, toLoad);
if (childNavInstruction) {
determineLoadingPlans(childNavInstruction, loadingPlans);
}
} else {
let viewPortInstruction = navigationInstruction.addViewPortInstruction(
viewPortName,
viewPortPlan.strategy,
viewPortPlan.prevModuleId,
viewPortPlan.prevComponent
) as ViewPortInstruction;

if (child_nav_instruction) {
viewPortInstruction.childNavigationInstruction = child_nav_instruction;
determineWhatToLoad(child_nav_instruction, toLoad);
let viewPortInstruction = navigationInstruction.addViewPortInstruction({
name: viewPortName,
strategy: viewPortPlan.strategy,
moduleId: viewPortPlan.prevModuleId,
component: viewPortPlan.prevComponent
}) as ViewPortInstruction;

if (childNavInstruction) {
viewPortInstruction.childNavigationInstruction = childNavInstruction;
determineLoadingPlans(childNavInstruction, loadingPlans);
}
}
}

return toLoad;
return loadingPlans;
};

/**
Expand All @@ -75,16 +75,17 @@ export const loadRoute = (
navigationInstruction: NavigationInstruction,
viewPortPlan: ViewPortPlan
): Promise<any> => {
let moduleId = viewPortPlan.config ? viewPortPlan.config.moduleId : null;
let planConfig = viewPortPlan.config;
let moduleId = planConfig ? planConfig.moduleId : null;

return loadComponent(routeLoader, navigationInstruction, viewPortPlan.config)
return loadComponent(routeLoader, navigationInstruction, planConfig)
.then((component) => {
let viewPortInstruction = navigationInstruction.addViewPortInstruction(
viewPortPlan.name,
viewPortPlan.strategy,
moduleId,
component
) as ViewPortInstruction;
let viewPortInstruction = navigationInstruction.addViewPortInstruction({
name: viewPortPlan.name,
strategy: viewPortPlan.strategy,
moduleId: moduleId,
component: component
}) as ViewPortInstruction;

let childRouter = component.childRouter;
if (childRouter) {
Expand Down Expand Up @@ -132,19 +133,20 @@ export const loadComponent = (
* typically contains information about view model, childContainer, view and router
*/
(component: ViewPortComponent) => {
let { viewModel, childContainer } = component;
component.router = router;
component.config = config;
let { viewModel, childContainer } = component;
component.router = router;
component.config = config;

if ('configureRouter' in viewModel) {
let childRouter = childContainer.getChildRouter();
component.childRouter = childRouter;
if ('configureRouter' in viewModel) {
let childRouter = childContainer.getChildRouter();
component.childRouter = childRouter;

return childRouter
.configure(c => viewModel.configureRouter(c, childRouter, lifecycleArgs[0], lifecycleArgs[1], lifecycleArgs[2]))
.then(() => component);
}
return childRouter
.configure(c => viewModel.configureRouter(c, childRouter, lifecycleArgs[0], lifecycleArgs[1], lifecycleArgs[2]))
.then(() => component);
}

return component;
});
return component;
}
);
};

0 comments on commit 7f2eeb3

Please sign in to comment.