UNPKG

kitchensink

Version:

Dispatch's awesome components and style guide

67 lines (53 loc) 1.6 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var StyleKeeper = function () { function StyleKeeper(userAgent) { _classCallCheck(this, StyleKeeper); this._userAgent = userAgent; this._listeners = []; this._cssSet = {}; } StyleKeeper.prototype.subscribe = function subscribe(listener) { var _this = this; if (this._listeners.indexOf(listener) === -1) { this._listeners.push(listener); } return { // Must be fat arrow to capture `this` remove: function remove() { var listenerIndex = _this._listeners.indexOf(listener); if (listenerIndex > -1) { _this._listeners.splice(listenerIndex, 1); } } }; }; StyleKeeper.prototype.addCSS = function addCSS(css) { var _this2 = this; if (!this._cssSet[css]) { this._cssSet[css] = true; this._emitChange(); } return { // Must be fat arrow to capture `this` remove: function remove() { delete _this2._cssSet[css]; _this2._emitChange(); } }; }; StyleKeeper.prototype.getCSS = function getCSS() { return Object.keys(this._cssSet).join('\n'); }; StyleKeeper.prototype._emitChange = function _emitChange() { this._listeners.forEach(function (listener) { return listener(); }); }; return StyleKeeper; }(); exports.default = StyleKeeper; module.exports = exports['default'];