Code coverage report for src/activation.js

Statements: 74.11% (83 / 112)      Branches: 63.51% (47 / 74)      Functions: 71.43% (20 / 28)      Lines: 75% (81 / 108)      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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 2571 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                                   1 17 2     15                                               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;
}
 
/**
 * A basic interface for an Observable type
 */
interface IObservable {
  subscribe(): ISubscription;
}
 
/**
 * A basic interface for a Subscription to an Observable
 */
interface ISubscription {
  unsubscribe(): void;
}
 
type SafeSubscriptionFunc = (sub: SafeSubscription) => ISubscription;
 
/**
 * wraps a subscription, allowing unsubscribe calls even if
 * the first value comes synchronously
 */
class SafeSubscription {
  constructor(subscriptionFunc: SafeSubscriptionFunc) {
    this._subscribed = true;
    this._subscription = subscriptionFunc(this);
 
    if (!this._subscribed) this.unsubscribe();
  }
 
  get subscribed(): boolean {
    return this._subscribed;
  }
 
  unsubscribe(): void {
    if (this._subscribed && this._subscription) this._subscription.unsubscribe();
 
    this._subscribed = false;
  }
}
 
function processPotential(obj, resolve, reject) {
  if (obj && typeof obj.then === 'function') {
    return Promise.resolve(obj).then(resolve).catch(reject);
  }
 
  Iif (obj && typeof obj.subscribe === 'function') {
    let obs: IObservable = obj;
    return new SafeSubscription(sub => obs.subscribe({
      next() {
        if (sub.subscribed) {
          sub.unsubscribe();
          resolve(obj);
        }
      },
      error(error) {
        if (sub.subscribed) {
          sub.unsubscribe();
          reject(error);
        }
      },
      complete() {
        if (sub.subscribed) {
          sub.unsubscribe();
          resolve(obj);
        }
      }
    }));
  }
 
  try {
    return resolve(obj);
  } catch (error) {
    return reject(error);
  }
}