bindCallback.js
3.91 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
106
107
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Observable_1 = require("../Observable");
var AsyncSubject_1 = require("../AsyncSubject");
var map_1 = require("../operators/map");
var canReportError_1 = require("../util/canReportError");
var isArray_1 = require("../util/isArray");
var isScheduler_1 = require("../util/isScheduler");
function bindCallback(callbackFunc, resultSelector, scheduler) {
if (resultSelector) {
if (isScheduler_1.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_1.map(function (args) { return isArray_1.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_1.Observable(function (subscriber) {
if (!scheduler) {
if (!subject) {
subject = new AsyncSubject_1.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_1.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);
}
});
};
}
exports.bindCallback = bindCallback;
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_1.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