UNPKG

nstdlib-nightly

Version:

Node.js standard library converted to runtime-agnostic ES modules.

72 lines (61 loc) 1.81 kB
// Source: https://github.com/nodejs/node/blob/65eff1eb/lib/internal/perf/event_loop_delay.js import { codes as __codes__ } from "nstdlib/lib/internal/errors"; import { createELDHistogram } from "nstdlib/stub/binding/performance"; import { validateInteger, validateObject, } from "nstdlib/lib/internal/validators"; import { Histogram, kHandle, kMap } from "nstdlib/lib/internal/histogram"; import { kEmptyObject } from "nstdlib/lib/internal/util"; import { markTransferMode } from "nstdlib/lib/internal/worker/js_transferable"; const { ERR_ILLEGAL_CONSTRUCTOR, ERR_INVALID_THIS } = __codes__; const kEnabled = Symbol("kEnabled"); class ELDHistogram extends Histogram { constructor(i) { throw new ERR_ILLEGAL_CONSTRUCTOR(); } /** * @returns {boolean} */ enable() { if (this[kEnabled] === undefined) throw new ERR_INVALID_THIS("ELDHistogram"); if (this[kEnabled]) return false; this[kEnabled] = true; this[kHandle].start(); return true; } /** * @returns {boolean} */ disable() { if (this[kEnabled] === undefined) throw new ERR_INVALID_THIS("ELDHistogram"); if (!this[kEnabled]) return false; this[kEnabled] = false; this[kHandle].stop(); return true; } } /** * @param {{ * resolution : number * }} [options] * @returns {ELDHistogram} */ function monitorEventLoopDelay(options = kEmptyObject) { validateObject(options, "options"); const { resolution = 10 } = options; validateInteger(resolution, "options.resolution", 1); return Reflect.construct( function () { markTransferMode(this, true, false); this[kEnabled] = false; this[kHandle] = createELDHistogram(resolution); this[kMap] = new Map(); }, [], ELDHistogram, ); } export default monitorEventLoopDelay;