UNPKG

@zendesk/retrace

Version:

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

80 lines 3.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getExternalApi = void 0; const constants_1 = require("./constants"); const performanceMark_1 = require("./performanceMark"); /** used to generate timing API that can be used outside of React, or together with React */ const getExternalApi = ({ actionLogCache, idPrefix, placement, }) => { const getFullId = (idSuffix) => `${idPrefix}/${idSuffix}`; const getActionLogForIdIfExists = (idSuffix) => { const id = getFullId(idSuffix); const actionLog = actionLogCache.get(id); actionLog?.updateOptions({ id }, placement); return actionLog; }; const getActionLogForId = (idSuffix) => { const id = getFullId(idSuffix); const getActionLog = actionLogCache.makeGetOrCreateFn(id); const actionLog = getActionLog(id); actionLog.updateOptions({ id }, placement); return actionLog; }; let renderStartMark = null; return { getActionLogForId, getActionLogForIdIfExists, markRenderStart: (idSuffix) => { const id = getFullId(idSuffix); const actionLog = getActionLogForId(idSuffix); actionLog.ensureReporting(); actionLog.setActive(true, placement); renderStartMark = renderStartMark ?? (0, performanceMark_1.performanceMark)(`${id}/${placement}/render-start`); }, markRenderEnd: (idSuffix) => { const id = getFullId(idSuffix); const actionLog = getActionLogForId(idSuffix); actionLog.setActive(true, placement); if (!renderStartMark) { actionLog.onInternalError(new Error(`ComponentTiming: markRenderDone called without a corresponding markRenderStart in '${placement}' for id: '${id}.`)); return; } actionLog.addSpan({ type: constants_1.ACTION_TYPE.RENDER, entry: Object.assign((0, performanceMark_1.performanceMeasure)(`${id}/${placement}/render`, renderStartMark), { startMark: renderStartMark, }), source: placement, }); renderStartMark = null; }, markStage: (idSuffix, stage, stageMeta) => { const actionLog = getActionLogForId(idSuffix); actionLog.ensureReporting(); actionLog.setActive(true, placement); actionLog.markStage({ stage, source: placement, metadata: stageMeta }); }, setActive: (idSuffix, active) => { const actionLog = getActionLogForId(idSuffix); actionLog.setActive(active, placement); }, dispose: (idSuffix) => { const actionLog = actionLogCache.get(getFullId(idSuffix)); if (!actionLog) return; actionLog.onBeaconRemoved(placement); }, clear: (idSuffix) => { const actionLog = getActionLogForIdIfExists(idSuffix); if (actionLog) { actionLog.clear(); } }, setMetadata: (idSuffix, metadata) => { const actionLog = getActionLogForId(idSuffix); actionLog.customMetadataBySource.set(placement, metadata); }, }; }; exports.getExternalApi = getExternalApi; //# sourceMappingURL=getExternalApi.js.map