All files / src/dom progress-watcher.ts

0% Statements 0/28
0% Branches 0/16
0% Functions 0/9
0% Lines 0/28

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 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                                                                                                                                                                                                                                                                                                                                           
import * as is from '../is/is';
import * as mathf from '../mathf/mathf';
 
export interface ProgressWatcherItem {
  range: number | number[];
  callback: Function;
  inactiveCallback?: Function;
  runWhen?: Function;
}
 
export enum Direction {
  Up = -1,
  Down = 1,
}
 
/**
 * A class that provides callbacks for progress points.
 *
 * ```
 * const progressWatcher = new ProgressWatcher();
 *
 *
 * // Triggers continously while runs from 0.2, 0.4
 *  progressWatcher.add({
 *    range: [0.2, 0.4],
 *    callback: (progress: number, direction: number)=> {
 *          console.log(progress, direction);
 *    }
 *
 *    // Runs when the progress is NOT 0.2 - 0.4
 *    inactiveCallback: (progress: number, direction: number)=> {
 *          console.log(progress, direction);
 *    }
 *  })
 *
 * // Triggers only when 0.2 is crossed in upwards or downwards direction.
 *  progressWatcher.add({
 *    range: 0.2,
 *    callback: (progress: number, direction: number)=> {
 *        if(direction === -1) {
 *          console.log('up');
 *        }
 *
 *        if(direction === 1) {
 *          console.log('down');
 *        }
 *    }
 *  })
 *
 *
 *  // RunWhen.  Add a condition to run.
 *  progressWatcher.add({
 *    range: [0.2, 0.4],
 *    callback: (progress: number, direction: number)=> {
 *          console.log(progress, direction);
 *    },
 *    runWhen: ()=> { return window.innerWidth < 1000 }
 *  })
 *
 *
 * // Remove watchFor
 * progressWatcher.remove(callback);
 *
 *
 * progressWatcher.update(0.2)
 *
 *
 * // If using with css parallaxer, you might do something
 * // like this.
 *  onRaf() {
 *       this.progressWatcher.setProgress(
 *           this.cssParallaxer.getProgress()
 *       );
 &  }
 *
 * ```
 *
 */
export class ProgressWatcher {
  private watchers: ProgressWatcherItem[] = [];
  private currentProgress = -1;
  private direction = 0;
 
  constructor() {}
 
  /**
   * Sets a callback for a specific range.
   * @param {number|number[]} A specific progress to watch for or
   *     an array like [0.1, 0.4] specifying the range to be watched.
   * @param {Function}
   */
  public add(progressWatchItem: ProgressWatcherItem): void {
    this.watchers.push(progressWatchItem);
  }
 
  /**
   * Removes a given set callback.
   * @param callback
   */
  public remove(callback: Function): void {
    this.watchers = this.watchers.filter((watcher: ProgressWatcherItem) => {
      return watcher.callback !== callback;
    });
  }
 
  /**
   * Updates the progress value.
   */
  public setProgress(progress: number): void {
    const previousProgress = this.currentProgress;
    this.currentProgress = progress;
    const direction = mathf.direction(previousProgress, this.currentProgress);
    if (direction === Direction.Up || direction === Direction.Down) {
      this.direction = direction;
    }
 
    // Loop through watchers.
    this.watchers.forEach((watcher: ProgressWatcherItem) => {
      let isBetween = false;
      if (is.array(watcher.range)) {
        isBetween = mathf.isBetween(
          this.currentProgress,
          (watcher.range as number[])[0],
          (watcher.range as number[])[1]
        );
      } else {
        // If we are only watching for a specific value, we used the
        // previous progress to see if we passed it.
        if (this.currentProgress !== previousProgress) {
          isBetween = mathf.isBetween(
            <number>watcher.range,
            this.currentProgress,
            previousProgress
          );
        }
      }
 
      if (isBetween) {
        if (watcher.runWhen && watcher.runWhen()) {
          watcher.callback(this.currentProgress, this.direction);
        } else {
          watcher.callback(this.currentProgress, this.direction);
        }
      } else {
        if (watcher.inactiveCallback) {
          watcher.inactiveCallback(this.currentProgress, this.direction);
        }
      }
    });
  }
 
  /**
   * Removes all watchers.
   */
  public clear() {
    this.watchers = [];
  }
 
  /**
   * Disposes progress watcher.
   */
  public dispose() {
    this.watchers = [];
  }
}