bindCallback.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /** PURE_IMPORTS_START _Observable,_AsyncSubject,_operators_map,_util_canReportError,_util_isArray,_util_isScheduler PURE_IMPORTS_END */
  2. import { Observable } from '../Observable';
  3. import { AsyncSubject } from '../AsyncSubject';
  4. import { map } from '../operators/map';
  5. import { canReportError } from '../util/canReportError';
  6. import { isArray } from '../util/isArray';
  7. import { isScheduler } from '../util/isScheduler';
  8. export function bindCallback(callbackFunc, resultSelector, scheduler) {
  9. if (resultSelector) {
  10. if (isScheduler(resultSelector)) {
  11. scheduler = resultSelector;
  12. }
  13. else {
  14. return function () {
  15. var args = [];
  16. for (var _i = 0; _i < arguments.length; _i++) {
  17. args[_i] = arguments[_i];
  18. }
  19. return bindCallback(callbackFunc, scheduler).apply(void 0, args).pipe(map(function (args) { return isArray(args) ? resultSelector.apply(void 0, args) : resultSelector(args); }));
  20. };
  21. }
  22. }
  23. return function () {
  24. var args = [];
  25. for (var _i = 0; _i < arguments.length; _i++) {
  26. args[_i] = arguments[_i];
  27. }
  28. var context = this;
  29. var subject;
  30. var params = {
  31. context: context,
  32. subject: subject,
  33. callbackFunc: callbackFunc,
  34. scheduler: scheduler,
  35. };
  36. return new Observable(function (subscriber) {
  37. if (!scheduler) {
  38. if (!subject) {
  39. subject = new AsyncSubject();
  40. var handler = function () {
  41. var innerArgs = [];
  42. for (var _i = 0; _i < arguments.length; _i++) {
  43. innerArgs[_i] = arguments[_i];
  44. }
  45. subject.next(innerArgs.length <= 1 ? innerArgs[0] : innerArgs);
  46. subject.complete();
  47. };
  48. try {
  49. callbackFunc.apply(context, args.concat([handler]));
  50. }
  51. catch (err) {
  52. if (canReportError(subject)) {
  53. subject.error(err);
  54. }
  55. else {
  56. console.warn(err);
  57. }
  58. }
  59. }
  60. return subject.subscribe(subscriber);
  61. }
  62. else {
  63. var state = {
  64. args: args, subscriber: subscriber, params: params,
  65. };
  66. return scheduler.schedule(dispatch, 0, state);
  67. }
  68. });
  69. };
  70. }
  71. function dispatch(state) {
  72. var _this = this;
  73. var self = this;
  74. var args = state.args, subscriber = state.subscriber, params = state.params;
  75. var callbackFunc = params.callbackFunc, context = params.context, scheduler = params.scheduler;
  76. var subject = params.subject;
  77. if (!subject) {
  78. subject = params.subject = new AsyncSubject();
  79. var handler = function () {
  80. var innerArgs = [];
  81. for (var _i = 0; _i < arguments.length; _i++) {
  82. innerArgs[_i] = arguments[_i];
  83. }
  84. var value = innerArgs.length <= 1 ? innerArgs[0] : innerArgs;
  85. _this.add(scheduler.schedule(dispatchNext, 0, { value: value, subject: subject }));
  86. };
  87. try {
  88. callbackFunc.apply(context, args.concat([handler]));
  89. }
  90. catch (err) {
  91. subject.error(err);
  92. }
  93. }
  94. this.add(subject.subscribe(subscriber));
  95. }
  96. function dispatchNext(state) {
  97. var value = state.value, subject = state.subject;
  98. subject.next(value);
  99. subject.complete();
  100. }
  101. function dispatchError(state) {
  102. var err = state.err, subject = state.subject;
  103. subject.error(err);
  104. }
  105. //# sourceMappingURL=bindCallback.js.map