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