import {Observable} from "./observable";

export class Subject extends Observable{
    _observers = [];
    constructor(){
        super((observer=>{
            this._observers.push(observer);

            // Return a cleanup function which will unsubscribe the observer
            return ()=>{
                this._observers = this._observers.filter(obs=>obs!==observer);
            }
        }));
        this._observers = [];
    }

    close(){
        for(let obs of this._observers){
            obs.complete();
        }
    }

    next(newValue){
        for(let obs of this._observers){
            obs.next(newValue);
        }
    }
}

//if(window && !window.ValueSubject) window.ValueSubject = ValueSubject;