/** * A polled function used by [fromPoll](../globals.html#frompoll) */ import { EventLike, EventStream, EventStreamSeed, isEnd, isValue, Scope, toEvents, valueEvent, } from "./abstractions" import { fromSubscribe } from "./fromsubscribe" import GlobalScheduler from "./scheduler" import { nop, rename } from "./util" export type PollFunction = () => EventLike /** Polls given function with given interval. When there are subscribers to the stream. Polling ends permanently when `f` returns an End event. * @param delay poll interval in milliseconds * @param poll function to be polled. Can return either V, Event or an array of Event. In the latter cases the End event can be used to stop polling. * @typeparam V Type of stream elements */ export function fromPoll( delay: number, poll: PollFunction, scope: Scope ): EventStream export function fromPoll( delay: number, poll: PollFunction ): EventStreamSeed export function fromPoll( delay: number, poll: PollFunction, scope?: Scope ): any { return rename( `fromPoll(${delay},fn)`, fromSubscribe((onValue, onEnd = nop) => { const interval = GlobalScheduler.scheduler.setInterval(() => { const events = toEvents(poll()) for (const event of events) { if (isValue(event)) { onValue(event.value) } else { GlobalScheduler.scheduler.clearInterval(interval) onEnd() } } }, delay) return () => GlobalScheduler.scheduler.clearInterval(interval) }, scope as any) ) }