UNPKG

@grafana/faro-core

Version:
103 lines 4.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const transports_1 = require("../../transports"); const utils_1 = require("../../utils"); const ItemBuffer_1 = require("../ItemBuffer"); const const_1 = require("./const"); const types_1 = require("./types"); class UserAction extends utils_1.Observable { constructor({ name, parentId, trigger, transports, attributes, trackUserActionsExcludeItem, importance = const_1.UserActionImportance.Normal, pushEvent, }) { super(); this.name = name; this.attributes = attributes; this.id = (0, utils_1.genShortID)(); this.trigger = trigger; this.parentId = parentId !== null && parentId !== void 0 ? parentId : this.id; this.trackUserActionsExcludeItem = trackUserActionsExcludeItem; this.importance = importance; this._pushEvent = pushEvent; this._itemBuffer = new ItemBuffer_1.ItemBuffer(); this._transports = transports; this._state = types_1.UserActionState.Started; this._start(); } addItem(item) { if (this._state === types_1.UserActionState.Started) { this._itemBuffer.addItem(item); return true; } return false; } _start() { this._state = types_1.UserActionState.Started; if (this._state === types_1.UserActionState.Started) { // `startTime` is wall-clock (Unix-epoch ms) so it can be used as the event's // `timestampOverwriteMs` and emitted as `userActionStartTime` for consumers that // correlate with other systems. `_startTimeMono` is the monotonic anchor used // for the duration calculation in `end()`. this.startTime = (0, utils_1.dateNow)(); this._startTimeMono = (0, utils_1.monoNow)(); } } halt() { if (this._state !== types_1.UserActionState.Started) { return; } this._state = types_1.UserActionState.Halted; this.notify(this._state); } cancel() { if (this._state === types_1.UserActionState.Started) { // Empty the buffer this._itemBuffer.flushBuffer((item) => { this._transports.execute(item); }); } this._state = types_1.UserActionState.Cancelled; this.notify(this._state); } end() { if (this._state === types_1.UserActionState.Cancelled) { return; } const endTime = (0, utils_1.dateNow)(); // Compute duration from the monotonic clock so it is unaffected by wall-clock // adjustments during the action. Note: `endTime - startTime` (both wall-clock) may // disagree with `duration` if the system clock was adjusted between start and end; // `duration` is authoritative. const duration = (0, utils_1.monoNow)() - this._startTimeMono; this._state = types_1.UserActionState.Ended; this._itemBuffer.flushBuffer((item) => { if (isExcludeFromUserAction(item, this.trackUserActionsExcludeItem)) { this._transports.execute(item); return; } const userActionItem = Object.assign(Object.assign({}, item), { payload: Object.assign(Object.assign({}, item.payload), { action: { parentId: this.id, name: this.name, } }) }); this._transports.execute(userActionItem); }); this._state = types_1.UserActionState.Ended; this.notify(this._state); this._pushEvent(const_1.userActionEventName, Object.assign({ userActionName: this.name, userActionStartTime: this.startTime.toString(), userActionEndTime: endTime.toString(), userActionDuration: duration.toString(), userActionTrigger: this.trigger, userActionImportance: this.importance }, (0, utils_1.stringifyObjectValues)(this.attributes)), undefined, { timestampOverwriteMs: this.startTime, customPayloadTransformer: (payload) => { payload.action = { id: this.id, name: this.name, }; return payload; }, }); } getState() { return this._state; } } exports.default = UserAction; function isExcludeFromUserAction(item, trackUserActionsExcludeItem) { return ((trackUserActionsExcludeItem === null || trackUserActionsExcludeItem === void 0 ? void 0 : trackUserActionsExcludeItem(item)) || (item.type === transports_1.TransportItemType.MEASUREMENT && item.payload.type === 'web-vitals')); } //# sourceMappingURL=userAction.js.map