import { Event, EventStream, EventStreamSeed, isValue, Observer, Property, PropertySeed, valueEvent, Scope, } from "./abstractions" import { applyScopeMaybe } from "./applyscope" import { PropertySeedImpl } from "./property" export function scan( initial: B, fn: (state: B, next: A) => B, scope: Scope ): (stream: EventStream | EventStreamSeed) => Property export function scan( initial: B, fn: (state: B, next: A) => B ): (stream: EventStream | EventStreamSeed) => PropertySeed export function scan( initial: B, fn: (state: B, next: A) => B, scope?: Scope ): any { return (seed: EventStream | EventStreamSeed) => { const source = seed.consume() let current = initial return applyScopeMaybe( new PropertySeedImpl( [source, "scan", [fn]], () => initial, (onValue: Observer, onEnd?: Observer) => { const unsub = source.subscribe((event) => { current = fn(current, event) onValue(current) }, onEnd) return unsub } ), scope ) } }