bindNodeCallback.js
3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { Observable } from '../Observable';
import { AsyncSubject } from '../AsyncSubject';
import { map } from '../operators/map';
import { canReportError } from '../util/canReportError';
import { isScheduler } from '../util/isScheduler';
import { isArray } from '../util/isArray';
export function bindNodeCallback(callbackFunc, resultSelector, scheduler) {
if (resultSelector) {
if (isScheduler(resultSelector)) {
scheduler = resultSelector;
}
else {
return (...args) => bindNodeCallback(callbackFunc, scheduler)(...args).pipe(map(args => isArray(args) ? resultSelector(...args) : resultSelector(args)));
}
}
return function (...args) {
const params = {
subject: undefined,
args,
callbackFunc,
scheduler,
context: this,
};
return new Observable(subscriber => {
const { context } = params;
let { subject } = params;
if (!scheduler) {
if (!subject) {
subject = params.subject = new AsyncSubject();
const handler = (...innerArgs) => {
const err = innerArgs.shift();
if (err) {
subject.error(err);
return;
}
subject.next(innerArgs.length <= 1 ? innerArgs[0] : innerArgs);
subject.complete();
};
try {
callbackFunc.apply(context, [...args, handler]);
}
catch (err) {
if (canReportError(subject)) {
subject.error(err);
}
else {
console.warn(err);
}
}
}
return subject.subscribe(subscriber);
}
else {
return scheduler.schedule(dispatch, 0, { params, subscriber, context });
}
});
};
}
function dispatch(state) {
const { params, subscriber, context } = state;
const { callbackFunc, args, scheduler } = params;
let subject = params.subject;
if (!subject) {
subject = params.subject = new AsyncSubject();
const handler = (...innerArgs) => {
const err = innerArgs.shift();
if (err) {
this.add(scheduler.schedule(dispatchError, 0, { err, subject }));
}
else {
const value = innerArgs.length <= 1 ? innerArgs[0] : innerArgs;
this.add(scheduler.schedule(dispatchNext, 0, { value, subject }));
}
};
try {
callbackFunc.apply(context, [...args, handler]);
}
catch (err) {
this.add(scheduler.schedule(dispatchError, 0, { err, subject }));
}
}
this.add(subject.subscribe(subscriber));
}
function dispatchNext(arg) {
const { value, subject } = arg;
subject.next(value);
subject.complete();
}
function dispatchError(arg) {
const { err, subject } = arg;
subject.error(err);
}
//# sourceMappingURL=bindNodeCallback.js.map