unstated-enhancers
Version:
Unstated tools for state management
69 lines (68 loc) • 2.38 kB
JavaScript
;
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var Counter = /** @class */ (function () {
function Counter() {
this.counters = {};
this.collapsed = true;
this.debounce = 200;
}
Counter.prototype.__sum = function (total, num) {
return total + num;
};
Counter.prototype.config = function (config) {
for (var _i = 0, _a = Object.entries(config); _i < _a.length; _i++) {
var _b = _a[_i], key = _b[0], value = _b[1];
this[key] = value;
}
};
Counter.prototype.count = function (component) {
var name = typeof component === 'string' ? component : component.constructor.name;
if (!this.counters[name]) {
this.counters[name] = 0;
}
this.counters[name] += 1;
this.__log();
};
Counter.prototype.__log = function () {
var _this = this;
if (this.debounceId) {
clearTimeout(this.debounceId);
}
this.debounceId = setTimeout(function () {
_this.__print();
}, this.debounce);
};
Counter.prototype.__print = function () {
var _this = this;
var titleStyles = ['color: black; font-weight: bold;'];
var group = this.collapsed ? console.groupCollapsed : console.group;
var counter = Object.values(this.counters).reduce(this.__sum);
var info = counter + " COMPONENT UPDATES";
group.apply(void 0, __spreadArrays(["%c " + info], titleStyles));
var sorted = {};
Object.keys(this.counters)
.sort(function (a, b) {
if (a > b) {
return 1;
}
if (a < b) {
return -1;
}
return 0;
}).forEach(function (name) {
console.log(name + " " + _this.counters[name]);
sorted[name] = _this.counters[name];
});
console.groupEnd();
this.counters = {};
};
return Counter;
}());
exports.default = Counter;