UNPKG

refraction

Version:

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

51 lines (41 loc) 1.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: 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;