Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / rxjs / src / internal / observable / bindNodeCallback.ts
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';
9
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>;
13
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>;
19
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>;
25
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>;
31
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>;
37
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>;
43
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 */
49
50 export function bindNodeCallback(callbackFunc: Function, scheduler?: SchedulerLike): (...args: any[]) => Observable<any[]>;
51 /**
52  * Converts a Node.js-style callback API to a function that returns an
53  * Observable.
54  *
55  * <span class="informal">It's just like {@link bindCallback}, but the
56  * callback is expected to be of type `callback(error, result)`.</span>
57  *
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.
65  *
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.
73  *
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.
81  *
82  * As in {@link bindCallback}, context (`this` property) of input function will be set to context
83  * of returned function, when it is called.
84  *
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.
90  *
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.
95  *
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.
105  *
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.
108  *
109  * ## Examples
110  * ###  Read a file from the filesystem and get the data as an Observable
111  * ```ts
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));
116  * ```
117  *
118  * ### Use on function calling callback with multiple arguments
119  * ```ts
120  * someFunction((err, a, b) => {
121  *   console.log(err); // null
122  *   console.log(a); // 5
123  *   console.log(b); // "some string"
124  * });
125  * const boundSomeFunction = bindNodeCallback(someFunction);
126  * boundSomeFunction()
127  * .subscribe(value => {
128  *   console.log(value); // [5, "some string"]
129  * });
130  * ```
131  *
132  * ### Use on function calling callback in regular style
133  * ```ts
134  * someFunction(a => {
135  *   console.log(a); // 5
136  * });
137  * const boundSomeFunction = bindNodeCallback(someFunction);
138  * boundSomeFunction()
139  * .subscribe(
140  *   value => {}             // never gets called
141  *   err => console.log(err) // 5
142  * );
143  * ```
144  *
145  * @see {@link bindCallback}
146  * @see {@link from}
147  *
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
150  * callbacks.
151  * @return {function(...params: *): Observable} A function which returns the
152  * Observable that delivers the same values the Node.js callback would
153  * deliver.
154  * @name bindNodeCallback
155  */
156 export function bindNodeCallback<T>(
157   callbackFunc: Function,
158   resultSelector: Function|SchedulerLike,
159   scheduler?: SchedulerLike
160 ): (...args: any[]) => Observable<T> {
161
162   if (resultSelector) {
163     if (isScheduler(resultSelector)) {
164       scheduler = resultSelector;
165     } else {
166       // DEPRECATED PATH
167       return (...args: any[]) => bindNodeCallback(callbackFunc, scheduler)(...args).pipe(
168         map(args => isArray(args) ? resultSelector(...args) : resultSelector(args))
169       );
170     }
171   }
172
173   return function(this: any, ...args: any[]): Observable<T> {
174     const params: ParamsState<T> = {
175       subject: undefined,
176       args,
177       callbackFunc,
178       scheduler,
179       context: this,
180     };
181     return new Observable<T>(subscriber => {
182       const { context } = params;
183       let { subject } = params;
184       if (!scheduler) {
185         if (!subject) {
186           subject = params.subject = new AsyncSubject<T>();
187           const handler = (...innerArgs: any[]) => {
188             const err = innerArgs.shift();
189
190             if (err) {
191               subject.error(err);
192               return;
193             }
194
195             subject.next(innerArgs.length <= 1 ? innerArgs[0] : innerArgs);
196             subject.complete();
197           };
198
199           try {
200             callbackFunc.apply(context, [...args, handler]);
201           } catch (err) {
202             if (canReportError(subject)) {
203               subject.error(err);
204             } else {
205               console.warn(err);
206             }
207           }
208         }
209         return subject.subscribe(subscriber);
210       } else {
211         return scheduler.schedule<DispatchState<T>>(dispatch, 0, { params, subscriber, context });
212       }
213     });
214   };
215 }
216
217 interface DispatchState<T> {
218   subscriber: Subscriber<T>;
219   context: any;
220   params: ParamsState<T>;
221 }
222
223 interface ParamsState<T> {
224   callbackFunc: Function;
225   args: any[];
226   scheduler: SchedulerLike;
227   subject: AsyncSubject<T>;
228   context: any;
229 }
230
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;
235
236   if (!subject) {
237     subject = params.subject = new AsyncSubject<T>();
238
239     const handler = (...innerArgs: any[]) => {
240       const err = innerArgs.shift();
241       if (err) {
242         this.add(scheduler.schedule<DispatchErrorArg<T>>(dispatchError, 0, { err, subject }));
243       } else {
244         const value = innerArgs.length <= 1 ? innerArgs[0] : innerArgs;
245         this.add(scheduler.schedule<DispatchNextArg<T>>(dispatchNext, 0, { value, subject }));
246       }
247     };
248
249     try {
250       callbackFunc.apply(context, [...args, handler]);
251     } catch (err) {
252       this.add(scheduler.schedule<DispatchErrorArg<T>>(dispatchError, 0, { err, subject }));
253     }
254   }
255
256   this.add(subject.subscribe(subscriber));
257 }
258
259 interface DispatchNextArg<T> {
260   subject: AsyncSubject<T>;
261   value: T;
262 }
263
264 function dispatchNext<T>(arg: DispatchNextArg<T>) {
265   const { value, subject } = arg;
266   subject.next(value);
267   subject.complete();
268 }
269
270 interface DispatchErrorArg<T> {
271   subject: AsyncSubject<T>;
272   err: any;
273 }
274
275 function dispatchError<T>(arg: DispatchErrorArg<T>) {
276   const { err, subject } = arg;
277   subject.error(err);
278 }