1 import type { Reducer } from 'redux';
4 CombinedOptimisticReducer,
5 DeterministicHistoryItem,
7 OptimisticEffectHistoryItem,
8 OptimisticFailedHistoryItem,
10 OptimisticReducersMapObject,
11 WithOptimisticReducer,
12 WrappedOptimisticState,
14 import { HistoryFlag } from '../types';
16 export const isOptimisticHistoryItem = (item: HistoryItem): item is OptimisticHistoryItem =>
17 item.type === HistoryFlag.OPTIMISTIC;
19 export const isFailedOptimisticHistoryItem = (item: HistoryItem): item is OptimisticFailedHistoryItem =>
20 item.type === HistoryFlag.OPTIMISTIC && Boolean(item.failed);
22 export const isOptimisticEffectHistoryItem = (item: HistoryItem): item is OptimisticEffectHistoryItem =>
23 item.type === HistoryFlag.OPTIMISTIC_EFFECT;
25 export const isDeterministicHistoryItem = (item: HistoryItem): item is DeterministicHistoryItem =>
26 item.type === HistoryFlag.DETERMINISTIC;
28 export const isOptimisticState = <T extends {}>(state?: T): state is WrappedOptimisticState<T> =>
29 state !== undefined && state !== null && typeof state === 'object' && Boolean((state as any).optimistic);
31 export const isOptimisticReducer = <T>(
32 reducer: (Reducer<T> | WithOptimisticReducer<T>) | OptimisticReducersMapObject<T>
33 ): reducer is WithOptimisticReducer<T> => typeof (reducer as any)?.innerReducer === 'function';
35 export const isCombinedOptimisticReducer = <T>(reducer: Reducer<T>): reducer is CombinedOptimisticReducer =>
36 typeof (reducer as any)?.innerCombinedReducers === 'object';
38 export const isOptimisticHistoryItemWithId =
39 (optimisticId: string, options?: { failed: boolean }) => (item: HistoryItem) =>
40 isOptimisticHistoryItem(item) &&
41 item.id === optimisticId &&
42 (options?.failed !== undefined ? Boolean(item.failed) === options.failed : true);
44 export const isOptimisticEffectHistoryItemWithId = (optimisticId: string) => (item: HistoryItem) =>
45 isOptimisticEffectHistoryItem(item) && item.id === optimisticId;