bindNodeCallback.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var Observable_1 = require("../Observable");
  4. var AsyncSubject_1 = require("../AsyncSubject");
  5. var map_1 = require("../operators/map");
  6. var canReportError_1 = require("../util/canReportError");
  7. var isScheduler_1 = require("../util/isScheduler");
  8. var isArray_1 = require("../util/isArray");
  9. function bindNodeCallback(callbackFunc, resultSelector, scheduler) {
  10. if (resultSelector) {
  11. if (isScheduler_1.isScheduler(resultSelector)) {
  12. scheduler = resultSelector;
  13. }
  14. else {
  15. return function () {
  16. var args = [];
  17. for (var _i = 0; _i < arguments.length; _i++) {
  18. args[_i] = arguments[_i];
  19. }
  20. return bindNodeCallback(callbackFunc, scheduler).apply(void 0, args).pipe(map_1.map(function (args) { return isArray_1.isArray(args) ? resultSelector.apply(void 0, args) : resultSelector(args); }));
  21. };
  22. }
  23. }
  24. return function () {
  25. var args = [];
  26. for (var _i = 0; _i < arguments.length; _i++) {
  27. args[_i] = arguments[_i];
  28. }
  29. var params = {
  30. subject: undefined,
  31. args: args,
  32. callbackFunc: callbackFunc,
  33. scheduler: scheduler,
  34. context: this,
  35. };
  36. return new Observable_1.Observable(function (subscriber) {
  37. var context = params.context;
  38. var subject = params.subject;
  39. if (!scheduler) {
  40. if (!subject) {
  41. subject = params.subject = new AsyncSubject_1.AsyncSubject();
  42. var handler = function () {
  43. var innerArgs = [];
  44. for (var _i = 0; _i < arguments.length; _i++) {
  45. innerArgs[_i] = arguments[_i];
  46. }
  47. var err = innerArgs.shift();
  48. if (err) {
  49. subject.error(err);
  50. return;
  51. }
  52. subject.next(innerArgs.length <= 1 ? innerArgs[0] : innerArgs);
  53. subject.complete();
  54. };
  55. try {
  56. callbackFunc.apply(context, args.concat([handler]));
  57. }
  58. catch (err) {
  59. if (canReportError_1.canReportError(subject)) {
  60. subject.error(err);
  61. }
  62. else {
  63. console.warn(err);
  64. }
  65. }
  66. }
  67. return subject.subscribe(subscriber);
  68. }
  69. else {
  70. return scheduler.schedule(dispatch, 0, { params: params, subscriber: subscriber, context: context });
  71. }
  72. });
  73. };
  74. }
  75. exports.bindNodeCallback = bindNodeCallback;
  76. function dispatch(state) {
  77. var _this = this;
  78. var params = state.params, subscriber = state.subscriber, context = state.context;
  79. var callbackFunc = params.callbackFunc, args = params.args, scheduler = params.scheduler;
  80. var subject = params.subject;
  81. if (!subject) {
  82. subject = params.subject = new AsyncSubject_1.AsyncSubject();
  83. var handler = function () {
  84. var innerArgs = [];
  85. for (var _i = 0; _i < arguments.length; _i++) {
  86. innerArgs[_i] = arguments[_i];
  87. }
  88. var err = innerArgs.shift();
  89. if (err) {
  90. _this.add(scheduler.schedule(dispatchError, 0, { err: err, subject: subject }));
  91. }
  92. else {
  93. var value = innerArgs.length <= 1 ? innerArgs[0] : innerArgs;
  94. _this.add(scheduler.schedule(dispatchNext, 0, { value: value, subject: subject }));
  95. }
  96. };
  97. try {
  98. callbackFunc.apply(context, args.concat([handler]));
  99. }
  100. catch (err) {
  101. this.add(scheduler.schedule(dispatchError, 0, { err: err, subject: subject }));
  102. }
  103. }
  104. this.add(subject.subscribe(subscriber));
  105. }
  106. function dispatchNext(arg) {
  107. var value = arg.value, subject = arg.subject;
  108. subject.next(value);
  109. subject.complete();
  110. }
  111. function dispatchError(arg) {
  112. var err = arg.err, subject = arg.subject;
  113. subject.error(err);
  114. }
  115. //# sourceMappingURL=bindNodeCallback.js.map