source: trip-planner-front/node_modules/rxjs/src/internal/observable/bindCallback.ts

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 15.9 KB
Line 
1import { SchedulerLike, SchedulerAction } from '../types';
2import { Observable } from '../Observable';
3import { AsyncSubject } from '../AsyncSubject';
4import { Subscriber } from '../Subscriber';
5import { map } from '../operators/map';
6import { canReportError } from '../util/canReportError';
7import { isArray } from '../util/isArray';
8import { isScheduler } from '../util/isScheduler';
9
10// tslint:disable:max-line-length
11/** @deprecated resultSelector is no longer supported, use a mapping function. */
12export function bindCallback(callbackFunc: Function, resultSelector: Function, scheduler?: SchedulerLike): (...args: any[]) => Observable<any>;
13
14export function bindCallback<R1, R2, R3, R4>(callbackFunc: (callback: (res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any, scheduler?: SchedulerLike): () => Observable<any[]>;
15export function bindCallback<R1, R2, R3>(callbackFunc: (callback: (res1: R1, res2: R2, res3: R3) => any) => any, scheduler?: SchedulerLike): () => Observable<[R1, R2, R3]>;
16export function bindCallback<R1, R2>(callbackFunc: (callback: (res1: R1, res2: R2) => any) => any, scheduler?: SchedulerLike): () => Observable<[R1, R2]>;
17export function bindCallback<R1>(callbackFunc: (callback: (res1: R1) => any) => any, scheduler?: SchedulerLike): () => Observable<R1>;
18export function bindCallback(callbackFunc: (callback: () => any) => any, scheduler?: SchedulerLike): () => Observable<void>;
19
20export 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[]>;
21export 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]>;
22export function bindCallback<A1, R1, R2>(callbackFunc: (arg1: A1, callback: (res1: R1, res2: R2) => any) => any, scheduler?: SchedulerLike): (arg1: A1) => Observable<[R1, R2]>;
23export function bindCallback<A1, R1>(callbackFunc: (arg1: A1, callback: (res1: R1) => any) => any, scheduler?: SchedulerLike): (arg1: A1) => Observable<R1>;
24export function bindCallback<A1>(callbackFunc: (arg1: A1, callback: () => any) => any, scheduler?: SchedulerLike): (arg1: A1) => Observable<void>;
25
26export 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[]>;
27export 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]>;
28export 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]>;
29export function bindCallback<A1, A2, R1>(callbackFunc: (arg1: A1, arg2: A2, callback: (res1: R1) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2) => Observable<R1>;
30export function bindCallback<A1, A2>(callbackFunc: (arg1: A1, arg2: A2, callback: () => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2) => Observable<void>;
31
32export 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[]>;
33export 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]>;
34export 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]>;
35export 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>;
36export 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>;
37
38export 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[]>;
39export 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]>;
40export 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]>;
41export 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>;
42export 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>;
43
44export 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[]>;
45export 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]>;
46export 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]>;
47export 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>;
48export 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>;
49
50export function bindCallback<A, R>(callbackFunc: (...args: Array<A | ((result: R) => any)>) => any, scheduler?: SchedulerLike): (...args: A[]) => Observable<R>;
51export function bindCallback<A, R>(callbackFunc: (...args: Array<A | ((...results: R[]) => any)>) => any, scheduler?: SchedulerLike): (...args: A[]) => Observable<R[]>;
52
53export function bindCallback(callbackFunc: Function, scheduler?: SchedulerLike): (...args: any[]) => Observable<any>;
54
55// tslint:enable:max-line-length
56
57/**
58 * Converts a callback API to a function that returns an Observable.
59 *
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
62 * Observable.</span>
63 *
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
67 * done.
68 *
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.
75 *
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.
80 *
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.
88 *
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.
94 *
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.
99 *
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.
104 *
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.
110 *
111 * ## Examples
112 *
113 * ### Convert jQuery's getJSON to an Observable API
114 * ```ts
115 * import { bindCallback } from 'rxjs';
116 * import * as jQuery from 'jquery';
117 *
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));
122 * ```
123 *
124 * ### Receive an array of arguments passed to a callback
125 * ```ts
126 * import { bindCallback } from 'rxjs';
127 *
128 * const someFunction = (a, b, c) => {
129 * console.log(a); // 5
130 * console.log(b); // 'some string'
131 * console.log(c); // {someProperty: 'someValue'}
132 * };
133 *
134 * const boundSomeFunction = bindCallback(someFunction);
135 * boundSomeFunction().subscribe(values => {
136 * console.log(values) // [5, 'some string', {someProperty: 'someValue'}]
137 * });
138 * ```
139 *
140 * ### Compare behaviour with and without async Scheduler
141 * ```ts
142 * import { bindCallback } from 'rxjs';
143 *
144 * function iCallMyCallbackSynchronously(cb) {
145 * cb();
146 * }
147 *
148 * const boundSyncFn = bindCallback(iCallMyCallbackSynchronously);
149 * const boundAsyncFn = bindCallback(iCallMyCallbackSynchronously, null, Rx.Scheduler.async);
150 *
151 * boundSyncFn().subscribe(() => console.log('I was sync!'));
152 * boundAsyncFn().subscribe(() => console.log('I was async!'));
153 * console.log('This happened...');
154 *
155 * // Logs:
156 * // I was sync!
157 * // This happened...
158 * // I was async!
159 * ```
160 *
161 * ### Use bindCallback on an object method
162 * ```ts
163 * import { bindCallback } from 'rxjs';
164 *
165 * const boundMethod = bindCallback(someObject.methodWithCallback);
166 * boundMethod.call(someObject) // make sure methodWithCallback has access to someObject
167 * .subscribe(subscriber);
168 * ```
169 *
170 * @see {@link bindNodeCallback}
171 * @see {@link from}
172 *
173 * @param {function} func A function with a callback as the last parameter.
174 * @param {SchedulerLike} [scheduler] The scheduler on which to schedule the
175 * callbacks.
176 * @return {function(...params: *): Observable} A function which returns the
177 * Observable that delivers the same values the callback would deliver.
178 * @name bindCallback
179 */
180export 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;
188 } else {
189 // DEPRECATED PATH
190 return (...args: any[]) => bindCallback(callbackFunc, scheduler)(...args).pipe(
191 map((args) => isArray(args) ? resultSelector(...args) : resultSelector(args)),
192 );
193 }
194 }
195
196 return function (this: any, ...args: any[]): Observable<T> {
197 const context = this;
198 let subject: AsyncSubject<T>;
199 const params = {
200 context,
201 subject,
202 callbackFunc,
203 scheduler,
204 };
205 return new Observable<T>(subscriber => {
206 if (!scheduler) {
207 if (!subject) {
208 subject = new AsyncSubject<T>();
209 const handler = (...innerArgs: any[]) => {
210 subject.next(innerArgs.length <= 1 ? innerArgs[0] : innerArgs);
211 subject.complete();
212 };
213
214 try {
215 callbackFunc.apply(context, [...args, handler]);
216 } catch (err) {
217 if (canReportError(subject)) {
218 subject.error(err);
219 } else {
220 console.warn(err);
221 }
222 }
223 }
224 return subject.subscribe(subscriber);
225 } else {
226 const state: DispatchState<T> = {
227 args, subscriber, params,
228 };
229 return scheduler.schedule<DispatchState<T>>(dispatch, 0, state);
230 }
231 });
232 };
233}
234
235interface DispatchState<T> {
236 args: any[];
237 subscriber: Subscriber<T>;
238 params: ParamsContext<T>;
239}
240
241interface ParamsContext<T> {
242 callbackFunc: Function;
243 scheduler: SchedulerLike;
244 context: any;
245 subject: AsyncSubject<T>;
246}
247
248function dispatch<T>(this: SchedulerAction<DispatchState<T>>, state: DispatchState<T>) {
249 const self = this;
250 const { args, subscriber, params } = state;
251 const { callbackFunc, context, scheduler } = params;
252 let { subject } = params;
253 if (!subject) {
254 subject = params.subject = new AsyncSubject<T>();
255
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 }));
259 };
260
261 try {
262 callbackFunc.apply(context, [...args, handler]);
263 } catch (err) {
264 subject.error(err);
265 }
266 }
267
268 this.add(subject.subscribe(subscriber));
269}
270
271interface NextState<T> {
272 subject: AsyncSubject<T>;
273 value: T;
274}
275
276function dispatchNext<T>(this: SchedulerAction<NextState<T>>, state: NextState<T>) {
277 const { value, subject } = state;
278 subject.next(value);
279 subject.complete();
280}
281
282interface ErrorState<T> {
283 subject: AsyncSubject<T>;
284 err: any;
285}
286
287function dispatchError<T>(this: SchedulerAction<ErrorState<T>>, state: ErrorState<T>) {
288 const { err, subject } = state;
289 subject.error(err);
290}
Note: See TracBrowser for help on using the repository browser.