bindNodeCallback.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /** PURE_IMPORTS_START _Observable,_AsyncSubject,_operators_map,_util_canReportError,_util_isScheduler,_util_isArray 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 { isScheduler } from '../util/isScheduler';
  7. import { isArray } from '../util/isArray';
  8. export function bindNodeCallback(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 bindNodeCallback(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 params = {
  29. subject: undefined,
  30. args: args,
  31. callbackFunc: callbackFunc,
  32. scheduler: scheduler,
  33. context: this,
  34. };
  35. return new Observable(function (subscriber) {
  36. var context = params.context;
  37. var subject = params.subject;
  38. if (!scheduler) {
  39. if (!subject) {
  40. subject = params.subject = new AsyncSubject();
  41. var handler = function () {
  42. var innerArgs = [];
  43. for (var _i = 0; _i < arguments.length; _i++) {
  44. innerArgs[_i] = arguments[_i];
  45. }
  46. var err = innerArgs.shift();
  47. if (err) {
  48. subject.error(err);
  49. return;
  50. }
  51. subject.next(innerArgs.length <= 1 ? innerArgs[0] : innerArgs);
  52. subject.complete();
  53. };
  54. try {
  55. callbackFunc.apply(context, args.concat([handler]));
  56. }
  57. catch (err) {
  58. if (canReportError(subject)) {
  59. subject.error(err);
  60. }
  61. else {
  62. console.warn(err);
  63. }
  64. }
  65. }
  66. return subject.subscribe(subscriber);
  67. }
  68. else {
  69. return scheduler.schedule(dispatch, 0, { params: params, subscriber: subscriber, context: context });
  70. }
  71. });
  72. };
  73. }
  74. function dispatch(state) {
  75. var _this = this;
  76. var params = state.params, subscriber = state.subscriber, context = state.context;
  77. var callbackFunc = params.callbackFunc, args = params.args, scheduler = params.scheduler;
  78. var subject = params.subject;
  79. if (!subject) {
  80. subject = params.subject = new AsyncSubject();
  81. var handler = function () {
  82. var innerArgs = [];
  83. for (var _i = 0; _i < arguments.length; _i++) {
  84. innerArgs[_i] = arguments[_i];
  85. }
  86. var err = innerArgs.shift();
  87. if (err) {
  88. _this.add(scheduler.schedule(dispatchError, 0, { err: err, subject: subject }));
  89. }
  90. else {
  91. var value = innerArgs.length <= 1 ? innerArgs[0] : innerArgs;
  92. _this.add(scheduler.schedule(dispatchNext, 0, { value: value, subject: subject }));
  93. }
  94. };
  95. try {
  96. callbackFunc.apply(context, args.concat([handler]));
  97. }
  98. catch (err) {
  99. this.add(scheduler.schedule(dispatchError, 0, { err: err, subject: subject }));
  100. }
  101. }
  102. this.add(subject.subscribe(subscriber));
  103. }
  104. function dispatchNext(arg) {
  105. var value = arg.value, subject = arg.subject;
  106. subject.next(value);
  107. subject.complete();
  108. }
  109. function dispatchError(arg) {
  110. var err = arg.err, subject = arg.subject;
  111. subject.error(err);
  112. }
  113. //# sourceMappingURL=bindNodeCallback.js.map