All files / patterns/observer/src observer.ts

100% Statements 7/7
100% Branches 0/0
100% Functions 5/5
100% Lines 6/6

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                    1x 7x     6x       2x   2x       1x      
export interface ObservableInterface {
    attach(observer: ObserverInterface): void;
    detach(observer: ObserverInterface): void;
    notify(message: string): void;
}
 
export interface ObserverInterface {
    update(subject: ObservableInterface, message: string): void;
}
 
export abstract class Observable implements ObservableInterface {
    protected observers: ObserverInterface[] = [];
 
    public attach(observer: ObserverInterface): number {
        return this.observers.push(observer);
    }
 
    public detach(observer: ObserverInterface): ObserverInterface[] {
        const observerIndex = this.observers.indexOf(observer);
 
        return this.observers.splice(observerIndex, 1);
    }
 
    public notify(message: string): void {
        return this.observers.forEach((observer): void => observer.update(this, message));
    }
}