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