import { assert } from "./_assert"
import { checkFunction } from "./_check"
import {
argsToObservables,
argsToObservablesAndFunction,
toFunction,
toFunctionsPropAsIs,
} from "./_interrop"
import { isFunction } from "./_util"
import * as F from "./index"
import {
Accum,
Bus,
Dispose,
EndHandler,
EndProjection,
Eq,
ErrorHandler,
EventStream,
FlatAccum,
Handler,
Observable,
Predicate,
Projection,
Property,
ValueHandler,
ValuesHandler,
} from "./index"
import * as Logic from "./operators/logic"
declare module "./Observable" {
interface Observable {
subscribe(handler: Handler): Dispose
awaiting(otherObservable: Observable): Property
onValue(f: ValueHandler): Dispose
onValues(f: ValuesHandler): Dispose
onError(f: ErrorHandler): Dispose
onEnd(f: EndHandler): Dispose
doAction(f: (val: A) => void): Observable
doError(f: (err: Error) => void): Observable
doEnd(f: () => void): Observable
doLog(label?: string): Observable
assign(obj: any, method: string, ...params: any[]): Dispose
log(label?: string): Dispose
map(project: Projection): Observable
mapError(project: Projection): Observable
mapEnd(f: EndProjection): Observable
filter(predicate: Predicate | Property): Observable
take(n: number): Observable
takeUntil(trigger: Observable): Observable
takeWhile(f: Predicate | Property): Observable
first(): Observable
last(): Observable
skip(n: number): Observable
skipUntil(trigger: Observable): Observable
skipWhile(trigger: Predicate | Property): Observable
skipErrors(): Observable
flatMapLatest(project: Projection>): Observable
flatMapFirst(project: Projection>): Observable
flatMap(project: Projection>): Observable
flatMapConcat(project: Projection>): Observable
flatMapWithConcurrencyLimit(
limit: number,
project: Projection>,
): Observable
flatMapError(project: Projection>): Observable
startWith(value: A): Observable
scan(seed: B, f: Accum): Property
fold(seed: B, f: Accum): Property
flatScan(seed: B, f: FlatAccum): Property
reduce(seed: B, f: Accum): Property
zip(other: Observable, f: (a: A, b: B) => C): EventStream
merge(other: Observable): EventStream
concat(other: Observable): EventStream
combine(other: Observable, f: (a: A, b: B) => C): Property
errors(): Observable
throttle(delay: number): Observable
debounce(delay: number): Observable
debounceImmediate(delay: number): Observable
delay(delay: number): Observable
bufferingThrottle(minimumInterval: number): Observable
slidingWindow(max: number, min?: number): Property
skipDuplicates(isEqual?: Eq): Observable
diff(start: A, f: F.Delta): Observable
toPromise(ctor?: PromiseConstructor): Promise
firstToPromise(ctor?: PromiseConstructor): Promise
}
}
declare module "./EventStream" {
interface EventStream {
toProperty(initialValue?: A): Property
doAction(f: (val: A) => void): EventStream
doError(f: (err: Error) => void): EventStream
doEnd(f: () => void): EventStream
doLog(label?: string): EventStream
map(project: Projection): EventStream
mapError(project: Projection): EventStream
mapEnd(f: EndProjection): EventStream
filter(predicate: Predicate | Property): EventStream
take(n: number): EventStream
takeUntil(trigger: Observable): EventStream
takeWhile(f: Predicate | Property): EventStream
first(): EventStream
last(): EventStream
skip(n: number): EventStream
skipUntil(trigger: Observable): EventStream
skipWhile(trigger: Predicate | Property): EventStream
skipErrors(): EventStream
flatMapLatest(project: Projection>): EventStream
flatMapFirst(project: Projection>): EventStream
flatMap(project: Projection>): EventStream
flatMapConcat(project: Projection>): EventStream
flatMapWithConcurrencyLimit(
limit: number,
project: Projection>,
): EventStream
flatMapError(project: Projection>): EventStream
startWith(value: A): EventStream
errors(): EventStream
throttle(delay: number): EventStream
debounce(delay: number): EventStream
debounceImmediate(delay: number): EventStream
delay(delay: number): EventStream
bufferingThrottle(minimumInterval: number): EventStream
bufferWithTime(delay: number): EventStream
bufferWithCount(count: number): EventStream
bufferWithTimeOrCount(delay: number, count: number): EventStream
skipDuplicates(isEqual?: Eq): EventStream
diff(start: A, f: F.Delta): EventStream
}
}
declare module "./Property" {
interface Property {
toEventStream(): EventStream
changes(): EventStream
doAction(f: (val: A) => void): Property
doError(f: (err: Error) => void): Property
doEnd(f: () => void): Property
doLog(label?: string): Property
map(project: Projection): Property
mapError(project: Projection): Property
mapEnd(f: EndProjection): Property
filter(predicate: Predicate | Property): Property
take(n: number): Property
takeUntil(trigger: Observable): Property
takeWhile(f: Predicate | Property): Property
first(): Property
last(): Property
skip(n: number): Property
skipUntil(trigger: Observable): Property
skipWhile(trigger: Predicate | Property): Property
skipErrors(): Property
flatMapLatest(project: Projection>): Property
flatMapFirst(project: Projection>): Property
flatMap(project: Projection>): Property
flatMapConcat(project: Projection>): Property
flatMapWithConcurrencyLimit(
limit: number,
project: Projection>,
): Property
flatMapError(project: Projection>): Property
sampledBy(sampler: EventStream): EventStream
sampledBy(sampler: Property): Property
sampledBy(sampler: EventStream, f: SampleFn): EventStream
sampledBy(sampler: Property, f: SampleFn): Property
sample(interval: number): EventStream
startWith(value: A): Property
and(other: Property): Property>
or(other: Property): Property>
not(): Property
errors(): Property
throttle(delay: number): Property
debounce(delay: number): Property
debounceImmediate(delay: number): Property
delay(delay: number): Property
bufferingThrottle(minimumInterval: number): Property
skipDuplicates(isEqual?: Eq): Property
diff(start: A, f: F.Delta): Property
}
}
declare module "./Bus" {
interface Bus {
push(event: T | F.AnyEvent): void
end(): void
error(err: Error): void
plug(obs: Observable): F.Dispose
}
}
export type SampleFn = (value: V, sample: S) => R
Observable.prototype.subscribe = function(handler: Handler, ...rest: any[]): Dispose {
return F.subscribe(toFunction(handler, rest), this)
}
Observable.prototype.awaiting = function(otherObservable: Observable): Property {
return F.awaiting(otherObservable, this)
}
Observable.prototype.onValue = function(f: ValueHandler, ...rest: any[]): Dispose {
return F.onValue(toFunction(f, rest), this)
}
Observable.prototype.onValues = function(f: ValuesHandler, ...rest: any[]): Dispose {
return F.onValues(toFunction(f, rest), this)
}
Observable.prototype.onError = function(f: ErrorHandler, ...rest: any[]): Dispose {
return F.onError(toFunction(f, rest), this)
}
Observable.prototype.onEnd = function(f: EndHandler, ...rest: any[]): Dispose {
return F.onEnd(toFunction(f, rest), this)
}
Observable.prototype.assign = function(obj: any, method: string, ...params: any[]): Dispose {
return F.onValue(toFunction(obj, [method, ...params]), this)
}
Observable.prototype.doAction = function(f: (val: A) => void, ...rest: any[]): Observable {
return F.doAction(toFunction(f, rest), this)
}
Observable.prototype.doError = function(f: (err: Error) => void, ...rest: any[]): Observable {
return F.doError(toFunction(f, rest), this)
}
Observable.prototype.doEnd = function(f: () => void, ...rest: any[]): Observable {
return F.doEnd(toFunction(f, rest), this)
}
Observable.prototype.doLog = function(label?: string): Observable {
return F.doLog(label, this)
}
Observable.prototype.log = function(label?: string): Dispose {
return F.log(label, this)
}
Observable.prototype.map = function(
project: Projection | Property,
...rest: any[]
): Observable {
const proj = toFunctionsPropAsIs(project, rest)
return F.isProperty(proj) ? F.sampleBy(this, proj) : F.map(proj, this)
}
Observable.prototype.mapError = function(
project: Projection,
...rest: any[]
): Observable {
return F.mapError(toFunction(project, rest), this)
}
Observable.prototype.mapEnd = function(f: EndProjection): Observable {
return F.mapEnd(f, this)
}
Observable.prototype.filter = function(
predicate: Predicate | Property,
...rest: any[]
): Observable {
return F.filter(toFunctionsPropAsIs(predicate, rest), this)
}
Observable.prototype.take = function(n: number): Observable {
return F.take(n, this)
}
Observable.prototype.takeUntil = function(trigger: Observable): Observable {
return F.takeUntil(trigger, this)
}
Observable.prototype.takeWhile = function(
f: Predicate | Property,
...rest: any[]
): Observable {
return F.takeWhile(toFunctionsPropAsIs(f, rest), this)
}
Observable.prototype.skip = function(n: number): Observable {
return F.skip(n, this)
}
Observable.prototype.skipUntil = function(trigger: Observable): Observable {
return F.skipUntil(trigger, this)
}
Observable.prototype.skipWhile = function(
f: Predicate | Property,
...rest: any[]
): Observable {
return F.skipWhile(toFunctionsPropAsIs(f, rest), this)
}
Observable.prototype.skipErrors = function(): Observable {
return F.skipErrors(this)
}
Observable.prototype.first = function(): Observable {
return F.first(this)
}
Observable.prototype.last = function(): Observable {
return F.last(this)
}
Observable.prototype.flatMapLatest = function(
project: Projection>,
...rest: any[]
): Observable {
return F.flatMapLatest(toFunction(project, rest), this)
}
Observable.prototype.flatMapFirst = function(
project: Projection>,
...rest: any[]
): Observable {
return F.flatMapFirst(toFunction(project, rest), this)
}
Observable.prototype.flatMap = function(
project: Projection>,
...rest: any[]
): Observable {
return F.flatMap(toFunction(project, rest), this)
}
Observable.prototype.flatMapConcat = function(
project: Projection>,
...rest: any[]
): Observable {
return F.flatMapConcat(toFunction(project, rest), this)
}
Observable.prototype.flatMapWithConcurrencyLimit = function(
limit: number,
project: Projection>,
...rest: any[]
): Observable {
return F.flatMapWithConcurrencyLimit(limit, toFunction(project, rest), this)
}
Observable.prototype.flatMapError = function(
project: Projection>,
...rest: any[]
): Observable {
return F.flatMapError(toFunction(project, rest), this)
}
Observable.prototype.startWith = function(value: A): Observable {
return F.startWith(value, this)
}
Observable.prototype.scan = function(seed: B, f: Accum, ...rest: any[]): Property {
return F.scan(seed, toFunction(f, rest), this)
}
Observable.prototype.fold = Observable.prototype.reduce = function(
seed: B,
f: Accum,
...rest: any[]
): Property {
return F.fold(seed, toFunction(f, rest), this)
}
Observable.prototype.flatScan = function(
seed: B,
f: FlatAccum,
...rest: any[]
): Property {
return F.flatScan(seed, toFunction(f, rest), this)
}
Observable.prototype.zip = function(
other: Observable,
f: (a: A, b: B) => C,
...rest: any[]
): EventStream {
return F.zipWith(toFunction(f, rest), [this, other])
}
Observable.prototype.merge = function(other: Observable): EventStream {
return F.mergeAll([this, other])
}
Observable.prototype.concat = function(other: Observable): EventStream {
return F.concatAll([this, other])
}
Observable.prototype.combine = function(
other: Observable,
f: (a: A, b: B) => C,
...rest: any[]
): Property {
return F.combineWith(toFunction(f, rest), [this, other] as any)
}
Observable.prototype.errors = function(): Observable {
return F.errors(this)
}
Observable.prototype.throttle = function(delay: number): Observable {
return F.throttle(delay, this)
}
Observable.prototype.bufferingThrottle = function(minimumInterval: number): Observable {
return F.bufferingThrottle(minimumInterval, this)
}
Observable.prototype.debounce = function(delay: number): Observable {
return F.debounce(delay, this)
}
Observable.prototype.debounceImmediate = function(delay: number): Observable {
return F.debounceImmediate(delay, this)
}
Observable.prototype.delay = function(delay: number): Observable {
return F.delay(delay, this)
}
Observable.prototype.slidingWindow = function(max: number, min?: number): Property {
return F.slidingWindow(min === undefined ? 0 : min, max, this)
}
Observable.prototype.skipDuplicates = function(isEqual?: Eq): Observable {
return isEqual === undefined ? F.skipEquals(this) : F.skipDuplicates(isEqual, this)
}
Observable.prototype.diff = function(start: A, f: F.Delta): Observable {
return F.diff(f, start, this)
}
Observable.prototype.toPromise = function(ctor?: PromiseConstructor): Promise {
return isFunction(ctor) ? F.toCustomPromise(ctor, this) : F.toPromise(this)
}
Observable.prototype.firstToPromise = function(ctor?: PromiseConstructor): Promise {
return isFunction(ctor) ? F.firstToCustomPromise(ctor, this) : F.firstToPromise(this)
}
EventStream.prototype.toProperty = function(initialValue?: A): Property {
return arguments.length === 0 ? F.toProperty(this) : F.toPropertyWith(initialValue, this)
}
// tslint:disable-next-line:align whitespace
;(EventStream.prototype as any).toEventStream = function(): EventStream {
return this
}
EventStream.prototype.bufferWithTime = function(delay: number): EventStream {
return F.bufferWithTime(delay, this) as EventStream
}
EventStream.prototype.bufferWithCount = function(count: number): EventStream {
return F.bufferWithCount(count, this) as EventStream
}
EventStream.prototype.bufferWithTimeOrCount = function(
delay: number,
count: number,
): EventStream {
return F.bufferWithTimeOrCount(delay, count, this) as EventStream
}
Property.prototype.sampledBy = function(
sampler: Observable,
f?: SampleFn,
...rest: any[]
): any {
const fn = toFunction(f === undefined ? (v: A, s: B) => v as any : f, rest)
return F.sampleWith(sampler, fn, this)
}
Property.prototype.sample = function(i: number): EventStream {
return F.sampleBy(F.interval(i, null as any), this) as EventStream
}
Property.prototype.and = function(other: Property): Property> {
return F.and(this, other) as Property>
}
Property.prototype.or = function(other: Property): Property> {
return F.or(this, other) as Property>
}
Property.prototype.not = function(): Property {
return F.not(this) as Property
}
const pProto = Property.prototype as any
pProto.toProperty = function(initialValue?: A): Property {
assert(arguments.length === 0, "No arguments supported")
return this
}
Property.prototype.toEventStream = function(): EventStream {
return F.toEventStream(this)
}
Property.prototype.changes = function(): EventStream {
return F.changes(this)
}
Bus.prototype.push = function(event: T | F.AnyEvent): void {
F.push(this, event)
}
Bus.prototype.error = function(err: Error): void {
F.pushError(this, err)
}
Bus.prototype.end = function(): void {
F.pushEnd(this)
}
Bus.prototype.plug = function(obs: Observable): F.Dispose {
return F.plug(this, obs)
}
// static operators
export { End, Error, Next } from "./Event"
export { EventStream } from "./EventStream"
export { Observable } from "./Observable"
export { Property } from "./Property"
export { Bus } from "./Bus"
export { Combineable, combineAsArray, combineTemplate } from "./operators/combine"
export { update } from "./operators/update"
export { when } from "./operators/when"
export { zipAsArray } from "./operators/zip"
export { fromArray } from "./sources/fromArray"
export { fromBinder } from "./sources/fromBinder"
export { fromPromise } from "./sources/fromPromise"
export { never } from "./sources/never"
export { repeat } from "./sources/repeat"
export { constant, once } from "./sources/single"
// classes and interfaces
export * from "./_interfaces"
// tslint:disable:no-shadowed-variable
export function later(delay: number): EventStream
export function later(delay: number, value: T): EventStream
export function later(delay: number, value?: T): EventStream {
return F.later(delay, value)
}
export function interval(period: number): EventStream
export function interval(period: number, value: T): EventStream
export function interval(period: number, value?: T): EventStream {
return F.interval(period, value)
}
export function fromEvent(target: EventTarget, event: string, ...args: any[]): EventStream {
return F.fromEvent(target, event, ...args)
}
export function fromPoll(interval: number, f: () => T | F.AnyEvent): EventStream {
return F.fromPoll(interval, f)
}
export function repeatedly(interval: number, events: Array>): EventStream {
return F.repeatedly(interval, events)
}
export function sequentially(
interval: number,
events: Array>,
): EventStream {
return F.sequentially(interval, events)
}
export function fromCallback(f: F.AsyncCallback, ...args: any[]): EventStream {
checkFunction(f)
return F.fromCallback(args.length > 0 ? f.bind(null, ...args) : f)
}
export function fromNodeCallback