Code coverage report for src/activation.js

Statements: 89.01% (81 / 91)      Branches: 75.86% (44 / 58)      Functions: 90.48% (19 / 21)      Lines: 88.76% (79 / 89)      Ignored: none     

All files » src/ » activation.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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 1941 1       13           3                               1 13 13     14 9     5     1 22 15 15 15 14   1       7     13     1 15 17 17   17     16   16 16       17 2 15 15       15     1 17   17 2   2 2 2 2   2 2     2         1 3 3 3     3 2     1     1 5   5 3 3 3 3           2     3     1 3   3 3 3 3   3 3             3                   3     1 17       17               17       17     1 17 2     15 15          
import {activationStrategy} from './navigation-plan';
import {isNavigationCommand} from './navigation-commands';
 
export class CanDeactivatePreviousStep {
  run(navigationInstruction: NavigationInstruction, next: Function) {
    return processDeactivatable(navigationInstruction.plan, 'canDeactivate', next);
  }
}
 
export class CanActivateNextStep {
  run(navigationInstruction: NavigationInstruction, next: Function) {
    return processActivatable(navigationInstruction, 'canActivate', next);
  }
}
 
export class DeactivatePreviousStep {
  run(navigationInstruction: NavigationInstruction, next: Function) {
    return processDeactivatable(navigationInstruction.plan, 'deactivate', next, true);
  }
}
 
export class ActivateNextStep {
  run(navigationInstruction: NavigationInstruction, next: Function) {
    return processActivatable(navigationInstruction, 'activate', next, true);
  }
}
 
function processDeactivatable(plan, callbackName, next, ignoreResult) {
  let infos = findDeactivatable(plan, callbackName);
  let i = infos.length; //query from inside out
 
  function inspect(val) {
    if (ignoreResult || shouldContinue(val)) {
      return iterate();
    }
 
    return next.cancel(val);
  }
 
  function iterate() {
    if (i--) {
      try {
        let viewModel = infos[i];
        let result = viewModel[callbackName]();
        return processPotential(result, inspect, next.cancel);
      } catch (error) {
        return next.cancel(error);
      }
    }
 
    return next();
  }
 
  return iterate();
}
 
function findDeactivatable(plan, callbackName, list: Array<Object> = []): Array<Object> {
  for (let viewPortName in plan) {
    let viewPortPlan = plan[viewPortName];
    let prevComponent = viewPortPlan.prevComponent;
 
    if ((viewPortPlan.strategy === activationStrategy.invokeLifecycle ||
        viewPortPlan.strategy === activationStrategy.replace) &&
        prevComponent) {
      let viewModel = prevComponent.viewModel;
 
      Eif (callbackName in viewModel) {
        list.push(viewModel);
      }
    }
 
    if (viewPortPlan.childNavigationInstruction) {
      findDeactivatable(viewPortPlan.childNavigationInstruction.plan, callbackName, list);
    } else Eif (prevComponent) {
      addPreviousDeactivatable(prevComponent, callbackName, list);
    }
  }
 
  return list;
}
 
function addPreviousDeactivatable(component, callbackName, list): void {
  let childRouter = component.childRouter;
 
  if (childRouter && childRouter.currentInstruction) {
    let viewPortInstructions = childRouter.currentInstruction.viewPortInstructions;
 
    for (let viewPortName in viewPortInstructions) {
      let viewPortInstruction = viewPortInstructions[viewPortName];
      let prevComponent = viewPortInstruction.component;
      let prevViewModel = prevComponent.viewModel;
 
      Eif (callbackName in prevViewModel) {
        list.push(prevViewModel);
      }
 
      addPreviousDeactivatable(prevComponent, callbackName, list);
    }
  }
}
 
function processActivatable(navigationInstruction: NavigationInstruction, callbackName: any, next: Function, ignoreResult: boolean) {
  let infos = findActivatable(navigationInstruction, callbackName);
  let length = infos.length;
  let i = -1; //query from top down
 
  function inspect(val, router) {
    if (ignoreResult || shouldContinue(val, router)) {
      return iterate();
    }
 
    return next.cancel(val);
  }
 
  function iterate() {
    i++;
 
    if (i < length) {
      try {
        let current = infos[i];
        let result = current.viewModel[callbackName](...current.lifecycleArgs);
        return processPotential(result, val => inspect(val, current.router), next.cancel);
      } catch (error) {
        return next.cancel(error);
      }
    }
 
    return next();
  }
 
  return iterate();
}
 
function findActivatable(navigationInstruction: NavigationInstruction, callbackName: string, list: Array<Object> = [], router: Router): Array<Object> {
  let plan = navigationInstruction.plan;
 
  Object.keys(plan).filter((viewPortName) => {
    let viewPortPlan = plan[viewPortName];
    let viewPortInstruction = navigationInstruction.viewPortInstructions[viewPortName];
    let viewModel = viewPortInstruction.component.viewModel;
 
    Eif ((viewPortPlan.strategy === activationStrategy.invokeLifecycle || viewPortPlan.strategy === activationStrategy.replace) && callbackName in viewModel) {
      list.push({
        viewModel,
        lifecycleArgs: viewPortInstruction.lifecycleArgs,
        router
      });
    }
 
    Iif (viewPortPlan.childNavigationInstruction) {
      findActivatable(
        viewPortPlan.childNavigationInstruction,
        callbackName,
        list,
        viewPortInstruction.component.childRouter || router
      );
    }
  });
 
  return list;
}
 
function shouldContinue(output, router: Router) {
  Iif (output instanceof Error) {
    return false;
  }
 
  Iif (isNavigationCommand(output)) {
    if (typeof output.setRouter === 'function') {
      output.setRouter(router);
    }
 
    return !!output.shouldContinueProcessing;
  }
 
  Iif (output === undefined) {
    return true;
  }
 
  return output;
}
 
function processPotential(obj, resolve, reject) {
  if (obj && typeof obj.then === 'function') {
    return Promise.resolve(obj).then(resolve).catch(reject);
  }
 
  try {
    return resolve(obj);
  } catch (error) {
    return reject(error);
  }
}