UNPKG

zpubsub

Version:

A javascript implementation of a publish/subscribe pattern.

355 lines (343 loc) 13.1 kB
(function webpackUniversalModuleDefinition(root, factory) { if(typeof exports === 'object' && typeof module === 'object') module.exports = factory(); else if(typeof define === 'function' && define.amd) define([], factory); else if(typeof exports === 'object') exports["zpubsub"] = factory(); else root["zpubsub"] = factory(); })(this, function() { return /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) { /******/ return installedModules[moduleId].exports; /******/ } /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ i: moduleId, /******/ l: false, /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ /******/ // Flag the module as loaded /******/ module.l = true; /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /******/ /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ /******/ // define getter function for harmony exports /******/ __webpack_require__.d = function(exports, name, getter) { /******/ if(!__webpack_require__.o(exports, name)) { /******/ Object.defineProperty(exports, name, { /******/ configurable: false, /******/ enumerable: true, /******/ get: getter /******/ }); /******/ } /******/ }; /******/ /******/ // getDefaultExport function for compatibility with non-harmony modules /******/ __webpack_require__.n = function(module) { /******/ var getter = module && module.__esModule ? /******/ function getDefault() { return module['default']; } : /******/ function getModuleExports() { return module; }; /******/ __webpack_require__.d(getter, 'a', getter); /******/ return getter; /******/ }; /******/ /******/ // Object.prototype.hasOwnProperty.call /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; /******/ /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ /******/ // Load entry module and return exports /******/ return __webpack_require__(__webpack_require__.s = 1); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * Represents an implementation of the IZPubSubEventObject. * * @this {ZPubSubEventObject} */ var ZPubSubEventObject = (function () { /** * Initializes a new instance of this object. * * @param {Object} owner The owner of the event. * @param {Function} callback The callback for when the event is invoked. */ function ZPubSubEventObject(owner, callback) { this.owner = owner; this.callback = callback; } return ZPubSubEventObject; }()); exports.ZPubSubEventObject = ZPubSubEventObject; /***/ }), /* 1 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function __export(m) { for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; } Object.defineProperty(exports, "__esModule", { value: true }); __export(__webpack_require__(2)); __export(__webpack_require__(0)); /***/ }), /* 2 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var zpubsub_event_1 = __webpack_require__(0); /** * Represents an implementation of the IZPubSub contract. * * @this {ZPubSub} */ var ZPubSub = (function () { /** * Initializes a new instance of this object. */ function ZPubSub() { this.subMap = {}; } /** * Publishes an event. * * The argument list beyond the message is passed through. * * @param {String} topic This is the string message that * represents the event id. * @param {Array} args The optional arguments to the callback. * * @returns {Array} The list of return values. This list can contain undefined and * null values. If there are no callbacks, then you will receive an * empty array. If you need just the first value of the array, use * yell instead. */ ZPubSub.prototype.publish = function (topic) { var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } if (!topic) { throw new Error('The topic to publish was not supplied.'); } var callbacks = this._getSubscription(topic); var results = []; for (var _a = 0, callbacks_1 = callbacks; _a < callbacks_1.length; _a++) { var store = callbacks_1[_a]; results.push(store.callback.apply(store.owner, args)); } return results; }; /** * Publishes the message and arguments and returns the first * defined response, if any. * * @param {String} topic The message to publish. * * @return {Object} The first defined response to the publish message. * Returns null if nobody responds. */ ZPubSub.prototype.yell = function (topic) { var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } if (!topic) { throw new Error('The topic to yell was not supplied.'); } var argsToApply = [topic].concat(args); var results = this.publish.apply(this, argsToApply); var definedElements = results.filter(function (x) { return x !== null && x !== undefined; }); return definedElements.length === 0 ? null : definedElements[0]; }; /** * Subscribes to an event. * * @param {String} topic The id of the event to subscribe to. * @param {Object} owner The object that owns the subscription. * @param {Function} callback The callback to invoke when the event is raised. This callback * will be invoked with 3 arguments. The first is the data that * gets passed to the publish method, and the 2nd is the owner of the * message callback, and the 3rd argument is the message itself.. * * @return {Object} This method returns an object that contains two properties: * 1. owner: The passed owner object. * 2. callback: The callback that will be invoked when msg is published. */ ZPubSub.prototype.subscribe = function (topic, owner, callback) { if (!topic) { throw new Error('The topic to subscribe to was not supplied.'); } if (!owner) { throw new Error('The owner for the subscription was not supplied.'); } if (typeof callback !== 'function') { var msg = [ 'The callback function for the topic was not defined or not a function.', 'Did you forget to pass the owner?' ].join(' '); throw new Error(msg); } var store = this._getSubscription(topic); var event = new zpubsub_event_1.ZPubSubEventObject(owner, callback); store.push(event); return event; }; /** * Removes a subscription from the callback list. * * @param {String} topic The id of the message to remove. * @param {Object} owner The object that owns the subscription. * @param {Function} callback The callback that was registered in the subscribe method. * * @returns {Boolean} True if the subscription list was modified, false otherwise. */ ZPubSub.prototype.unsubscribe = function (topic, owner, callback) { if (!topic) { throw new Error('The topic to unsubscribe from was not supplied.'); } if (!owner) { throw new Error('The owner that owns the topic to unsubscribe from was not supplied.'); } if (typeof callback !== 'function') { throw new Error('The specific callback for the topic and owner pair to unsubscribe from was not supplied.'); } var callbacks = this._getSubscription(topic); var modified = false; for (var i = callbacks.length - 1; i >= 0; i -= 1) { var current = callbacks[i]; if (current.owner === owner && current.callback === callback) { callbacks.splice(i, 1); modified = true; } } return modified; }; /** * Removes all subscriptions from an owner. * * @param {type} owner The object to remove all subscriptions for. * * @returns {boolean} True if the subscription list was modified, false otherwise. */ ZPubSub.prototype.unsubscribeAll = function (owner) { if (!owner) { throw new Error('The owner to remove all subscriptions for was not supplied.'); } var modified = false; for (var property in this.subMap) { var value = this.subMap[property]; for (var index = value.length - 1; index >= 0; index = index - 1) { var currentCallback = value[index]; if (currentCallback.owner === owner) { value.splice(index, 1); modified = true; } } } return modified; }; /** * Registers a series of method objects on the service * for the given topic. * * This method creates convinence methods for a given topic * name. * * You will get the following methods on this service by calling * this function: * 1. publish{topic}(args) => shortcut to publish(topic, args); * 2. subscribe{topic}(owner, callback) => shortcut to subscribe(topic, owner, callback); * 3. unsubscribe{topic}(owner, callback) => shortcut to unsubscribe(topic, owner, callback); * * It's good practice to make sure that the topic name is javascript * friendly. * * @param {String} topic The sur name of the convinence function. */ ZPubSub.prototype.register = function (topic) { if (!topic) { throw new Error('The topic to register was not supplied.'); } var publishName = 'publish' + topic; var yellName = 'yell' + topic; var subscribeName = 'subscribe' + topic; var unsubscribeName = 'unsubscribe' + topic; this[publishName] = this.publish.bind(this, topic); this[yellName] = this.yell.bind(this, topic); this[subscribeName] = this.subscribe.bind(this, topic); this[unsubscribeName] = this.unsubscribe.bind(this, topic); }; /** * Removes the convinence methods created by register. * * @param {String} topic The topic to deregister. */ ZPubSub.prototype.deregister = function (topic) { if (!topic) { throw new Error('The topic to deregister was not supplied.'); } var publishName = 'publish' + topic; var yellName = 'yell' + topic; var subscribeName = 'subscribe' + topic; var unsubscribeName = 'unsubscribe' + topic; if (this.hasOwnProperty(publishName)) { delete this[publishName]; } if (this.hasOwnProperty(yellName)) { delete this[yellName]; } if (this.hasOwnProperty(subscribeName)) { delete this[subscribeName]; } if (this.hasOwnProperty(unsubscribeName)) { delete this[unsubscribeName]; } }; /** * Gets the subscription list for the specified topic. * * @param {String} topic The topic to retrieve the list for. * * @return {Array<ZPubSubEventObject>} The list of events for the topic. */ ZPubSub.prototype._getSubscription = function (topic) { if (!this.subMap.hasOwnProperty(topic)) { this.subMap[topic] = []; } return this.subMap[topic]; }; return ZPubSub; }()); exports.ZPubSub = ZPubSub; /***/ }) /******/ ]); }); //# sourceMappingURL=zpubsub.js.map