import { Observable } from '../Observable'; import { Subscriber } from '../Subscriber'; import { Subscription } from '../Subscription'; import { Scheduler } from '../Scheduler'; import { tryCatch } from '../util/tryCatch'; import { errorObject } from '../util/errorObject'; import { AsyncSubject } from '../AsyncSubject'; /** * We need this JSDoc comment for affecting ESDoc. * @extends {Ignored} * @hide true */ export class BoundCallbackObservable extends Observable { subject: AsyncSubject; /* tslint:disable:max-line-length */ static create(callbackFunc: (callback: (result: R) => any) => any, selector?: void, scheduler?: Scheduler): () => Observable; static create(callbackFunc: (v1: T, callback: (result: R) => any) => any, selector?: void, scheduler?: Scheduler): (v1: T) => Observable; static create(callbackFunc: (v1: T, v2: T2, callback: (result: R) => any) => any, selector?: void, scheduler?: Scheduler): (v1: T, v2: T2) => Observable; static create(callbackFunc: (v1: T, v2: T2, v3: T3, callback: (result: R) => any) => any, selector?: void, scheduler?: Scheduler): (v1: T, v2: T2, v3: T3) => Observable; static create(callbackFunc: (v1: T, v2: T2, v3: T3, v4: T4, callback: (result: R) => any) => any, selector?: void, scheduler?: Scheduler): (v1: T, v2: T2, v3: T3, v4: T4) => Observable; static create(callbackFunc: (v1: T, v2: T2, v3: T3, v4: T4, v5: T5, callback: (result: R) => any) => any, selector?: void, scheduler?: Scheduler): (v1: T, v2: T2, v3: T3, v4: T4, v5: T5) => Observable; static create(callbackFunc: (v1: T, v2: T2, v3: T3, v4: T4, v5: T5, v6: T6, callback: (result: R) => any) => any, selector?: void, scheduler?: Scheduler): (v1: T, v2: T2, v3: T3, v4: T4, v5: T5, v6: T6) => Observable; static create(callbackFunc: (callback: (...args: any[]) => any) => any, selector: (...args: any[]) => R, scheduler?: Scheduler): () => Observable; static create(callbackFunc: (v1: T, callback: (...args: any[]) => any) => any, selector: (...args: any[]) => R, scheduler?: Scheduler): (v1: T) => Observable; static create(callbackFunc: (v1: T, v2: T2, callback: (...args: any[]) => any) => any, selector: (...args: any[]) => R, scheduler?: Scheduler): (v1: T, v2: T2) => Observable; static create(callbackFunc: (v1: T, v2: T2, v3: T3, callback: (...args: any[]) => any) => any, selector: (...args: any[]) => R, scheduler?: Scheduler): (v1: T, v2: T2, v3: T3) => Observable; static create(callbackFunc: (v1: T, v2: T2, v3: T3, v4: T4, callback: (...args: any[]) => any) => any, selector: (...args: any[]) => R, scheduler?: Scheduler): (v1: T, v2: T2, v3: T3, v4: T4) => Observable; static create(callbackFunc: (v1: T, v2: T2, v3: T3, v4: T4, v5: T5, callback: (...args: any[]) => any) => any, selector: (...args: any[]) => R, scheduler?: Scheduler): (v1: T, v2: T2, v3: T3, v4: T4, v5: T5) => Observable; static create(callbackFunc: (v1: T, v2: T2, v3: T3, v4: T4, v5: T5, v6: T6, callback: (...args: any[]) => any) => any, selector: (...args: any[]) => R, scheduler?: Scheduler): (v1: T, v2: T2, v3: T3, v4: T4, v5: T5, v6: T6) => Observable; static create(callbackFunc: Function, selector?: void, scheduler?: Scheduler): (...args: any[]) => Observable; static create(callbackFunc: Function, selector?: (...args: any[]) => T, scheduler?: Scheduler): (...args: any[]) => Observable; /* tslint:enable:max-line-length */ /** * Converts a callback API to a function that returns an Observable. * * Give it a function `f` of type `f(x, callback)` and * it will return a function `g` that when called as `g(x)` will output an * Observable. * * `bindCallback` is not an operator because its input and output are not * Observables. The input is a function `func` with some parameters, but the * last parameter must be a callback function that `func` calls when it is * done. The output of `bindCallback` is a function that takes the same * parameters as `func`, except the last one (the callback). When the output * function is called with arguments, it will return an Observable where the * results will be delivered to. * * @example Convert jQuery's getJSON to an Observable API * // Suppose we have jQuery.getJSON('/my/url', callback) * var getJSONAsObservable = Rx.Observable.bindCallback(jQuery.getJSON); * var result = getJSONAsObservable('/my/url'); * result.subscribe(x => console.log(x), e => console.error(e)); * * @see {@link bindNodeCallback} * @see {@link from} * @see {@link fromPromise} * * @param {function} func Function with a callback as the last parameter. * @param {function} [selector] A function which takes the arguments from the * callback and maps those a value to emit on the output Observable. * @param {Scheduler} [scheduler] The scheduler on which to schedule the * callbacks. * @return {function(...params: *): Observable} A function which returns the * Observable that delivers the same values the callback would deliver. * @static true * @name bindCallback * @owner Observable */ static create(func: Function, selector: Function | void = undefined, scheduler?: Scheduler): (...args: any[]) => Observable { return (...args: any[]): Observable => { return new BoundCallbackObservable(func, selector, args, scheduler); }; } constructor(private callbackFunc: Function, private selector: Function, private args: any[], private scheduler: Scheduler) { super(); } protected _subscribe(subscriber: Subscriber): Subscription { const callbackFunc = this.callbackFunc; const args = this.args; const scheduler = this.scheduler; let subject = this.subject; if (!scheduler) { if (!subject) { subject = this.subject = new AsyncSubject(); const handler = function handlerFn(this: any, ...innerArgs: any[]) { const source = (handlerFn).source; const { selector, subject } = source; if (selector) { const result = tryCatch(selector).apply(this, innerArgs); if (result === errorObject) { subject.error(errorObject.e); } else { subject.next(result); subject.complete(); } } else { subject.next(innerArgs.length === 1 ? innerArgs[0] : innerArgs); subject.complete(); } }; // use named function instance to avoid closure. (handler).source = this; const result = tryCatch(callbackFunc).apply(this, args.concat(handler)); if (result === errorObject) { subject.error(errorObject.e); } } return subject.subscribe(subscriber); } else { return scheduler.schedule(BoundCallbackObservable.dispatch, 0, { source: this, subscriber }); } } static dispatch(state: { source: BoundCallbackObservable, subscriber: Subscriber }) { const self = (this); const { source, subscriber } = state; const { callbackFunc, args, scheduler } = source; let subject = source.subject; if (!subject) { subject = source.subject = new AsyncSubject(); const handler = function handlerFn(this: any, ...innerArgs: any[]) { const source = (handlerFn).source; const { selector, subject } = source; if (selector) { const result = tryCatch(selector).apply(this, innerArgs); if (result === errorObject) { self.add(scheduler.schedule(dispatchError, 0, { err: errorObject.e, subject })); } else { self.add(scheduler.schedule(dispatchNext, 0, { value: result, subject })); } } else { const value = innerArgs.length === 1 ? innerArgs[0] : innerArgs; self.add(scheduler.schedule(dispatchNext, 0, { value, subject })); } }; // use named function to pass values in without closure (handler).source = source; const result = tryCatch(callbackFunc).apply(this, args.concat(handler)); if (result === errorObject) { subject.error(errorObject.e); } } self.add(subject.subscribe(subscriber)); } } interface DispatchNextArg { subject: AsyncSubject; value: T; } function dispatchNext(arg: DispatchNextArg) { const { value, subject } = arg; subject.next(value); subject.complete(); } interface DispatchErrorArg { subject: AsyncSubject; err: any; } function dispatchError(arg: DispatchErrorArg) { const { err, subject } = arg; subject.error(err); }