123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- /** 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
|