UNPKG

react-themable-hoc

Version:

React higher-order-components that allow for css-in-js-style themes.

110 lines (94 loc) 3.6 kB
(function (global, factory) { if (typeof define === "function" && define.amd) { define(["exports"], factory); } else if (typeof exports !== "undefined") { factory(exports); } else { var mod = { exports: {} }; factory(mod.exports); global.ThemeEvents = mod.exports; } })(this, function (exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); 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 _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 ThemeEvents = function () { function ThemeEvents() { _classCallCheck(this, ThemeEvents); } _createClass(ThemeEvents, null, [{ key: "subscribe", value: function subscribe(event, listener) { var _this = this; var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; if (event && !listener) { // Partial application - returns function that takes a listener and options // for this event type return function (l, o) { return _this.subscribe(event, l, o); }; } if (!this.listeners[event]) { this.listeners[event] = []; } this.listeners[event].push(listener); return function () { return _this.unsubscribe(event, listener); }; } }, { key: "unsubscribe", value: function unsubscribe(event, listener) { var listenerIndex = this.listeners[event].indexOf(listener); if (listenerIndex < 0) return; this.listeners[event] = [].concat(_toConsumableArray(this.listeners[event].slice(0, listenerIndex)), _toConsumableArray(this.listeners[event].slice(listenerIndex + 1))); } }, { key: "publish", value: function publish(event, payload) { if (this.listeners[event]) { this.listeners[event].forEach(function (callback) { return callback(payload); }); } } }]); return ThemeEvents; }(); ThemeEvents.listeners = {}; exports.default = ThemeEvents; });