bindCallback.js
3.85 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
94
95
96
97
98
99
100
101
102
103
104
105
/** PURE_IMPORTS_START _Observable,_AsyncSubject,_operators_map,_util_canReportError,_util_isArray,_util_isScheduler PURE_IMPORTS_END */
import { Observable } from '../Observable';
import { AsyncSubject } from '../AsyncSubject';
import { map } from '../operators/map';
import { canReportError } from '../util/canReportError';
import { isArray } from '../util/isArray';
import { isScheduler } from '../util/isScheduler';
export function bindCallback(callbackFunc, resultSelector, scheduler) {
if (resultSelector) {
if (isScheduler(resultSelector)) {
scheduler = resultSelector;
}
else {
return function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return bindCallback(callbackFunc, scheduler).apply(void 0, args).pipe(map(function (args) { return isArray(args) ? resultSelector.apply(void 0, args) : resultSelector(args); }));
};
}
}
return function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
var context = this;
var subject;
var params = {
context: context,
subject: subject,
callbackFunc: callbackFunc,
scheduler: scheduler,
};
return new Observable(function (subscriber) {
if (!scheduler) {
if (!subject) {
subject = new AsyncSubject();
var handler = function () {
var innerArgs = [];
for (var _i = 0; _i < arguments.length; _i++) {
innerArgs[_i] = arguments[_i];
}
subject.next(innerArgs.length <= 1 ? innerArgs[0] : innerArgs);
subject.complete();
};
try {
callbackFunc.apply(context, args.concat([handler]));
}
catch (err) {
if (canReportError(subject)) {
subject.error(err);
}
else {
console.warn(err);
}
}
}
return subject.subscribe(subscriber);
}
else {
var state = {
args: args, subscriber: subscriber, params: params,
};
return scheduler.schedule(dispatch, 0, state);
}
});
};
}
function dispatch(state) {
var _this = this;
var self = this;
var args = state.args, subscriber = state.subscriber, params = state.params;
var callbackFunc = params.callbackFunc, context = params.context, scheduler = params.scheduler;
var subject = params.subject;
if (!subject) {
subject = params.subject = new AsyncSubject();
var handler = function () {
var innerArgs = [];
for (var _i = 0; _i < arguments.length; _i++) {
innerArgs[_i] = arguments[_i];
}
var value = innerArgs.length <= 1 ? innerArgs[0] : innerArgs;
_this.add(scheduler.schedule(dispatchNext, 0, { value: value, subject: subject }));
};
try {
callbackFunc.apply(context, args.concat([handler]));
}
catch (err) {
subject.error(err);
}
}
this.add(subject.subscribe(subscriber));
}
function dispatchNext(state) {
var value = state.value, subject = state.subject;
subject.next(value);
subject.complete();
}
function dispatchError(state) {
var err = state.err, subject = state.subject;
subject.error(err);
}
//# sourceMappingURL=bindCallback.js.map