@grafana/faro-core
Version:
Core package of Faro.
79 lines • 3.32 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;
const reactive_1 = require("../../utils/reactive");
const const_1 = require("./const");
const types_1 = require("./types");
const userAction_1 = __importDefault(require("./userAction"));
exports.userActionsMessageBus = new reactive_1.Observable();
function initializeUserActionsAPI({ transports, internalLogger, config, pushEvent, }) {
var _a;
const trackUserActionsExcludeItem = (_a = config.userActionsInstrumentation) === null || _a === void 0 ? void 0 : _a.excludeItem;
// Currently running user action. It can be in either started or halted
// state
let activeUserAction;
// If there is a an action already running, return undefined to indicate
// we were not able to create one.
const startUserAction = (name, attributes, options) => {
const currentRunningUserAction = getActiveUserAction();
if (currentRunningUserAction === undefined) {
const userAction = new userAction_1.default({
name,
transports,
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,
pushEvent,
});
userAction
.filter((v) => [types_1.UserActionState.Ended, types_1.UserActionState.Cancelled].includes(v))
.first()
.subscribe(() => {
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;
}
};
const getActiveUserAction = () => {
return activeUserAction;
};
const api = {
startUserAction,
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;
}
const 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