export class EventBus<T>
{
  private value:        T
  private subscribers:  ((val: T) => void)[]  = []

  constructor (
      initialValue: T)
  {
    this.value = initialValue
  }

  get ()
  : T
  {
    return this.value
  }

  set (
      val:  T)
  : void
  {
    this.value = val
    this.subscribers.forEach (f => f (val))
  }

  subscribe (
      func: (val: T) => void)
  : () => void
  {
    this.subscribers.push (func)
    return () => {
      this.subscribers = this.subscribers.filter (sub => sub !== func)
    }
  }
}
