Debian Codegraph @

node/rxjs/src/internal/observable/bindCallbackInternals.ts

raw
1 import { SchedulerLike } from '../types';
2 import { isScheduler } from '../util/isScheduler';
3 import { Observable } from '../Observable';
4 import { subscribeOn } from '../operators/subscribeOn';
5 import { mapOneOrManyArgs } from '../util/mapOneOrManyArgs';
6 import { observeOn } from '../operators/observeOn';
7 import { AsyncSubject } from '../AsyncSubject';
8
9 export function bindCallbackInternals(
10 isNodeStyle: boolean,
11 callbackFunc: any,
12 resultSelector?: any,
13 scheduler?: SchedulerLike
14 ): (...args: any[]) => Observable<unknown> {
15 if (resultSelector) {
16 if (isScheduler(resultSelector)) {
17 scheduler = resultSelector;
18 } else {
19 // The user provided a result selector.
20 return function (this: any, ...args: any[]) {
21 return (bindCallbackInternals(isNodeStyle, callbackFunc, scheduler) as any)
22 .apply(this, args)
23 .pipe(mapOneOrManyArgs(resultSelector as any));
24 };
25 }
26 }
27
28 // If a scheduler was passed, use our `subscribeOn` and `observeOn` operators
29 // to compose that behavior for the user.
30 if (scheduler) {
31 return function (this: any, ...args: any[]) {
32 return (bindCallbackInternals(isNodeStyle, callbackFunc) as any)
33 .apply(this, args)
34 .pipe(subscribeOn(scheduler!), observeOn(scheduler!));
35 };
36 }
37
38 return function (this: any, ...args: any[]): Observable<any> {
39 // We're using AsyncSubject, because it emits when it completes,
40 // and it will play the value to all late-arriving subscribers.
41 const subject = new AsyncSubject<any>();
42
43 // If this is true, then we haven't called our function yet.
44 let uninitialized = true;
45 return new Observable((subscriber) => {
46 // Add our subscriber to the subject.
47 const subs = subject.subscribe(subscriber);
48
49 if (uninitialized) {
50 uninitialized = false;
51 // We're going to execute the bound function
52 // This bit is to signal that we are hitting the callback asynchronously.
53 // Because we don't have any anti-"Zalgo" guarantees with whatever
54 // function we are handed, we use this bit to figure out whether or not
55 // we are getting hit in a callback synchronously during our call.
56 let isAsync = false;
57
58 // This is used to signal that the callback completed synchronously.
59 let isComplete = false;
60
61 // Call our function that has a callback. If at any time during this
62 // call, an error is thrown, it will be caught by the Observable
63 // subscription process and sent to the consumer.
64 callbackFunc.apply(
65 // Pass the appropriate `this` context.
66 this,
67 [
68 // Pass the arguments.
69 ...args,
70 // And our callback handler.
71 (...results: any[]) => {
72 if (isNodeStyle) {
73 // If this is a node callback, shift the first value off of the
74 // results and check it, as it is the error argument. By shifting,
75 // we leave only the argument(s) we want to pass to the consumer.
76 const err = results.shift();
77 if (err != null) {
78 subject.error(err);
79 // If we've errored, we can stop processing this function
80 // as there's nothing else to do. Just return to escape.
81 return;
82 }
83 }
84 // If we have one argument, notify the consumer
85 // of it as a single value, otherwise, if there's more than one, pass
86 // them as an array. Note that if there are no arguments, `undefined`
87 // will be emitted.
88 subject.next(1 < results.length ? results : results[0]);
89 // Flip this flag, so we know we can complete it in the synchronous
90 // case below.
91 isComplete = true;
92 // If we're not asynchronous, we need to defer the `complete` call
93 // until after the call to the function is over. This is because an
94 // error could be thrown in the function after it calls our callback,
95 // and if that is the case, if we complete here, we are unable to notify
96 // the consumer than an error occurred.
97 if (isAsync) {
98 subject.complete();
99 }
100 },
101 ]
102 );
103 // If we flipped `isComplete` during the call, we resolved synchronously,
104 // notify complete, because we skipped it in the callback to wait
105 // to make sure there were no errors during the call.
106 if (isComplete) {
107 subject.complete();
108 }
109
110 // We're no longer synchronous. If the callback is called at this point
111 // we can notify complete on the spot.
112 isAsync = true;
113 }
114
115 // Return the subscription from adding our subscriber to the subject.
116 return subs;
117 });
118 };
119 }