113 lines
4.3 KiB
JavaScript
113 lines
4.3 KiB
JavaScript
/** PURE_IMPORTS_START _Observable,_AsyncSubject,_operators_map,_util_canReportError,_util_isScheduler,_util_isArray PURE_IMPORTS_END */
|
|
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 function () {
|
|
var args = [];
|
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
args[_i] = arguments[_i];
|
|
}
|
|
return bindNodeCallback(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 params = {
|
|
subject: undefined,
|
|
args: args,
|
|
callbackFunc: callbackFunc,
|
|
scheduler: scheduler,
|
|
context: this,
|
|
};
|
|
return new Observable(function (subscriber) {
|
|
var context = params.context;
|
|
var subject = params.subject;
|
|
if (!scheduler) {
|
|
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 err = innerArgs.shift();
|
|
if (err) {
|
|
subject.error(err);
|
|
return;
|
|
}
|
|
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 {
|
|
return scheduler.schedule(dispatch, 0, { params: params, subscriber: subscriber, context: context });
|
|
}
|
|
});
|
|
};
|
|
}
|
|
function dispatch(state) {
|
|
var _this = this;
|
|
var params = state.params, subscriber = state.subscriber, context = state.context;
|
|
var callbackFunc = params.callbackFunc, args = params.args, 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 err = innerArgs.shift();
|
|
if (err) {
|
|
_this.add(scheduler.schedule(dispatchError, 0, { err: err, subject: subject }));
|
|
}
|
|
else {
|
|
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) {
|
|
this.add(scheduler.schedule(dispatchError, 0, { err: err, subject: subject }));
|
|
}
|
|
}
|
|
this.add(subject.subscribe(subscriber));
|
|
}
|
|
function dispatchNext(arg) {
|
|
var value = arg.value, subject = arg.subject;
|
|
subject.next(value);
|
|
subject.complete();
|
|
}
|
|
function dispatchError(arg) {
|
|
var err = arg.err, subject = arg.subject;
|
|
subject.error(err);
|
|
}
|
|
//# sourceMappingURL=bindNodeCallback.js.map
|