import { ActyxEvent, EventKey, LocalSnapshot, StateWithProvenance, Timestamp } from '@actyx/sdk' import { Option } from 'fp-ts/lib/Option' // A local snapshot where the state has already been serialised export type SerializedStateSnap = LocalSnapshot // A local snapshot pending persistence export type PendingSnapshot = { snap: SerializedStateSnap tag: string timestamp: Timestamp } // A Reducer for the Events-Or-State message stream that // - is unaware of serialization // - can apply event chunks within bounds export type SimpleReducer = { // Apply the events between fromIdx and toIdxInclusive to the state appendEvents: (events: ActyxEvent[], fromIdx: number, toIdxInclusive: number) => LocalSnapshot // Directly set the state setState: (state: LocalSnapshot) => void } // A Reducer for the Events-Or-State message stream // which will cache intermediate States. // (This type would extend `Reducer`, if JS had 0-copy slices.) export type CachingReducer = { // Apply the given events to the state and return the new latest state. // Internally, this will queue up snapshots and persist them async (persistence can be awaited via awaitPendingPersistence) appendEvents: (events: ActyxEvent[]) => StateWithProvenance // Just directly set a certain state. // Will invalidate all later cached states. setState: (state: SerializedStateSnap) => void // Await persistence for all states (local snapshots) awaitPendingPersistence: () => Promise // Get the latest state still valid according to the given arguments. // This method by itself does not invalidate anything inside the cache, it just filters. latestKnownValidState: (invalidateStatesAfter: EventKey) => Option }