@grafana/faro-core
Version:
Core package of Faro.
80 lines • 3.51 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.userActionsMessageBus = void 0;
exports.initializeUserActionsAPI = initializeUserActionsAPI;
exports.addItemToUserActionBuffer = addItemToUserActionBuffer;
var reactive_1 = require("../../utils/reactive");
var const_1 = require("./const");
var types_1 = require("./types");
var userAction_1 = __importDefault(require("./userAction"));
exports.userActionsMessageBus = new reactive_1.Observable();
function initializeUserActionsAPI(_a) {
var _b;
var transports = _a.transports, internalLogger = _a.internalLogger, config = _a.config, pushEvent = _a.pushEvent;
var trackUserActionsExcludeItem = (_b = config.userActionsInstrumentation) === null || _b === void 0 ? void 0 : _b.excludeItem;
// Currently running user action. It can be in either started or halted
// state
var activeUserAction;
// If there is a an action already running, return undefined to indicate
// we were not able to create one.
var startUserAction = function (name, attributes, options) {
var currentRunningUserAction = getActiveUserAction();
if (currentRunningUserAction === undefined) {
var userAction = new userAction_1.default({
name: name,
transports: transports,
attributes: attributes,
trigger: (options === null || options === void 0 ? void 0 : options.triggerName) || const_1.userActionStartByApiCallEventName,
importance: (options === null || options === void 0 ? void 0 : options.importance) || const_1.UserActionImportance.Normal,
trackUserActionsExcludeItem: trackUserActionsExcludeItem,
pushEvent: pushEvent,
});
userAction
.filter(function (v) { return [types_1.UserActionState.Ended, types_1.UserActionState.Cancelled].includes(v); })
.first()
.subscribe(function () {
activeUserAction = undefined;
});
exports.userActionsMessageBus.notify({
type: const_1.userActionStart,
userAction: userAction,
});
activeUserAction = userAction;
return activeUserAction;
}
else {
internalLogger.error('Attempted to create a new user action while one is already running. This is not possible.');
return undefined;
}
};
var getActiveUserAction = function () {
return activeUserAction;
};
var api = {
startUserAction: startUserAction,
getActiveUserAction: getActiveUserAction,
};
return api;
}
/**
* Adds an item to the buffer associated with the given UserAction.
* The item will only be added if the UserAction is in the Started state.
* @param userAction The UserAction instance
* @param item The item to add to the buffer
* @returns {boolean} True if the item was added, false otherwise
*/
function addItemToUserActionBuffer(userAction, item) {
if (!userAction) {
return false;
}
var state = userAction === null || userAction === void 0 ? void 0 : userAction.getState();
if (state !== types_1.UserActionState.Started) {
return false;
}
userAction.addItem(item);
return true;
}
//# sourceMappingURL=initialize.js.map