KnobStore.js
2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
require("core-js/modules/es6.object.define-property");
require("core-js/modules/es6.array.index-of");
require("core-js/modules/es6.array.iterator");
require("core-js/modules/es6.object.keys");
require("core-js/modules/web.dom.iterable");
require("core-js/modules/es6.array.for-each");
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
var callArg = function callArg(fn) {
return fn();
};
var callAll = function callAll(fns) {
return fns.forEach(callArg);
};
var KnobStore =
/*#__PURE__*/
function () {
function KnobStore() {
_classCallCheck(this, KnobStore);
this.store = {};
this.callbacks = [];
}
_createClass(KnobStore, [{
key: "has",
value: function has(key) {
return this.store[key] !== undefined;
}
}, {
key: "set",
value: function set(key, value) {
this.store[key] = value;
this.store[key].used = true;
this.store[key].groupId = value.groupId; // debounce the execution of the callbacks for 50 milliseconds
if (this.timer) {
clearTimeout(this.timer);
}
this.timer = setTimeout(callAll, 50, this.callbacks);
}
}, {
key: "get",
value: function get(key) {
var knob = this.store[key];
if (knob) {
knob.used = true;
}
return knob;
}
}, {
key: "getAll",
value: function getAll() {
return this.store;
}
}, {
key: "reset",
value: function reset() {
this.store = {};
}
}, {
key: "markAllUnused",
value: function markAllUnused() {
var _this = this;
Object.keys(this.store).forEach(function (knobName) {
_this.store[knobName].used = false;
});
}
}, {
key: "subscribe",
value: function subscribe(cb) {
this.callbacks.push(cb);
}
}, {
key: "unsubscribe",
value: function unsubscribe(cb) {
var index = this.callbacks.indexOf(cb);
this.callbacks.splice(index, 1);
}
}]);
return KnobStore;
}();
exports.default = KnobStore;