bindCallback.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 isArray_1 = require("../util/isArray");
  8. var isScheduler_1 = require("../util/isScheduler");
  9. function bindCallback(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 bindCallback(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 context = this;
  30. var subject;
  31. var params = {
  32. context: context,
  33. subject: subject,
  34. callbackFunc: callbackFunc,
  35. scheduler: scheduler,
  36. };
  37. return new Observable_1.Observable(function (subscriber) {
  38. if (!scheduler) {
  39. if (!subject) {
  40. subject = new AsyncSubject_1.AsyncSubject();
  41. var handler = function () {
  42. var innerArgs = [];
  43. for (var _i = 0; _i < arguments.length; _i++) {
  44. innerArgs[_i] = arguments[_i];
  45. }
  46. subject.next(innerArgs.length <= 1 ? innerArgs[0] : innerArgs);
  47. subject.complete();
  48. };
  49. try {
  50. callbackFunc.apply(context, args.concat([handler]));
  51. }
  52. catch (err) {
  53. if (canReportError_1.canReportError(subject)) {
  54. subject.error(err);
  55. }
  56. else {
  57. console.warn(err);
  58. }
  59. }
  60. }
  61. return subject.subscribe(subscriber);
  62. }
  63. else {
  64. var state = {
  65. args: args, subscriber: subscriber, params: params,
  66. };
  67. return scheduler.schedule(dispatch, 0, state);
  68. }
  69. });
  70. };
  71. }
  72. exports.bindCallback = bindCallback;
  73. function dispatch(state) {
  74. var _this = this;
  75. var self = this;
  76. var args = state.args, subscriber = state.subscriber, params = state.params;
  77. var callbackFunc = params.callbackFunc, context = params.context, scheduler = params.scheduler;
  78. var subject = params.subject;
  79. if (!subject) {
  80. subject = params.subject = new AsyncSubject_1.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 value = innerArgs.length <= 1 ? innerArgs[0] : innerArgs;
  87. _this.add(scheduler.schedule(dispatchNext, 0, { value: value, subject: subject }));
  88. };
  89. try {
  90. callbackFunc.apply(context, args.concat([handler]));
  91. }
  92. catch (err) {
  93. subject.error(err);
  94. }
  95. }
  96. this.add(subject.subscribe(subscriber));
  97. }
  98. function dispatchNext(state) {
  99. var value = state.value, subject = state.subject;
  100. subject.next(value);
  101. subject.complete();
  102. }
  103. function dispatchError(state) {
  104. var err = state.err, subject = state.subject;
  105. subject.error(err);
  106. }
  107. //# sourceMappingURL=bindCallback.js.map