UNPKG

refraction

Version:

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

119 lines (96 loc) 3.83 kB
'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 = require('./Mediator'); var _Mediator2 = _interopRequireDefault(_Mediator); var _History = require('./History'); 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;