Skip to content

Commit

Permalink
refactor(all): adjust interfaces/impl, add router events export
Browse files Browse the repository at this point in the history
  • Loading branch information
bigopon committed Mar 29, 2019
1 parent 260e692 commit 7976e1f
Show file tree
Hide file tree
Showing 22 changed files with 139 additions and 116 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"typescript.tsdk": "aurelia-router/node_modules/typescript/lib"
"typescript.tsdk": "aurelia-router/node_modules/typescript/lib",
"tslint.autoFixOnSave": true
}
2 changes: 1 addition & 1 deletion build/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function generateDts() {
console.log('\n==============\nGenerating dts bundle...\n==============');
return new Promise(resolve => {
const ChildProcess = require('child_process');
ChildProcess.exec('npm run bundle-dts', (err, stdout, stderr) => {
ChildProcess.exec('npm run build:dts', (err, stdout, stderr) => {
if (err || stderr) {
console.log('Generating dts error:');
console.log(stderr);
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"author": "Rob Eisenberg <rob@bluespire.com> (http://robeisenberg.com/)",
"main": "dist/commonjs/aurelia-router.js",
"module": "dist/es2015/aurelia-router.js",
"typings": "dist/index.d.ts",
"typings": "dist/aurelia-router.d.ts",
"repository": {
"type": "git",
"url": "http://github.com/aurelia/router"
Expand All @@ -23,8 +23,10 @@
"start": "npm run dev -- --format es2015",
"dev": "node build/scripts/dev",
"build": "node build/scripts/build",
"bundle-dts": "dts-bundle-generator -o dist/index.d.ts src/index.ts",
"test": "karma start",
"build:dts": "dts-bundle-generator -o dist/aurelia-router.d.ts src/aurelia-router.ts",
"test": "karma start --single-run",
"test:watch": "karma start",
"test:debugger": "karma ",
"lint": "tslint -c tslint.json '{src,test}/**/*.ts' --force"
},
"jspm": {
Expand Down Expand Up @@ -58,7 +60,7 @@
"aurelia-history": "^1.1.0",
"aurelia-logging": "^1.0.0",
"aurelia-path": "^1.0.0",
"aurelia-route-recognizer": "^1.2.0"
"aurelia-route-recognizer": "^1.3.2"
},
"devDependencies": {
"@types/jasmine": "^2.8.8",
Expand Down
5 changes: 2 additions & 3 deletions src/activation.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { activationStrategy } from './navigation-plan';
import { Next, ViewPortComponent, ViewPortPlan } from './interfaces';
import { isNavigationCommand } from './navigation-commands';
import { NavigationInstruction } from './navigation-instruction';
import { Next } from './pipeline';
import { activationStrategy } from './navigation-plan';
import { Router } from './router';
import { ViewPortComponent, ViewPortPlan } from './interfaces';

export class CanDeactivatePreviousStep {
run(navigationInstruction: NavigationInstruction, next: Next) {
Expand Down
12 changes: 11 additions & 1 deletion src/index.ts → src/aurelia-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export {
ConfiguresRouter,
RouteConfig,
NavigationResult,
Next,
PipelineResult,
PipelineStep,
ViewPort,
Expand All @@ -32,8 +33,17 @@ export {
NavigationInstruction,
NavigationInstructionInit
} from './navigation-instruction';

// export {
// PipelineStatus
// } from './pipeline-status';

export {
RouterEvent
} from './router-events';

export { PipelineProvider } from './pipeline-provider';
export { Pipeline, Next, pipelineStatus } from './pipeline';
export { Pipeline } from './pipeline';
export { RouteLoader, LoadRouteStep } from './route-loading';
export { RouterConfiguration } from './router-configuration';
export { Router } from './router';
28 changes: 27 additions & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Router } from './router';
import { NavModel } from './nav-model';
import { RouterConfiguration } from './router-configuration';
import { NavigationCommand } from './navigation-commands';
import { Next } from './pipeline';
import { IObservable } from './activation';

/**@internal */
Expand Down Expand Up @@ -297,3 +296,30 @@ export interface ViewPortInstruction {
export type NavigationResult = boolean | Promise<PipelineResult | boolean>;

export type LifecycleArguments = [Record<string, string>, RouteConfig, NavigationInstruction];

/**
* A callback to indicate when pipeline processing should advance to the next step
* or be aborted.
*/
export interface Next {
/**
* Indicates the successful completion of the pipeline step.
*/
(): Promise<any>;
/**
* Indicates the successful completion of the entire pipeline.
*/
complete: (result?: any) => Promise<any>;

/**
* Indicates that the pipeline should cancel processing.
*/
cancel: (result?: any) => Promise<any>;

/**
* Indicates that pipeline processing has failed and should be stopped.
*/
reject: (result?: any) => Promise<any>;
}

export type StepRunnerFunction = <TThis = any>(this: TThis, instruction: NavigationInstruction, next: Next) => any;
3 changes: 1 addition & 2 deletions src/navigation-plan.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ActivationStrategy, Next, ViewPortPlan } from './interfaces';
import { Redirect } from './navigation-commands';
import { NavigationInstruction } from './navigation-instruction';
import { ActivationStrategy, ViewPortPlan } from './interfaces';
import { Next } from './pipeline';

/**
* The strategy to use when activating modules during navigation.
Expand Down
40 changes: 40 additions & 0 deletions src/next.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { PipelineStatus } from './pipeline-status';
import { NavigationInstruction } from './navigation-instruction';
import { Next, StepRunnerFunction } from './interfaces';

/**@internal exported for unit testing */
export function createNextFn(instruction: NavigationInstruction, steps: StepRunnerFunction[]): Next {
let index = -1;
const next: Next = function() {
index++;

if (index < steps.length) {
let currentStep = steps[index];

try {
return currentStep(instruction, next);
} catch (e) {
return next.reject(e);
}
} else {
return next.complete();
}
} as Next;

next.complete = createCompletionHandler(next, PipelineStatus.completed);
next.cancel = createCompletionHandler(next, PipelineStatus.canceled);
next.reject = createCompletionHandler(next, PipelineStatus.rejected);

return next;
}


/**@internal exported for unit testing */
export function createCompletionHandler(next: Next, status: PipelineStatus) {
return (output: any) => Promise
.resolve({
status,
output,
completed: status === PipelineStatus.completed
});
}
9 changes: 9 additions & 0 deletions src/pipeline-status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* The status of a Pipeline.
*/
export enum PipelineStatus {
completed = 'completed',
canceled = 'canceled',
rejected = 'rejected',
running = 'running'
}
84 changes: 7 additions & 77 deletions src/pipeline.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,16 @@
import { PipelineStep, PipelineResult } from './interfaces';
import { PipelineStep, PipelineResult, Next, StepRunnerFunction } from './interfaces';
import { NavigationInstruction } from './navigation-instruction';

export interface PipeLineStatus {
completed: 'completed';
canceled: 'canceled';
rejected: 'rejected';
running: 'running';
}

export type PipeLineStatusType = PipeLineStatus[keyof PipeLineStatus];

/**
* The status of a Pipeline.
*/
export const pipelineStatus: PipeLineStatus = {
completed: 'completed',
canceled: 'canceled',
rejected: 'rejected',
running: 'running'
};

/**
* A callback to indicate when pipeline processing should advance to the next step
* or be aborted.
*/
export interface Next {
/**
* Indicates the successful completion of the pipeline step.
*/
(): Promise<any>;
/**
* Indicates the successful completion of the entire pipeline.
*/
complete: (result?: any) => Promise<any>;

/**
* Indicates that the pipeline should cancel processing.
*/
cancel: (result?: any) => Promise<any>;

/**
* Indicates that pipeline processing has failed and should be stopped.
*/
reject: (result?: any) => Promise<any>;
}
import { createNextFn } from './next';

/**
* The class responsible for managing and processing the navigation pipeline.
*/
export class Pipeline {
/**
* The pipeline steps.
* The pipeline steps. And steps added via addStep will be converted to a function
* The actualy running functions with correct step contexts of this pipeline
*/
steps: Array<Function | PipelineStep> = [];
steps: StepRunnerFunction[] = [];

/**
* Adds a step to the pipeline.
Expand Down Expand Up @@ -86,35 +44,7 @@ export class Pipeline {
* @param instruction The navigation instruction to process.
*/
run(instruction: NavigationInstruction): Promise<PipelineResult> {
let index = -1;
const steps = this.steps;

const next: Next = function() {
index++;

if (index < steps.length) {
let currentStep = steps[index];

try {
return (currentStep as Function)(instruction, next);
} catch (e) {
return next.reject(e);
}
} else {
return next.complete();
}
} as Next;

next.complete = createCompletionHandler(next, pipelineStatus.completed);
next.cancel = createCompletionHandler(next, pipelineStatus.canceled);
next.reject = createCompletionHandler(next, pipelineStatus.rejected);

return next();
const nextFn = createNextFn(instruction, this.steps);
return nextFn();
}
}

function createCompletionHandler(next: Next, status: PipeLineStatusType) {
return (output: any) => {
return Promise.resolve({ status, output, completed: status === pipelineStatus.completed });
};
}
7 changes: 3 additions & 4 deletions src/route-loading.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { activationStrategy, _buildNavigationPlan } from './navigation-plan';
import { NavigationInstruction } from './navigation-instruction';
import { ViewPortInstruction, RouteConfig, ViewPortComponent, ViewPortPlan } from './interfaces';
import { Next, RouteConfig, ViewPortComponent, ViewPortPlan } from './interfaces';
import { Redirect } from './navigation-commands';
import { NavigationInstruction } from './navigation-instruction';
import { activationStrategy, _buildNavigationPlan } from './navigation-plan';
import { Router } from './router';
import { Next } from './pipeline';

export class RouteLoader {
loadRoute(router: Router, config: RouteConfig, navigationInstruction: NavigationInstruction): Promise<ViewPortComponent> {
Expand Down
2 changes: 1 addition & 1 deletion src/router-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export class RouterConfiguration {
}

if (this.unknownRouteConfig) {
router.handleUnknownRoutes(this.unknownRouteConfig);
router.handleUnknownRoutes(this.unknownRouteConfig as any);
}

if (this._fallbackRoute) {
Expand Down
8 changes: 8 additions & 0 deletions src/router-events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export enum RouterEvent {
processing = 'router:navigation:processing',
error = 'router:navigation:error',
canceled = 'router:navigation:canceled',
complete = 'router:navigation:complete',
success = 'router:navigation:success',
childComplete = 'router:navigation:child:complete'
}
10 changes: 5 additions & 5 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,14 +336,14 @@ export class Router {
// to the childRoute property of params for the childRouter to recognize. When generating routes, we
// use the childRecognizer when childRoute params are available to generate a child router enabled route.
let recognizer = 'childRoute' in params ? this._childRecognizer : this._recognizer;
let hasRoute = recognizer.hasRoute(nameOrRoute);
let hasRoute = recognizer.hasRoute(nameOrRoute as string | RouteHandler);
if (!hasRoute) {
if (this.parent) {
return this.parent.generate(nameOrRoute, params, options);
}
throw new Error(`A route with name '${nameOrRoute}' could not be found. Check that \`name: '${nameOrRoute}'\` was specified in the route's config.`);
}
let path = recognizer.generate(nameOrRoute, params);
let path = recognizer.generate(nameOrRoute as string | RouteHandler, params);
let rootedPath = _createRootedPath(path, this.baseUrl, this.history._hasPushState, options.absolute);
return options.absolute ? `${this.history.getAbsoluteRoot()}${rootedPath}` : rootedPath;
}
Expand Down Expand Up @@ -643,7 +643,7 @@ export class Router {
instruction: NavigationInstruction
) {
return Promise.resolve(config)
.then(c => {
.then((c: any) => {
if (typeof c === 'string') {
return { moduleId: c } as RouteConfig;
} else if (typeof c === 'function') {
Expand All @@ -652,8 +652,8 @@ export class Router {

return c;
})
.then(c => typeof c === 'string' ? { moduleId: c } as RouteConfig : c)
.then(c => {
.then((c: string | RouteConfig) => typeof c === 'string' ? { moduleId: c } as RouteConfig : c)
.then((c: RouteConfig) => {
c.route = instruction.params.path;
validateRouteConfig(c, this.routes);

Expand Down
2 changes: 1 addition & 1 deletion test/activation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
ActivateNextStep,
NavigationInstructionInit,
ViewPortPlan
} from '../src';
} from '../src/aurelia-router';
import { ValueOf, createPipelineState, MockPipelineState, MockInstruction } from './shared';

describe('activation', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/app-router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
ViewPortInstruction,
ViewPort,
PipelineStep
} from '../src';
} from '../src/aurelia-router';
import { MockHistory, MockInstruction } from './shared';
import { EventAggregator } from 'aurelia-event-aggregator';
import { History } from 'aurelia-history';
Expand Down
Loading

0 comments on commit 7976e1f

Please sign in to comment.