export default class Subscriber {
  constructor (disposeHandler) {
    this.disposeHandler = disposeHandler
    this.emitHandlers = []
    this.errorHandlers = []
  }

  subscribe (forEmit, forError) {
    if (forEmit) {
      this.emitHandlers.push(forEmit)
    }
    if (forError) {
      this.errorHandlers.push(forError)
    }
    return this
  }

  emit (err, payload) {
    if (err) {
      this.errorHandlers.map(cb => cb(err))
    }
    if (payload) {
      this.emitHandlers.map(cb => cb(payload))
    }
    return this
  }

  dispose () {
    this.disposeHandler()
    delete this.emitHandlers
    delete this.errorHandlers
    return this
  }
}

export default function Subscriber (disposeHandler) {
  const emitHandlers = []
  const errorHandlers = []

  this.subscribe = (forEmit, forError) => {
    if (forEmit) {
      emitHandlers.push(forEmit)
    }
    if (forError) {
      errorHandlers.push(forError)
    }
    return this
  }

  this.emit = (err, payload) => {
    if (err) {
      errorHandlers.map(cb => cb(err))
    }
    if (payload) {
      emitHandlers.map(cb => cb(payload))
    }
    return this
  }

  this.dispose = () => {
    disposeHandler()
    emitHandlers.splice(0)
    errorHandlers.splice(0)
    return this
  }
}

fix subscriber class to have privates and use function