All files / utils/observer Observer.ts

100% Statements 10/10
66.67% Branches 2/3
100% Functions 4/4
100% Lines 10/10

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    3x   280x             280x         282x         1x   1x   1x           64x   64x   65x        
import Subscriber from './Subscriber'
 
export default class Observer {
 
  private _subscribers: Subscriber[] = [];
 
  constructor(
 
    /**
     * The name of the callback the subscriber is using when notified
     */
    public callbackName: string = 'onNotify'
  ) { }
 
  subscribe(subscriber: Subscriber) {
 
    this._subscribers.push(subscriber);
  }
 
  unsubscribe(subscriber: Subscriber) {
 
    const index = this._subscribers.indexOf(subscriber);
 
    Eif (index > -1) {
 
      this._subscribers.splice(index, 1);
    }
  }
 
  notify(...args: any[]) {
 
    args.push(this); // Append the observer to the list of arguments to make it accessable to the subscriber
 
    for (let subscriber of this._subscribers) {
 
      (subscriber as any)[this.callbackName](...args);
    }
  }
}