Code coverage report for src/scheduler/FutureAction.ts

Statements: 91.3% (42 / 46)      Branches: 71.43% (10 / 14)      Functions: 100% (7 / 7)      Lines: 90% (36 / 40)      Ignored: none     

All files » src/scheduler/ » FutureAction.ts
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 721     1   1           55 55 55     1 147     147     281 154     154     27   27 27 27   27         27 22 22 22 22     27     1   50 50 50   50 5 5     50 2     50 50 50   1  
import {root} from '../util/root';
import {Action} from './Action';
import {Scheduler} from '../Scheduler';
import {Subscription} from '../Subscription';
 
export class FutureAction<T> extends Subscription implements Action {
 
  public id: any;
  public state: any;
  public delay: number;
 
  constructor(public scheduler: Scheduler,
              public work: (x?: any) => Subscription | void) {
    super();
  }
 
  execute() {
    Iif (this.isUnsubscribed) {
      throw new Error('How did did we execute a canceled Action?');
    }
    this.work(this.state);
  }
 
  schedule(state?: any, delay: number = 0): Action {
    Iif (this.isUnsubscribed) {
      return this;
    }
    return this._schedule(state, delay);
  }
 
  protected _schedule(state?: any, Idelay: number = 0): Action {
 
    this.delay = delay;
    this.state = state;
    const id = this.id;
 
    Iif (id != null) {
      this.id = undefined;
      root.clearTimeout(id);
    }
 
    this.id = root.setTimeout(() => {
      this.id = null;
      const {scheduler} = this;
      scheduler.actions.push(this);
      scheduler.flush();
    }, delay);
 
    return this;
  }
 
  protected _unsubscribe() {
 
    const {id, scheduler} = this;
    const {actions} = scheduler;
    const index = actions.indexOf(this);
 
    if (id != null) {
      this.id = null;
      root.clearTimeout(id);
    }
 
    if (index !== -1) {
      actions.splice(index, 1);
    }
 
    this.work = null;
    this.state = null;
    this.scheduler = null;
  }
}