type ViewState = { received: number; shown: number; pausedCount: number; paused: boolean; }; function idle(): ViewState { return { received: 0, shown: 0, pausedCount: 0, paused: false }; } function pause(state: ViewState): ViewState { return { ...state, paused: true, pausedCount: 0 }; } function resume(state: ViewState): ViewState { return { ...state, paused: false, pausedCount: 0, shown: state.received, }; } function applyBatch(state: ViewState, size: number): ViewState { if (!state.paused) { return { ...state, received: state.received + size, shown: state.shown + size, }; } return { ...state, received: state.received + size, pausedCount: state.pausedCount + size, }; } const steps: ViewState[] = []; let state = idle(); state = applyBatch(state, 5); steps.push(state); state = pause(state); state = applyBatch(state, 3); steps.push(state); state = applyBatch(state, 2); steps.push(state); state = resume(state); steps.push(state); state = applyBatch(state, 1); steps.push(state); const pausedSnapshot = steps[2]; const afterResume = steps[3]; const afterOneMore = steps[4]; console.log( JSON.stringify({ evt: "smoke", ok: pausedSnapshot !== undefined && pausedSnapshot.received === 10 && pausedSnapshot.shown === 5 && pausedSnapshot.pausedCount === 5 && afterResume !== undefined && afterResume.shown === 10 && afterResume.pausedCount === 0 && afterOneMore !== undefined && afterOneMore.shown === 11 && afterOneMore.received === 11, steps, }), );