UNPKG

@barchart/common-js

Version:
163 lines (157 loc) 4.49 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var WindowCounter_exports = {}; __export(WindowCounter_exports, { default: () => WindowCounter }); module.exports = __toCommonJS(WindowCounter_exports); var assert = __toESM(require("./../lang/assert.js")); class WindowCounter { #duration; #windows; #maximum; #previousCount; /** * @param {number} duration * @param {number} windows */ constructor(duration, windows) { assert.argumentIsRequired(duration, "duration", Number); assert.argumentIsRequired(windows, "windows", Number); this.#duration = duration; this.#windows = [new Window(getTime(), this.#duration)]; this.#maximum = Math.max(windows, 2); this.#previousCount = 0; } /** * @public * @param {number} count */ increment(count) { assert.argumentIsRequired(count, "count", Number); this.#advance().increment(count); } /** * @public * @returns {number} */ getCurrent() { return this.#advance().getCount(); } /** * @public * @returns {number} */ getPrevious() { this.#advance(); let returnVal; if (this.#windows.length > 1) { returnVal = this.#windows[1].getCount(); } else { returnVal = 0; } return returnVal; } /** * @public * @returns {number} */ getAverage() { const previousWindows = this.#windows.length - 1; let returnVal; if (previousWindows > 0) { returnVal = this.#previousCount / previousWindows; } else { returnVal = 0; } return returnVal; } /** * Returns a string representation. * * @public * @returns {string} */ toString() { return "[WindowCounter]"; } #advance() { const now = getTime(); while (!this.#windows[0].contains(now)) { const previous = this.#windows[0]; const current = new Window(previous.getEnd(), this.#duration); this.#windows.unshift(current); this.#previousCount = this.#previousCount + previous.getCount(); if (this.#windows.length > this.#maximum) { const removed = this.#windows.pop(); this.#previousCount = this.#previousCount - removed.getCount(); } } return this.#windows[0]; } } function getTime() { return (/* @__PURE__ */ new Date()).getTime(); } class Window { #start; #end; #count; constructor(start, duration) { this.#start = start; this.#end = start + duration; this.#count = 0; } contains(now) { return !(now < this.#start || now > this.#end); } increment(count) { this.#count = this.#count + count; } getStart() { return this.#start; } getEnd() { return this.#end; } getCount() { return this.#count; } } { const cjsExports = module.exports; const cjsDefaultExport = cjsExports && cjsExports.__esModule ? cjsExports.default : cjsExports; if (cjsDefaultExport && (typeof cjsDefaultExport === 'function' || typeof cjsDefaultExport === 'object')) { Object.keys(cjsExports).forEach((key) => { if (key !== 'default' && key !== '__esModule') { cjsDefaultExport[key] = cjsExports[key]; } }); } module.exports = cjsDefaultExport; }