import { Subscriber } from "../_core" import { Dispose, Sink, SinkEvent, SinkResult, Subscribe } from "../_interfaces" import { isArray, isFunction } from "../_util" import * as Event from "../Event" import { Activation, Root } from "./_base" export class FromBinder extends Root { constructor(public f: Subscribe) { super(false) } protected create(subscriber: Subscriber): Activation> { return new FromBinderActivation(this, subscriber) } } class FromBinderActivation extends Activation> { private userDispose: Dispose | null = null protected start(): void { if (this.active) { const sink: Sink = (event: SinkEvent): SinkResult => { this.handleSinkEvent(event) return this.active ? Event.more : Event.noMore } // TODO try-catch? const { f } = this.owner const dispose = f(sink) if (isFunction(dispose)) { if (this.active) { this.userDispose = dispose } else { dispose() } } } } protected stop(): void { // TODO try-catch? const dispose = this.userDispose if (dispose !== null) { this.userDispose = null dispose() } } private handleSinkEvent(event: SinkEvent): void { if (isArray(event)) { const n = event.length for (let i = 0; this.active && i < n; i++) { this.send(event[i]) } } else { this.send(event) } } }