UNPKG

@equinor/fusion-observable

Version:
57 lines 2.27 kB
/* eslint-disable @typescript-eslint/no-explicit-any */ import { actionSuffixDivider, createAction, matchActionSuffix, } from './create-action'; export function createAsyncAction(type, request, success, failure) { const action = createAction([type, 'request'].join(actionSuffixDivider), request); if (success) { Object.assign(action, { success: createAction([type, 'success'].join(actionSuffixDivider), success), }); } if (failure) { Object.assign(action, { failure: createAction([type, 'failure'].join(actionSuffixDivider), failure), }); } return action; } /** * Type guard that checks whether an action has a specific suffix in its type string. * * @template A - The action type. * @template Suffix - The expected suffix string. * @param action - The action to check. * @param suffix - The suffix to look for (e.g., `'request'`, `'success'`, `'failure'`). * @returns `true` if the action type ends with `::suffix`. */ export function isActionWithSuffix(action, suffix) { return !!action.type.match(matchActionSuffix(suffix)); } /** * Type guard that returns `true` if the action is a `::request` action. * * @param action - The action to check. * @returns `true` if the action type ends with `::request`. */ export const isRequestAction = (action) => isActionWithSuffix(action, 'request'); /** * Type guard that returns `true` if the action is a `::success` action. * * @param action - The action to check. * @returns `true` if the action type ends with `::success`. */ export const isSuccessAction = (action) => isActionWithSuffix(action, 'success'); /** * Type guard that returns `true` if the action is a `::failure` action. * * @param action - The action to check. * @returns `true` if the action type ends with `::failure`. */ export const isFailureAction = (action) => isActionWithSuffix(action, 'failure'); /** * Type guard that returns `true` if the action is either a `::success` or `::failure` action. * * @param action - The action to check. * @returns `true` if the action is a completion action. */ export const isCompleteAction = (action) => isSuccessAction(action) || isFailureAction(action); //# sourceMappingURL=create-async-action.js.map