UNPKG

@grafana/faro-web-sdk

Version:

Faro instrumentations, metas, transports for web.

140 lines 5.91 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 __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.ActivityWindowTracker = void 0; exports.isRequestStartMessage = isRequestStartMessage; exports.isRequestEndMessage = isRequestEndMessage; var faro_core_1 = require("@grafana/faro-core"); var const_1 = require("./monitors/const"); function isRequestStartMessage(msg) { return msg.type === const_1.MESSAGE_TYPE_HTTP_REQUEST_START; } function isRequestEndMessage(msg) { return msg.type === const_1.MESSAGE_TYPE_HTTP_REQUEST_END; } /** * Tracks events in a time‑boxed activity window. When the window goes quiet for `inactivityMs`, * it enters a draining phase: new short events are ignored; only active operations are awaited * until they end or `drainTimeoutMs` elapses. */ var ActivityWindowTracker = /** @class */ (function (_super) { __extends(ActivityWindowTracker, _super); function ActivityWindowTracker(eventsObservable, options) { var _a, _b, _c, _d; var _this = _super.call(this) || this; _this._tracking = false; _this.eventsObservable = eventsObservable; _this._options = { inactivityMs: (_a = options === null || options === void 0 ? void 0 : options.inactivityMs) !== null && _a !== void 0 ? _a : 100, drainTimeoutMs: (_b = options === null || options === void 0 ? void 0 : options.drainTimeoutMs) !== null && _b !== void 0 ? _b : 10 * 1000, isOperationStart: (_c = options === null || options === void 0 ? void 0 : options.isOperationStart) !== null && _c !== void 0 ? _c : (function () { return undefined; }), isOperationEnd: (_d = options === null || options === void 0 ? void 0 : options.isOperationEnd) !== null && _d !== void 0 ? _d : (function () { return undefined; }), }; _this._initialize(); return _this; } ActivityWindowTracker.prototype._initialize = function () { var _this = this; this.eventsObservable .filter(function () { return _this._tracking; }) .subscribe(function (event) { var _a, _b, _c; _this._lastEventTime = Date.now(); (_a = _this._currentEvents) === null || _a === void 0 ? void 0 : _a.push(event); var startKey = _this._options.isOperationStart(event); if (startKey) { (_b = _this._activeOperations) === null || _b === void 0 ? void 0 : _b.set(startKey, true); } var endKey = _this._options.isOperationEnd(event); if (endKey) { (_c = _this._activeOperations) === null || _c === void 0 ? void 0 : _c.delete(endKey); } _this._scheduleInactivityCheck(); }); }; ActivityWindowTracker.prototype.startTracking = function () { if (this._tracking) { return; } this._tracking = true; this._startTime = Date.now(); this._lastEventTime = Date.now(); this.notify({ message: 'tracking-started', }); this._currentEvents = []; this._activeOperations = new Map(); this._scheduleInactivityCheck(); }; ActivityWindowTracker.prototype.stopTracking = function () { this._tracking = false; this._clearTimer(this._inactivityTid); this._clearTimer(this._drainTid); var duration = 0; if (this.hasActiveOperations()) { duration = Date.now() - this._startTime; } else { duration = this._lastEventTime ? this._lastEventTime - this._startTime : 0; } this.notify({ message: 'tracking-ended', events: this._currentEvents, duration: duration, }); }; ActivityWindowTracker.prototype._scheduleInactivityCheck = function () { var _this = this; this._inactivityTid = startTimeout(this._inactivityTid, function () { if (_this.hasActiveOperations()) { _this._startDrainTimeout(); } else { _this.stopTracking(); } }, this._options.inactivityMs); }; ActivityWindowTracker.prototype._startDrainTimeout = function () { var _this = this; this._drainTid = startTimeout(this._drainTid, function () { _this.stopTracking(); }, this._options.drainTimeoutMs); }; ActivityWindowTracker.prototype.hasActiveOperations = function () { return !!this._activeOperations && this._activeOperations.size > 0; }; ActivityWindowTracker.prototype._clearTimer = function (id) { if (id) { clearTimeout(id); } }; return ActivityWindowTracker; }(faro_core_1.Observable)); exports.ActivityWindowTracker = ActivityWindowTracker; function startTimeout(timeoutId, cb, delay) { if (timeoutId) { clearTimeout(timeoutId); } //@ts-expect-error for some reason vscode is using the node types timeoutId = setTimeout(function () { cb(); }, delay); return timeoutId; } //# sourceMappingURL=activityWindowTracker.js.map