UNPKG

dissemination

Version:

Lightweight event/command library created to replace Backbone.Radio

145 lines (116 loc) 4.12 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global = global || self, global.dissemination = factory()); }(this, function () { 'use strict'; var EventMixin = { on: function on(name, listener) { var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; var once = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false; if (!this._listeners) { this._listeners = {}; } if (!this._listeners[name]) { this._listeners[name] = []; } this._listeners[name].push({ listener: listener, options: options, once: !!once }); }, once: function once(name, listener, options) { this.on(name, listener, options, true); }, off: function off(name, listener) { if (!this.listenersRegistered(name)) { throw new TypeError("Listeners for event \"".concat(name, "\" are not registered.")); } if (listener === undefined) { // Removing all listeners for a given event delete this._listeners[name]; return; } var index = this._listeners[name].findIndex(function (item) { return item.listener === listener; }); if (index === -1) { throw new TypeError("Specified listener for event \"".concat(name, "\" is not registered.")); } this._listeners[name].splice(index, 1); if (this._listeners[name].length === 0) { delete this._listeners[name]; } }, fire: function fire(name) { var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; if (!this._listeners || !this._listeners[name]) { return; } var listeners = this._listeners[name]; for (var i = 0; i < listeners.length; i++) { var item = listeners[i]; var listener = item.listener, options = item.options, _item$once = item.once, once = _item$once === void 0 ? false : _item$once; if (once) { listeners.splice(i, 1); i--; } if (listener(params, options) === false) { break; } } }, listenersRegistered: function listenersRegistered(name) { return this._listeners && !!this._listeners[name]; } }; var CommandMixin = { handle: function handle(name, handler) { if (!this._handlers) { this._handlers = {}; } if (this._handlers[name]) { throw new TypeError("Another handler for command \"".concat(name, "\" is already registered.")); } this._handlers[name] = handler; }, unhandle: function unhandle(name) { if (!this.handlerRegistered(name)) { throw new TypeError("Handler for command \"".concat(name, "\" is not registered.")); } delete this._handlers[name]; }, request: function request(name, options) { if (!this.handlerRegistered(name)) { throw new TypeError("Handler for command \"".concat(name, "\" is not registered.")); } return this._handlers[name](options || {}); }, execute: function execute(name, options) { this.request(name, options); }, handlerRegistered: function handlerRegistered(name) { return this._handlers && !!this._handlers[name]; } }; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var Channel = function Channel() { _classCallCheck(this, Channel); }; Object.assign(Channel.prototype, EventMixin, CommandMixin); var channels = {}; function getChannel(name) { name = name || 'application'; if (!channels[name]) { channels[name] = new Channel(); } return channels[name]; } getChannel.Channel = Channel; getChannel.EventMixin = EventMixin; getChannel.CommandMixin = CommandMixin; return getChannel; }));