UNPKG

@grafana/faro-core

Version:
123 lines 5.59 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); var transports_1 = require("../../transports"); var utils_1 = require("../../utils"); var ItemBuffer_1 = require("../ItemBuffer"); var const_1 = require("./const"); var types_1 = require("./types"); var UserAction = /** @class */ (function (_super) { __extends(UserAction, _super); function UserAction(_a) { var name = _a.name, parentId = _a.parentId, trigger = _a.trigger, transports = _a.transports, attributes = _a.attributes, trackUserActionsExcludeItem = _a.trackUserActionsExcludeItem, _b = _a.importance, importance = _b === void 0 ? const_1.UserActionImportance.Normal : _b, pushEvent = _a.pushEvent; var _this = _super.call(this) || this; _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(); return _this; } UserAction.prototype.addItem = function (item) { if (this._state === types_1.UserActionState.Started) { this._itemBuffer.addItem(item); return true; } return false; }; UserAction.prototype._start = function () { this._state = types_1.UserActionState.Started; if (this._state === types_1.UserActionState.Started) { this.startTime = (0, utils_1.dateNow)(); } }; UserAction.prototype.halt = function () { if (this._state !== types_1.UserActionState.Started) { return; } this._state = types_1.UserActionState.Halted; this.notify(this._state); }; UserAction.prototype.cancel = function () { if (this._state === types_1.UserActionState.Started) { // Empty the buffer this._itemBuffer.flushBuffer(); } this._state = types_1.UserActionState.Cancelled; this.notify(this._state); }; UserAction.prototype.end = function () { var _this = this; if (this._state === types_1.UserActionState.Cancelled) { return; } var endTime = (0, utils_1.dateNow)(); var duration = endTime - this.startTime; this._state = types_1.UserActionState.Ended; this._itemBuffer.flushBuffer(function (item) { if (isExcludeFromUserAction(item, _this.trackUserActionsExcludeItem)) { _this._transports.execute(item); return; } var userActionItem = __assign(__assign({}, item), { payload: __assign(__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, __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: function (payload) { payload.action = { id: _this.id, name: _this.name, }; return payload; }, }); }; UserAction.prototype.getState = function () { return this._state; }; return UserAction; }(utils_1.Observable)); 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