UNPKG

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