UNPKG

refraction

Version:

A guard that represent central point of control in your application.

286 lines (227 loc) 9.18 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["Refraction"] = factory(); else root["Refraction"] = 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] = { /******/ exports: {}, /******/ id: moduleId, /******/ loaded: false /******/ }; /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ // Flag the module as loaded /******/ module.loaded = 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; /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ // Load entry module and return exports /******/ return __webpack_require__(0); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ function(module, exports, __webpack_require__) { 'use strict'; exports.__esModule = true; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _Mediator = __webpack_require__(2); var _Mediator2 = _interopRequireDefault(_Mediator); var _History = __webpack_require__(1); var _History2 = _interopRequireDefault(_History); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var getSubscriberLevel = function getSubscriberLevel(subscriber) { return subscriber.refractionLevel || 100; }; var compareFunction = function compareFunction(a, b) { var result = 0; var aPriority = getSubscriberLevel(a); var bPriority = getSubscriberLevel(b); if (aPriority < bPriority) { result = -1; } else if (aPriority > bPriority) { result = 1; } return result; }; var Refraction = function () { function Refraction() { var middlewares = arguments.length <= 0 || arguments[0] === undefined ? [] : arguments[0]; _classCallCheck(this, Refraction); _initialiseProps.call(this); this.applyMiddleware.apply(this, _toConsumableArray(middlewares).concat([this.addToHistory])); } _createClass(Refraction, [{ key: 'subscribe', value: function subscribe(subscriber) { this.mediator.subscribe(subscriber, compareFunction); return this.unsubscribe.bind(this, subscriber); } }, { key: 'unsubscribe', value: function unsubscribe(subscriber) { this.mediator.unsubscribe(subscriber); } }, { key: 'publish', value: function publish(channel, param) { var newParam = param; this.middlewares.forEach(function (middleware) { newParam = middleware(channel, newParam); }); this.mediator.publish(channel, newParam); } }, { key: 'setHistoryLimit', value: function setHistoryLimit(limit) { this.history.setLimit(limit); } }, { key: 'clearHistory', value: function clearHistory() { this.history.clear(); } }, { key: 'getHistory', value: function getHistory() { return this.history.get(); } }]); return Refraction; }(); var _initialiseProps = function _initialiseProps() { var _this = this; this.middlewares = []; this.mediator = new _Mediator2['default'](); this.history = new _History2['default'](); this.applyMiddleware = function () { for (var _len = arguments.length, middlewares = Array(_len), _key = 0; _key < _len; _key++) { middlewares[_key] = arguments[_key]; } if (middlewares.length === 1) { _this.middlewares.push(middlewares[0]); } else if (middlewares.length > 0) { middlewares.forEach(function (middleware) { return _this.applyMiddleware(middleware); }); } }; this.addToHistory = function (channel, param) { _this.history.add({ channel: channel, time: performance && performance.now ? performance.now() : null, param: param }); return param; }; }; exports['default'] = Refraction; /***/ }, /* 1 */ /***/ function(module, exports) { "use strict"; exports.__esModule = true; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var History = function () { function History(limit) { _classCallCheck(this, History); this.items = []; this.limit = 200; this.setLimit(limit); } _createClass(History, [{ key: "setLimit", value: function setLimit(limit) { this.limit = Number(limit) > 0 ? Number(limit) : this.limit; } }, { key: "add", value: function add(payload) { this.items.push(payload); if (this.items.length > this.limit) { this.items = this.items.slice(this.items.length - this.limit); } } }, { key: "get", value: function get() { return this.items; } }, { key: "clear", value: function clear() { this.items = []; } }]); return History; }(); exports["default"] = History; /***/ }, /* 2 */ /***/ function(module, exports) { "use strict"; exports.__esModule = true; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var Mediator = function () { function Mediator() { var _this = this; _classCallCheck(this, Mediator); this.subscribers = []; this.publish = function (channel, param) { _this.subscribers.forEach(function (subscriber) { if (!subscriber) { _this.unsubscribe(subscriber); } else if (subscriber[channel]) { subscriber[channel](param); } }); }; } _createClass(Mediator, [{ key: "subscribe", value: function subscribe(subscriber, compareFunction) { if (this.subscribers.indexOf(subscriber) === -1) { this.subscribers.push(subscriber); this.subscribers.sort(compareFunction); } } }, { key: "unsubscribe", value: function unsubscribe(subscriber) { var index = this.subscribers.indexOf(subscriber); if (index !== -1) { this.subscribers.splice(index, 1); } } }]); return Mediator; }(); exports["default"] = Mediator; /***/ } /******/ ]) }); ;