1 import type { WithOptimisticHistory } from '../types';
2 import { isOptimisticHistoryItem } from './assertions';
5 * Splits an OptimisticHistory into two parts :
6 * - every action before the first optimistic item matched in the history
7 * - the rest of the actions including the first optimistic item matched
9 * Because the split happens on the first optimistic item, it is safe to
10 * assume that all items that end up on the left side are simple actions.
13 export const splitHistoryOnFirstOptimisticItem = (
14 history: WithOptimisticHistory
15 ): readonly [WithOptimisticHistory, WithOptimisticHistory] => {
16 const firstOptimisticItemIndex = history.findIndex(isOptimisticHistoryItem);
18 if (firstOptimisticItemIndex === -1) {
19 return [history, [] as WithOptimisticHistory] as const;
22 const left = history.slice(0, firstOptimisticItemIndex);
23 const right = history.slice(firstOptimisticItemIndex);
25 return [left, right] as const;