function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

/**
 * Simple listeneable data Emitter.
 * The implementation is adopted from `@dr.pogodin/react-utils`.
 * Docs: https://dr.pogodin.studio/docs/react-utils/docs/api/classes/Emitter
 */
export default class Emitter {
  /**
   * Creates a new Emitter.
   */
  constructor() {
    _defineProperty(this, "listeners", void 0);

    this.listeners = [];
  }
  /**
   * Returns "true" if any listener is connected; "false" otherwise.
   * @return {boolean}
   */


  get hasListeners() {
    return !!this.listeners.length;
  }
  /**
   * Adds `listener` if it is not already connected.
   * @param {function} listener
   * @return {function} Unsubscribe function.
   */


  addListener(listener) {
    if (!this.listeners.includes(listener)) {
      this.listeners.push(listener);
    }

    return () => this.removeListener(listener);
  }
  /**
   * Calls every connected listener with the given arguments.
   * @param  {...any} args
   */


  emit() {
    const {
      listeners
    } = this;

    for (let i = 0; i < listeners.length; ++i) {
      listeners[i](...arguments);
    }
  }
  /**
   * Removes specified `listener`, if connected.
   * @param {function} listener
   */


  removeListener(listener) {
    const idx = this.listeners.indexOf(listener);
    if (idx >= 0) this.listeners.splice(idx, 1);
  }

}
//# sourceMappingURL=Emitter.js.map