logger-timer
Version:
A quick shortcut to adding a bunch of timers and dumping their deltas.
153 lines (145 loc) • 5.34 kB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else if(typeof exports === 'object')
exports["censor-sensor"] = factory();
else
root["censor-sensor"] = factory();
})(typeof self !== 'undefined' ? self : this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
;
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
__export(__webpack_require__(1));
/***/ }),
/* 1 */
/***/ (function(module, exports, __webpack_require__) {
;
Object.defineProperty(exports, "__esModule", { value: true });
class LoggerTimer {
constructor(opts = { isActive: true, dumpThreshold: 0 }) {
this.timerStarts = {};
this.timerStops = {};
this.isActive = opts.isActive;
this.dumpThreshold = opts.dumpThreshold;
}
// start a timer with a name
startTimer(timerName) {
// can't start timers if not active
if (!this.isActive)
return;
// can't start a started timer
if (this.timerStarts[timerName])
throw new Error(`Timer ${timerName} has already been started!`);
this.timerStarts[timerName] = Date.now();
}
stopTimer(timerName) {
// not that it should matter, but it doesn't hurt to be thorough
if (!this.isActive)
return;
// if a timer hasn't been started, it can't be stopped
if (!this.timerStarts[timerName])
throw new Error(`Timer ${timerName} has not been started!`);
// just some sanity checking
if (this.timerStops[timerName])
throw new Error(`Timer ${timerName} has already been stopped!`);
this.timerStops[timerName] = Date.now();
}
// just in case someone wants to reuse a timer instance
setActive(isActive) {
this.isActive = isActive;
}
getTimerDeltas() {
// only iterate over timerStops because we don't care about unstopped timers
return Object.keys(this.timerStops).reduce((prev, cur) => {
prev[cur] = this.timerStops[cur] - this.timerStarts[cur];
return prev;
}, {});
}
// dump the timers in a nice format, default to console.info
dumpTimers(cb = console.info) {
const deltas = this.getTimerDeltas();
Object.keys(deltas).forEach(timerName => {
const delta = deltas[timerName];
if (delta < this.dumpThreshold)
return;
cb(`[${delta}ms] ${timerName}`);
});
}
}
exports.LoggerTimer = LoggerTimer;
/***/ })
/******/ ]);
});
//# sourceMappingURL=index.js.map