UNPKG

@zendesk/retrace

Version:

define and capture Product Operation Traces along with computed metrics with an optional friendly React beacon API

54 lines 2.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TickParentResolver = exports.TICK_META_END = exports.TICK_META = void 0; /** symbol for storing the tick meta for starting of the span */ exports.TICK_META = Symbol('tickMeta'); /** symbol for storing the tick meta for closing of the span */ exports.TICK_META_END = Symbol('tickMetaEnd'); class TickParentResolver { #utilities; constructor(utilities) { this.#utilities = utilities; this.#tickId = utilities.generateId('tick'); } #currentTickSpans = []; #isFlushScheduled = false; #tickId; #ensureFlushScheduled() { if (!this.#isFlushScheduled) { this.#isFlushScheduled = true; // double queueMicrotask ensures the flush happens at the very end of the current event loop tick, // after all spans are added to the current tick queueMicrotask(() => { queueMicrotask(this.#flushCurrentTickSpans); }); } } #flushCurrentTickSpans = () => { // we define an attribute to indicate that the tick is completed, and will not change this.#currentTickSpans.tickCompleted = true; // very important: the array instance is created fresh to preserve references to arrays in previous tick closures this.#currentTickSpans = []; this.#tickId = this.#utilities.generateId('tick'); this.#isFlushScheduled = false; }; addSpanToCurrentTick(span, endingSpan = false) { const spansInCurrentTick = this.#currentTickSpans; const thisSpanInCurrentTickIndex = spansInCurrentTick.push(span) - 1; // eslint-disable-next-line no-param-reassign span.tickId = this.#tickId; // store a non-enumerable reference to the tick meta on the span - helpful for parent resolution after the trace was finished // eslint-disable-next-line no-param-reassign span[endingSpan ? exports.TICK_META_END : exports.TICK_META] = { spansInCurrentTick, thisSpanInCurrentTickIndex, }; this.#ensureFlushScheduled(); return { spansInCurrentTick, thisSpanInCurrentTickIndex, }; } } exports.TickParentResolver = TickParentResolver; //# sourceMappingURL=TickParentResolver.js.map