(function (global) {
  global.ARQ = global.ARQ || {};
  var listeners = {};
  ARQ.bus = {
    on: function (ev, fn) {
      if (!listeners[ev]) listeners[ev] = [];
      listeners[ev].push(fn);
      return function () { ARQ.bus.off(ev, fn); };
    },
    off: function (ev, fn) {
      if (!listeners[ev]) return;
      listeners[ev] = listeners[ev].filter(function (f) { return f !== fn; });
    },
    emit: function (ev, payload) {
      (listeners[ev] || []).forEach(function (fn) {
        try { fn(payload); } catch (e) { console.error(e); }
      });
    },
  };
})(typeof window !== 'undefined' ? window : this);
