All files flowzilla.ts

100% Statements 46/46
93.33% Branches 28/30
100% Functions 12/12
100% Lines 42/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 1024x 4x   4x   4x 1x     4x   16x 16x   32x 16x   4x 5x   37x   5x   24x   22x 22x   1x   21x 21x     4x 2x 2x   4x 2x 2x   2x 2x                           45x           45x   1x     6x 12x       6x     38x       38x     38x         37x 37x 37x         1x       4x  
import { insertToArray } from './utils';
import { NAME, POSITION } from './constants';
import { ISkill, IOptions, IPosition, ITracker, ICallbacks } from './types';
import { dispatch } from './dispatch';
 
export function createFlowzilla<T1 = any>(name?: string) {
  return new Flowzilla<T1>(name);
}
 
export class Flowzilla<T1 = any> {
  public name: string;
  private $skillSet: any = {};
  private skills: ISkill<any>[] = [];
  tracker?: ITracker;
  constructor(Ename = 'chappie') {
    this.name = name;
  }
  public numberOfSkills() {
    return this.skills.length;
  }
  private appendTracker(options: IOptions = {}) {
    if (this.tracker && !options.tracker) {
      options.tracker = this.tracker;
    }
    return options;
  }
  run<T>(type: string, value?: T1, options?: IOptions, callback?: ICallbacks) {
    options = this.appendTracker(options);
    if (callback) {
      return dispatch(callback, this.skills, type, value, options);
    }
    return new Promise<T>((yay, nay) => {
      return dispatch([nay, yay], this.skills, type, value, options);
    });
  }
  runSync(type: string, value?: T1, options?: IOptions) {
    options = this.appendTracker(options);
    return dispatch(undefined, this.skills, type, value, options);
  }
  removeSkill(skill: string | ISkill) {
    skill = typeof skill === 'string' ? this.$skillSet[skill] : skill;
    const index = this.skills.indexOf(skill as any);
    if (index >= 0) {
      delete this.$skillSet[skill as any];
      this.skills.splice(this.skills.indexOf(skill as any), 1);
    }
  }
  addSkill<T = any>(
    skill: ISkill<T> | ISkill<T>[],
    position?: IPosition,
    otherSkill?: ISkill<any> | ISkill<T>[] | string | string[]
  ): void;
  addSkill<T = any>(
    name: string,
    skill: ISkill<T> | ISkill<T>[],
    position?: IPosition,
    otherSkill?: ISkill<any> | ISkill<T>[] | string | string[]
  ): void;
  addSkill<T = any>(
    n: string | ISkill<T> | ISkill<T>[] | undefined,
    s?: ISkill<T> | ISkill<T>[] | IPosition,
    p?: IPosition | ISkill<any> | ISkill<T>[],
    o?: ISkill<any> | ISkill<T>[] | string | string[]
  ): void {
    const skill = (typeof n !== 'string' ? n : s) as ISkill<T> | ISkill<T>[];
    if (!skill) {
      throw new Error('Please provide skill or name+skill');
    }
    if (Array.isArray(skill)) {
      skill.forEach(skill =>
        typeof n === 'string'
          ? this.addSkill(n as any, skill as any, p as any, o as any)
          : this.addSkill(skill as any, s as any, p as any)
      );
      return;
    }
    const name =
      (typeof n !== 'string' ? undefined : n) ||
      skill.name ||
      skill[NAME] ||
      `skill${this.skills.length}`;
    const otherSkill = ((typeof n !== 'string' ? p : o) ||
      skill['skills'] ||
      []) as ISkill<any> | ISkill<T>[] | string | string[];
    const position = ((typeof n !== 'string' ? s : p) ||
      skill[POSITION] ||
      'END') as IPosition;
    // Is already learned?
    if (this.skills.indexOf(skill) === -1 && !this.$skillSet[name]) {
      skill[NAME] = name;
      this.$skillSet[name] = skill;
      this.skills = insertToArray(
        this.skills,
        skill,
        position,
        otherSkill,
        s => this.$skillSet[s]
      );
    }
  }
}