import { checkFunctionOrProperty } from "../_check" import { Source } from "../_core" import { In, Out, Predicate } from "../_interfaces" import { makeObservable } from "../_obs" import { Transaction } from "../_tx" import { curry2 } from "../_util" import { dispatcherOf, Observable } from "../Observable" import { isProperty, Property } from "../Property" import { Operator } from "./_base" import { filter } from "./filter" import { startWith } from "./startWith" import { takeUntil } from "./takeUntil" export const takeWhile: CurriedTakeWhile = curry2(_takeWhile) export interface CurriedTakeWhile { ( f: Predicate | Property, observable: In, ): Out (f: Predicate | Property): ( observable: In, ) => Out } function _takeWhile( f: Predicate | Property, observable: Observable, ): Observable { checkFunctionOrProperty(f) return isProperty(f) ? takeUntil(filter(x => !x, startWith(true, f)), observable) : makeObservable(observable, new TakeWhile(dispatcherOf(observable), f)) } class TakeWhile extends Operator { constructor(src: Source, private f: Predicate) { super(src) } public next(tx: Transaction, val: T): void { const { f } = this if (!f(val)) { this.sink.end(tx) } else { this.sink.next(tx, val) } } }