import { ReplaySubject, Observable, BehaviorSubject } from 'rxjs';

export class Event<T> {
   private source: ReplaySubject<T>;

   observable: Observable<T>;

   snapshot: T = undefined;

   imutable = false;

   constructor(initial?: any, imutable = false) {
      this.imutable = imutable;
      this.source = new ReplaySubject(1);
      this.snapshot = initial;
      this.observable = this.source.asObservable();

      if (initial) {
         this.source.next(initial);
      }
   }

   set(data: T): void {
      if (this.imutable) {
         if (this.snapshot !== data) {
            this.snapshot = data;
            this.source.next(data);
         }
      } else {
         this.snapshot = data;
         this.source.next(data);
      }
   }
}
