1 import { SchedulerLike, SchedulerAction } from '../types';
2 import { Observable } from '../Observable';
3 import { AsyncSubject } from '../AsyncSubject';
4 import { Subscriber } from '../Subscriber';
5 import { map } from '../operators/map';
6 import { canReportError } from '../util/canReportError';
7 import { isArray } from '../util/isArray';
8 import { isScheduler } from '../util/isScheduler';
10 // tslint:disable:max-line-length
11 /** @deprecated resultSelector is no longer supported, use a mapping function. */
12 export function bindCallback(callbackFunc: Function, resultSelector: Function, scheduler?: SchedulerLike): (...args: any[]) => Observable<any>;
14 export function bindCallback<R1, R2, R3, R4>(callbackFunc: (callback: (res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any, scheduler?: SchedulerLike): () => Observable<any[]>;
15 export function bindCallback<R1, R2, R3>(callbackFunc: (callback: (res1: R1, res2: R2, res3: R3) => any) => any, scheduler?: SchedulerLike): () => Observable<[R1, R2, R3]>;
16 export function bindCallback<R1, R2>(callbackFunc: (callback: (res1: R1, res2: R2) => any) => any, scheduler?: SchedulerLike): () => Observable<[R1, R2]>;
17 export function bindCallback<R1>(callbackFunc: (callback: (res1: R1) => any) => any, scheduler?: SchedulerLike): () => Observable<R1>;
18 export function bindCallback(callbackFunc: (callback: () => any) => any, scheduler?: SchedulerLike): () => Observable<void>;
20 export function bindCallback<A1, R1, R2, R3, R4>(callbackFunc: (arg1: A1, callback: (res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any, scheduler?: SchedulerLike): (arg1: A1) => Observable<any[]>;
21 export function bindCallback<A1, R1, R2, R3>(callbackFunc: (arg1: A1, callback: (res1: R1, res2: R2, res3: R3) => any) => any, scheduler?: SchedulerLike): (arg1: A1) => Observable<[R1, R2, R3]>;
22 export function bindCallback<A1, R1, R2>(callbackFunc: (arg1: A1, callback: (res1: R1, res2: R2) => any) => any, scheduler?: SchedulerLike): (arg1: A1) => Observable<[R1, R2]>;
23 export function bindCallback<A1, R1>(callbackFunc: (arg1: A1, callback: (res1: R1) => any) => any, scheduler?: SchedulerLike): (arg1: A1) => Observable<R1>;
24 export function bindCallback<A1>(callbackFunc: (arg1: A1, callback: () => any) => any, scheduler?: SchedulerLike): (arg1: A1) => Observable<void>;
26 export function bindCallback<A1, A2, R1, R2, R3, R4>(callbackFunc: (arg1: A1, arg2: A2, callback: (res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2) => Observable<any[]>;
27 export function bindCallback<A1, A2, R1, R2, R3>(callbackFunc: (arg1: A1, arg2: A2, callback: (res1: R1, res2: R2, res3: R3) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2) => Observable<[R1, R2, R3]>;
28 export function bindCallback<A1, A2, R1, R2>(callbackFunc: (arg1: A1, arg2: A2, callback: (res1: R1, res2: R2) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2) => Observable<[R1, R2]>;
29 export function bindCallback<A1, A2, R1>(callbackFunc: (arg1: A1, arg2: A2, callback: (res1: R1) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2) => Observable<R1>;
30 export function bindCallback<A1, A2>(callbackFunc: (arg1: A1, arg2: A2, callback: () => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2) => Observable<void>;
32 export function bindCallback<A1, A2, A3, R1, R2, R3, R4>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, callback: (res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3) => Observable<any[]>;
33 export function bindCallback<A1, A2, A3, R1, R2, R3>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, callback: (res1: R1, res2: R2, res3: R3) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3) => Observable<[R1, R2, R3]>;
34 export function bindCallback<A1, A2, A3, R1, R2>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, callback: (res1: R1, res2: R2) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3) => Observable<[R1, R2]>;
35 export function bindCallback<A1, A2, A3, R1>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, callback: (res1: R1) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3) => Observable<R1>;
36 export function bindCallback<A1, A2, A3>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, callback: () => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3) => Observable<void>;
38 export function bindCallback<A1, A2, A3, A4, R1, R2, R3, R4>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Observable<any[]>;
39 export function bindCallback<A1, A2, A3, A4, R1, R2, R3>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (res1: R1, res2: R2, res3: R3) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Observable<[R1, R2, R3]>;
40 export function bindCallback<A1, A2, A3, A4, R1, R2>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (res1: R1, res2: R2) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Observable<[R1, R2]>;
41 export function bindCallback<A1, A2, A3, A4, R1>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (res1: R1) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Observable<R1>;
42 export function bindCallback<A1, A2, A3, A4>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: () => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Observable<void>;
44 export function bindCallback<A1, A2, A3, A4, A5, R1, R2, R3, R4>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Observable<any[]>;
45 export function bindCallback<A1, A2, A3, A4, A5, R1, R2, R3>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (res1: R1, res2: R2, res3: R3) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Observable<[R1, R2, R3]>;
46 export function bindCallback<A1, A2, A3, A4, A5, R1, R2>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (res1: R1, res2: R2) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Observable<[R1, R2]>;
47 export function bindCallback<A1, A2, A3, A4, A5, R1>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (res1: R1) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Observable<R1>;
48 export function bindCallback<A1, A2, A3, A4, A5>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: () => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Observable<void>;
50 export function bindCallback<A, R>(callbackFunc: (...args: Array<A | ((result: R) => any)>) => any, scheduler?: SchedulerLike): (...args: A[]) => Observable<R>;
51 export function bindCallback<A, R>(callbackFunc: (...args: Array<A | ((...results: R[]) => any)>) => any, scheduler?: SchedulerLike): (...args: A[]) => Observable<R[]>;
53 export function bindCallback(callbackFunc: Function, scheduler?: SchedulerLike): (...args: any[]) => Observable<any>;
55 // tslint:enable:max-line-length
58 * Converts a callback API to a function that returns an Observable.
60 * <span class="informal">Give it a function `f` of type `f(x, callback)` and
61 * it will return a function `g` that when called as `g(x)` will output an
64 * `bindCallback` is not an operator because its input and output are not
65 * Observables. The input is a function `func` with some parameters. The
66 * last parameter must be a callback function that `func` calls when it is
69 * The output of `bindCallback` is a function that takes the same parameters
70 * as `func`, except the last one (the callback). When the output function
71 * is called with arguments it will return an Observable. If function `func`
72 * calls its callback with one argument, the Observable will emit that value.
73 * If on the other hand the callback is called with multiple values the resulting
74 * Observable will emit an array with said values as arguments.
76 * It is **very important** to remember that input function `func` is not called
77 * when the output function is, but rather when the Observable returned by the output
78 * function is subscribed. This means if `func` makes an AJAX request, that request
79 * will be made every time someone subscribes to the resulting Observable, but not before.
81 * The last optional parameter - `scheduler` - can be used to control when the call
82 * to `func` happens after someone subscribes to Observable, as well as when results
83 * passed to callback will be emitted. By default, the subscription to an Observable calls `func`
84 * synchronously, but using {@link asyncScheduler} as the last parameter will defer the call to `func`,
85 * just like wrapping the call in `setTimeout` with a timeout of `0` would. If you were to use the async Scheduler
86 * and call `subscribe` on the output Observable, all function calls that are currently executing
87 * will end before `func` is invoked.
89 * By default, results passed to the callback are emitted immediately after `func` invokes the callback.
90 * In particular, if the callback is called synchronously, then the subscription of the resulting Observable
91 * will call the `next` function synchronously as well. If you want to defer that call,
92 * you may use {@link asyncScheduler} just as before. This means that by using `Scheduler.async` you can
93 * ensure that `func` always calls its callback asynchronously, thus avoiding terrifying Zalgo.
95 * Note that the Observable created by the output function will always emit a single value
96 * and then complete immediately. If `func` calls the callback multiple times, values from subsequent
97 * calls will not appear in the stream. If you need to listen for multiple calls,
98 * you probably want to use {@link fromEvent} or {@link fromEventPattern} instead.
100 * If `func` depends on some context (`this` property) and is not already bound, the context of `func`
101 * will be the context that the output function has at call time. In particular, if `func`
102 * is called as a method of some objec and if `func` is not already bound, in order to preserve the context
103 * it is recommended that the context of the output function is set to that object as well.
105 * If the input function calls its callback in the "node style" (i.e. first argument to callback is
106 * optional error parameter signaling whether the call failed or not), {@link bindNodeCallback}
107 * provides convenient error handling and probably is a better choice.
108 * `bindCallback` will treat such functions the same as any other and error parameters
109 * (whether passed or not) will always be interpreted as regular callback argument.
113 * ### Convert jQuery's getJSON to an Observable API
115 * import { bindCallback } from 'rxjs';
116 * import * as jQuery from 'jquery';
118 * // Suppose we have jQuery.getJSON('/my/url', callback)
119 * const getJSONAsObservable = bindCallback(jQuery.getJSON);
120 * const result = getJSONAsObservable('/my/url');
121 * result.subscribe(x => console.log(x), e => console.error(e));
124 * ### Receive an array of arguments passed to a callback
126 * import { bindCallback } from 'rxjs';
128 * const someFunction = (a, b, c) => {
129 * console.log(a); // 5
130 * console.log(b); // 'some string'
131 * console.log(c); // {someProperty: 'someValue'}
134 * const boundSomeFunction = bindCallback(someFunction);
135 * boundSomeFunction().subscribe(values => {
136 * console.log(values) // [5, 'some string', {someProperty: 'someValue'}]
140 * ### Compare behaviour with and without async Scheduler
142 * import { bindCallback } from 'rxjs';
144 * function iCallMyCallbackSynchronously(cb) {
148 * const boundSyncFn = bindCallback(iCallMyCallbackSynchronously);
149 * const boundAsyncFn = bindCallback(iCallMyCallbackSynchronously, null, Rx.Scheduler.async);
151 * boundSyncFn().subscribe(() => console.log('I was sync!'));
152 * boundAsyncFn().subscribe(() => console.log('I was async!'));
153 * console.log('This happened...');
157 * // This happened...
161 * ### Use bindCallback on an object method
163 * import { bindCallback } from 'rxjs';
165 * const boundMethod = bindCallback(someObject.methodWithCallback);
166 * boundMethod.call(someObject) // make sure methodWithCallback has access to someObject
167 * .subscribe(subscriber);
170 * @see {@link bindNodeCallback}
173 * @param {function} func A function with a callback as the last parameter.
174 * @param {SchedulerLike} [scheduler] The scheduler on which to schedule the
176 * @return {function(...params: *): Observable} A function which returns the
177 * Observable that delivers the same values the callback would deliver.
180 export function bindCallback<T>(
181 callbackFunc: Function,
182 resultSelector?: Function|SchedulerLike,
183 scheduler?: SchedulerLike
184 ): (...args: any[]) => Observable<T> {
185 if (resultSelector) {
186 if (isScheduler(resultSelector)) {
187 scheduler = resultSelector;
190 return (...args: any[]) => bindCallback(callbackFunc, scheduler)(...args).pipe(
191 map((args) => isArray(args) ? resultSelector(...args) : resultSelector(args)),
196 return function (this: any, ...args: any[]): Observable<T> {
197 const context = this;
198 let subject: AsyncSubject<T>;
205 return new Observable<T>(subscriber => {
208 subject = new AsyncSubject<T>();
209 const handler = (...innerArgs: any[]) => {
210 subject.next(innerArgs.length <= 1 ? innerArgs[0] : innerArgs);
215 callbackFunc.apply(context, [...args, handler]);
217 if (canReportError(subject)) {
224 return subject.subscribe(subscriber);
226 const state: DispatchState<T> = {
227 args, subscriber, params,
229 return scheduler.schedule<DispatchState<T>>(dispatch, 0, state);
235 interface DispatchState<T> {
237 subscriber: Subscriber<T>;
238 params: ParamsContext<T>;
241 interface ParamsContext<T> {
242 callbackFunc: Function;
243 scheduler: SchedulerLike;
245 subject: AsyncSubject<T>;
248 function dispatch<T>(this: SchedulerAction<DispatchState<T>>, state: DispatchState<T>) {
250 const { args, subscriber, params } = state;
251 const { callbackFunc, context, scheduler } = params;
252 let { subject } = params;
254 subject = params.subject = new AsyncSubject<T>();
256 const handler = (...innerArgs: any[]) => {
257 const value = innerArgs.length <= 1 ? innerArgs[0] : innerArgs;
258 this.add(scheduler.schedule<NextState<T>>(dispatchNext, 0, { value, subject }));
262 callbackFunc.apply(context, [...args, handler]);
268 this.add(subject.subscribe(subscriber));
271 interface NextState<T> {
272 subject: AsyncSubject<T>;
276 function dispatchNext<T>(this: SchedulerAction<NextState<T>>, state: NextState<T>) {
277 const { value, subject } = state;
282 interface ErrorState<T> {
283 subject: AsyncSubject<T>;
287 function dispatchError<T>(this: SchedulerAction<ErrorState<T>>, state: ErrorState<T>) {
288 const { err, subject } = state;