Code coverage report for src/pipeline-provider.js

Statements: 52.38% (11 / 21)      Branches: 0% (0 / 4)      Functions: 33.33% (4 / 12)      Lines: 52.38% (11 / 21)      Ignored: none     

All files » src/ » pipeline-provider.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 771 1 1 1 1 1                         1 26 26                                                                                       1                 104      
import {Container} from 'aurelia-dependency-injection';
import {Pipeline} from './pipeline';
import {BuildNavigationPlanStep} from './navigation-plan';
import {LoadRouteStep} from './route-loading';
import {CommitChangesStep} from './navigation-instruction';
import {
  CanDeactivatePreviousStep,
  CanActivateNextStep,
  DeactivatePreviousStep,
  ActivateNextStep
} from './activation';
 
/**
* Class responsible for creating the navigation pipeline.
*/
export class PipelineProvider {
  static inject() { return [Container]; }
 
  constructor(container: Container) {
    this.container = container;
    this.steps = [
      BuildNavigationPlanStep,
      CanDeactivatePreviousStep, //optional
      LoadRouteStep,
      this._createPipelineSlot('authorize'),
      CanActivateNextStep, //optional
      this._createPipelineSlot('preActivate', 'modelbind'),
      //NOTE: app state changes start below - point of no return
      DeactivatePreviousStep, //optional
      ActivateNextStep, //optional
      this._createPipelineSlot('preRender', 'precommit'),
      CommitChangesStep,
      this._createPipelineSlot('postRender', 'postcomplete')
    ];
  }
 
  /**
  * Create the navigation pipeline.
  */
  createPipeline(): Pipeline {
    let pipeline = new Pipeline();
    this.steps.forEach(step => pipeline.addStep(this.container.get(step)));
    return pipeline;
  }
 
  /**
  * Adds a step into the pipeline at a known slot location.
  */
  addStep(name: string, step: PipelineStep): void {
    let found = this.steps.find(x => x.slotName === name || x.slotAlias === name);
    if (found) {
      found.steps.push(step);
    } else {
      throw new Error(`Invalid pipeline slot name: ${name}.`);
    }
  }
 
  _createPipelineSlot(name, alias) {
    class PipelineSlot {
      static inject = [Container];
      static slotName = name;
      static slotAlias = alias;
      static steps = [];
 
      constructor(container) {
        this.container = container;
      }
 
      getSteps() {
        return PipelineSlot.steps.map(x => this.container.get(x));
      }
    }
 
    return PipelineSlot;
  }
}