import { checkFunction } from "../_check" import { Source } from "../_core" import { In, Out } from "../_interfaces" import { makeObservable } from "../_obs" import { Transaction } from "../_tx" import { curry3 } from "../_util" import { dispatcherOf, Observable } from "../Observable" import { Sliding } from "./slidingWindow" export type Delta = (a: ValueType, b: ValueType) => DeltaType export const diff: CurriedDiff = curry3(_diff) export interface CurriedDiff { ( f: Delta, start: ValueType, observable: In, ): Out (f: Delta): ( start: ValueType, observable: In, ) => Out (f: Delta, start: ValueType): ( observable: In, ) => Out (f: Delta): ( start: ValueType, ) => (observable: In) => Out } function _diff(f: Delta, start: T, observable: Observable): Observable { checkFunction(f) return makeObservable(observable, new Diff(dispatcherOf(observable), f, start)) } class Diff extends Sliding { constructor(source: Source, private d: Delta, start: T) { super(source, 2, 2, [start]) } protected nextWin(tx: Transaction, [a, b]: T[]): void { const { d } = this this.sink.next(tx, d(a, b)) } }