@grafana/faro-core
Version:
Core package of Faro.
71 lines • 2.93 kB
JavaScript
import { Observable } from '../../utils/reactive';
import { UserActionImportance, userActionStart, userActionStartByApiCallEventName } from './const';
import { UserActionState, } from './types';
import UserAction from './userAction';
export const userActionsMessageBus = new Observable();
export 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({
name,
transports,
attributes,
trigger: (options === null || options === void 0 ? void 0 : options.triggerName) || userActionStartByApiCallEventName,
importance: (options === null || options === void 0 ? void 0 : options.importance) || UserActionImportance.Normal,
trackUserActionsExcludeItem,
pushEvent,
});
userAction
.filter((v) => [UserActionState.Ended, UserActionState.Cancelled].includes(v))
.first()
.subscribe(() => {
activeUserAction = undefined;
});
userActionsMessageBus.notify({
type: 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
*/
export function addItemToUserActionBuffer(userAction, item) {
if (!userAction) {
return false;
}
const state = userAction === null || userAction === void 0 ? void 0 : userAction.getState();
if (state !== UserActionState.Started) {
return false;
}
userAction.addItem(item);
return true;
}
//# sourceMappingURL=initialize.js.map