UNPKG

@snipsonian/observable-state

Version:

Observable-state snippets (redux-like)

34 lines (33 loc) 1.52 kB
import { AsyncOperation, AsyncStatus } from './types'; export const asyncOperationList = Object.values(AsyncOperation); export function isValidAsyncOperation(possibleOperation) { return asyncOperationList.indexOf(possibleOperation) !== -1; } export function isAnyAsyncOperationBusy(asyncEntity) { return Object.values(AsyncOperation) .some((operation) => isAsyncOperationBusy(asyncEntity, operation)); } export function isFetchBusy(asyncEntity) { return isAsyncOperationBusy(asyncEntity, AsyncOperation.fetch); } export function isUpdateBusy(asyncEntity) { return isAsyncOperationBusy(asyncEntity, AsyncOperation.update); } export function isAsyncOperationBusy(asyncEntity, operation) { return hasAsyncOperationExpectedStatus(asyncEntity, operation, AsyncStatus.Busy); } export function hasFetchSucceeded(asyncEntity) { return hasAsyncOperationSucceeded(asyncEntity, AsyncOperation.fetch); } export function hasFetchFailed(asyncEntity) { return hasAsyncOperationFailed(asyncEntity, AsyncOperation.fetch); } export function hasAsyncOperationSucceeded(asyncEntity, operation) { return hasAsyncOperationExpectedStatus(asyncEntity, operation, AsyncStatus.Success); } export function hasAsyncOperationFailed(asyncEntity, operation) { return hasAsyncOperationExpectedStatus(asyncEntity, operation, AsyncStatus.Error); } function hasAsyncOperationExpectedStatus(asyncEntity, operation, status) { return asyncEntity[operation] && (asyncEntity[operation].status === status); }