UNPKG

1.6 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
8
9var StyleKeeper = function () {
10 function StyleKeeper(userAgent) {
11 _classCallCheck(this, StyleKeeper);
12
13 this._userAgent = userAgent;
14 this._listeners = [];
15 this._cssSet = {};
16 }
17
18 StyleKeeper.prototype.subscribe = function subscribe(listener) {
19 var _this = this;
20
21 if (this._listeners.indexOf(listener) === -1) {
22 this._listeners.push(listener);
23 }
24
25 return {
26 // Must be fat arrow to capture `this`
27 remove: function remove() {
28 var listenerIndex = _this._listeners.indexOf(listener);
29 if (listenerIndex > -1) {
30 _this._listeners.splice(listenerIndex, 1);
31 }
32 }
33 };
34 };
35
36 StyleKeeper.prototype.addCSS = function addCSS(css) {
37 var _this2 = this;
38
39 if (!this._cssSet[css]) {
40 this._cssSet[css] = true;
41 this._emitChange();
42 }
43
44 return {
45 // Must be fat arrow to capture `this`
46 remove: function remove() {
47 delete _this2._cssSet[css];
48 _this2._emitChange();
49 }
50 };
51 };
52
53 StyleKeeper.prototype.getCSS = function getCSS() {
54 return Object.keys(this._cssSet).join('\n');
55 };
56
57 StyleKeeper.prototype._emitChange = function _emitChange() {
58 this._listeners.forEach(function (listener) {
59 return listener();
60 });
61 };
62
63 return StyleKeeper;
64}();
65
66exports.default = StyleKeeper;
67module.exports = exports['default'];
\No newline at end of file