1 import { Observable } from '../Observable';
2 import { AsyncSubject } from '../AsyncSubject';
3 import { Subscriber } from '../Subscriber';
4 import { SchedulerAction, SchedulerLike } from '../types';
5 import { map } from '../operators/map';
6 import { canReportError } from '../util/canReportError';
7 import { isScheduler } from '../util/isScheduler';
8 import { isArray } from '../util/isArray';
10 /* tslint:disable:max-line-length */
11 /** @deprecated resultSelector is deprecated, pipe to map instead */
12 export function bindNodeCallback(callbackFunc: Function, resultSelector: Function, scheduler?: SchedulerLike): (...args: any[]) => Observable<any>;
14 export function bindNodeCallback<R1, R2, R3, R4>(callbackFunc: (callback: (err: any, res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any, scheduler?: SchedulerLike): (...args: any[]) => Observable<any[]>;
15 export function bindNodeCallback<R1, R2, R3>(callbackFunc: (callback: (err: any, res1: R1, res2: R2, res3: R3) => any) => any, scheduler?: SchedulerLike): () => Observable<[R1, R2, R3]>;
16 export function bindNodeCallback<R1, R2>(callbackFunc: (callback: (err: any, res1: R1, res2: R2) => any) => any, scheduler?: SchedulerLike): () => Observable<[R1, R2]>;
17 export function bindNodeCallback<R1>(callbackFunc: (callback: (err: any, res1: R1) => any) => any, scheduler?: SchedulerLike): () => Observable<R1>;
18 export function bindNodeCallback(callbackFunc: (callback: (err: any) => any) => any, scheduler?: SchedulerLike): () => Observable<void>;
20 export function bindNodeCallback<A1, R1, R2, R3, R4>(callbackFunc: (arg1: A1, callback: (err: any, res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any, scheduler?: SchedulerLike): (...args: any[]) => Observable<any[]>;
21 export function bindNodeCallback<A1, R1, R2, R3>(callbackFunc: (arg1: A1, callback: (err: any, res1: R1, res2: R2, res3: R3) => any) => any, scheduler?: SchedulerLike): (arg1: A1) => Observable<[R1, R2, R3]>;
22 export function bindNodeCallback<A1, R1, R2>(callbackFunc: (arg1: A1, callback: (err: any, res1: R1, res2: R2) => any) => any, scheduler?: SchedulerLike): (arg1: A1) => Observable<[R1, R2]>;
23 export function bindNodeCallback<A1, R1>(callbackFunc: (arg1: A1, callback: (err: any, res1: R1) => any) => any, scheduler?: SchedulerLike): (arg1: A1) => Observable<R1>;
24 export function bindNodeCallback<A1>(callbackFunc: (arg1: A1, callback: (err: any) => any) => any, scheduler?: SchedulerLike): (arg1: A1) => Observable<void>;
26 export function bindNodeCallback<A1, A2, R1, R2, R3, R4>(callbackFunc: (arg1: A1, arg2: A2, callback: (err: any, res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any, scheduler?: SchedulerLike): (...args: any[]) => Observable<any[]>;
27 export function bindNodeCallback<A1, A2, R1, R2, R3>(callbackFunc: (arg1: A1, arg2: A2, callback: (err: any, res1: R1, res2: R2, res3: R3) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2) => Observable<[R1, R2, R3]>;
28 export function bindNodeCallback<A1, A2, R1, R2>(callbackFunc: (arg1: A1, arg2: A2, callback: (err: any, res1: R1, res2: R2) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2) => Observable<[R1, R2]>;
29 export function bindNodeCallback<A1, A2, R1>(callbackFunc: (arg1: A1, arg2: A2, callback: (err: any, res1: R1) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2) => Observable<R1>;
30 export function bindNodeCallback<A1, A2>(callbackFunc: (arg1: A1, arg2: A2, callback: (err: any) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2) => Observable<void>;
32 export function bindNodeCallback<A1, A2, A3, R1, R2, R3, R4>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, callback: (err: any, res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any, scheduler?: SchedulerLike): (...args: any[]) => Observable<any[]>;
33 export function bindNodeCallback<A1, A2, A3, R1, R2, R3>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, callback: (err: any, res1: R1, res2: R2, res3: R3) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3) => Observable<[R1, R2, R3]>;
34 export function bindNodeCallback<A1, A2, A3, R1, R2>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, callback: (err: any, res1: R1, res2: R2) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3) => Observable<[R1, R2]>;
35 export function bindNodeCallback<A1, A2, A3, R1>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, callback: (err: any, res1: R1) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3) => Observable<R1>;
36 export function bindNodeCallback<A1, A2, A3>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, callback: (err: any) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3) => Observable<void>;
38 export function bindNodeCallback<A1, A2, A3, A4, R1, R2, R3, R4>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (err: any, res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any, scheduler?: SchedulerLike): (...args: any[]) => Observable<any[]>;
39 export function bindNodeCallback<A1, A2, A3, A4, R1, R2, R3>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (err: any, 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 bindNodeCallback<A1, A2, A3, A4, R1, R2>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (err: any, res1: R1, res2: R2) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Observable<[R1, R2]>;
41 export function bindNodeCallback<A1, A2, A3, A4, R1>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (err: any, res1: R1) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Observable<R1>;
42 export function bindNodeCallback<A1, A2, A3, A4>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (err: any) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Observable<void>;
44 export function bindNodeCallback<A1, A2, A3, A4, A5, R1, R2, R3, R4>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (err: any, res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any, scheduler?: SchedulerLike): (...args: any[]) => Observable<any[]>;
45 export function bindNodeCallback<A1, A2, A3, A4, A5, R1, R2, R3>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (err: any, 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 bindNodeCallback<A1, A2, A3, A4, A5, R1, R2>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (err: any, res1: R1, res2: R2) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Observable<[R1, R2]>;
47 export function bindNodeCallback<A1, A2, A3, A4, A5, R1>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (err: any, res1: R1) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Observable<R1>;
48 export function bindNodeCallback<A1, A2, A3, A4, A5>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (err: any) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Observable<void>; /* tslint:enable:max-line-length */
50 export function bindNodeCallback(callbackFunc: Function, scheduler?: SchedulerLike): (...args: any[]) => Observable<any[]>;
52 * Converts a Node.js-style callback API to a function that returns an
55 * <span class="informal">It's just like {@link bindCallback}, but the
56 * callback is expected to be of type `callback(error, result)`.</span>
58 * `bindNodeCallback` is not an operator because its input and output are not
59 * Observables. The input is a function `func` with some parameters, but the
60 * last parameter must be a callback function that `func` calls when it is
61 * done. The callback function is expected to follow Node.js conventions,
62 * where the first argument to the callback is an error object, signaling
63 * whether call was successful. If that object is passed to callback, it means
64 * something went wrong.
66 * The output of `bindNodeCallback` is a function that takes the same
67 * parameters as `func`, except the last one (the callback). When the output
68 * function is called with arguments, it will return an Observable.
69 * If `func` calls its callback with error parameter present, Observable will
70 * error with that value as well. If error parameter is not passed, Observable will emit
71 * second parameter. If there are more parameters (third and so on),
72 * Observable will emit an array with all arguments, except first error argument.
74 * Note that `func` will not be called at the same time output function is,
75 * but rather whenever resulting Observable is subscribed. By default call to
76 * `func` will happen synchronously after subscription, but that can be changed
77 * with proper `scheduler` provided as optional third parameter. {@link SchedulerLike}
78 * can also control when values from callback will be emitted by Observable.
79 * To find out more, check out documentation for {@link bindCallback}, where
80 * {@link SchedulerLike} works exactly the same.
82 * As in {@link bindCallback}, context (`this` property) of input function will be set to context
83 * of returned function, when it is called.
85 * After Observable emits value, it will complete immediately. This means
86 * even if `func` calls callback again, values from second and consecutive
87 * calls will never appear on the stream. If you need to handle functions
88 * that call callbacks multiple times, check out {@link fromEvent} or
89 * {@link fromEventPattern} instead.
91 * Note that `bindNodeCallback` can be used in non-Node.js environments as well.
92 * "Node.js-style" callbacks are just a convention, so if you write for
93 * browsers or any other environment and API you use implements that callback style,
94 * `bindNodeCallback` can be safely used on that API functions as well.
96 * Remember that Error object passed to callback does not have to be an instance
97 * of JavaScript built-in `Error` object. In fact, it does not even have to an object.
98 * Error parameter of callback function is interpreted as "present", when value
99 * of that parameter is truthy. It could be, for example, non-zero number, non-empty
100 * string or boolean `true`. In all of these cases resulting Observable would error
101 * with that value. This means usually regular style callbacks will fail very often when
102 * `bindNodeCallback` is used. If your Observable errors much more often then you
103 * would expect, check if callback really is called in Node.js-style and, if not,
104 * switch to {@link bindCallback} instead.
106 * Note that even if error parameter is technically present in callback, but its value
107 * is falsy, it still won't appear in array emitted by Observable.
110 * ### Read a file from the filesystem and get the data as an Observable
112 * import * as fs from 'fs';
113 * const readFileAsObservable = bindNodeCallback(fs.readFile);
114 * const result = readFileAsObservable('./roadNames.txt', 'utf8');
115 * result.subscribe(x => console.log(x), e => console.error(e));
118 * ### Use on function calling callback with multiple arguments
120 * someFunction((err, a, b) => {
121 * console.log(err); // null
122 * console.log(a); // 5
123 * console.log(b); // "some string"
125 * const boundSomeFunction = bindNodeCallback(someFunction);
126 * boundSomeFunction()
127 * .subscribe(value => {
128 * console.log(value); // [5, "some string"]
132 * ### Use on function calling callback in regular style
134 * someFunction(a => {
135 * console.log(a); // 5
137 * const boundSomeFunction = bindNodeCallback(someFunction);
138 * boundSomeFunction()
140 * value => {} // never gets called
141 * err => console.log(err) // 5
145 * @see {@link bindCallback}
148 * @param {function} func Function with a Node.js-style callback as the last parameter.
149 * @param {SchedulerLike} [scheduler] The scheduler on which to schedule the
151 * @return {function(...params: *): Observable} A function which returns the
152 * Observable that delivers the same values the Node.js callback would
154 * @name bindNodeCallback
156 export function bindNodeCallback<T>(
157 callbackFunc: Function,
158 resultSelector: Function|SchedulerLike,
159 scheduler?: SchedulerLike
160 ): (...args: any[]) => Observable<T> {
162 if (resultSelector) {
163 if (isScheduler(resultSelector)) {
164 scheduler = resultSelector;
167 return (...args: any[]) => bindNodeCallback(callbackFunc, scheduler)(...args).pipe(
168 map(args => isArray(args) ? resultSelector(...args) : resultSelector(args))
173 return function(this: any, ...args: any[]): Observable<T> {
174 const params: ParamsState<T> = {
181 return new Observable<T>(subscriber => {
182 const { context } = params;
183 let { subject } = params;
186 subject = params.subject = new AsyncSubject<T>();
187 const handler = (...innerArgs: any[]) => {
188 const err = innerArgs.shift();
195 subject.next(innerArgs.length <= 1 ? innerArgs[0] : innerArgs);
200 callbackFunc.apply(context, [...args, handler]);
202 if (canReportError(subject)) {
209 return subject.subscribe(subscriber);
211 return scheduler.schedule<DispatchState<T>>(dispatch, 0, { params, subscriber, context });
217 interface DispatchState<T> {
218 subscriber: Subscriber<T>;
220 params: ParamsState<T>;
223 interface ParamsState<T> {
224 callbackFunc: Function;
226 scheduler: SchedulerLike;
227 subject: AsyncSubject<T>;
231 function dispatch<T>(this: SchedulerAction<DispatchState<T>>, state: DispatchState<T>) {
232 const { params, subscriber, context } = state;
233 const { callbackFunc, args, scheduler } = params;
234 let subject = params.subject;
237 subject = params.subject = new AsyncSubject<T>();
239 const handler = (...innerArgs: any[]) => {
240 const err = innerArgs.shift();
242 this.add(scheduler.schedule<DispatchErrorArg<T>>(dispatchError, 0, { err, subject }));
244 const value = innerArgs.length <= 1 ? innerArgs[0] : innerArgs;
245 this.add(scheduler.schedule<DispatchNextArg<T>>(dispatchNext, 0, { value, subject }));
250 callbackFunc.apply(context, [...args, handler]);
252 this.add(scheduler.schedule<DispatchErrorArg<T>>(dispatchError, 0, { err, subject }));
256 this.add(subject.subscribe(subscriber));
259 interface DispatchNextArg<T> {
260 subject: AsyncSubject<T>;
264 function dispatchNext<T>(arg: DispatchNextArg<T>) {
265 const { value, subject } = arg;
270 interface DispatchErrorArg<T> {
271 subject: AsyncSubject<T>;
275 function dispatchError<T>(arg: DispatchErrorArg<T>) {
276 const { err, subject } = arg;