FromEventPatternObservable.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /** PURE_IMPORTS_START .._util_isFunction,.._Observable,.._Subscription PURE_IMPORTS_END */
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b)
  4. if (b.hasOwnProperty(p))
  5. d[p] = b[p];
  6. function __() { this.constructor = d; }
  7. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  8. };
  9. import { isFunction } from '../util/isFunction';
  10. import { Observable } from '../Observable';
  11. import { Subscription } from '../Subscription';
  12. /**
  13. * We need this JSDoc comment for affecting ESDoc.
  14. * @extends {Ignored}
  15. * @hide true
  16. */
  17. export var FromEventPatternObservable = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  18. __extends(FromEventPatternObservable, _super);
  19. function FromEventPatternObservable(addHandler, removeHandler, selector) {
  20. _super.call(this);
  21. this.addHandler = addHandler;
  22. this.removeHandler = removeHandler;
  23. this.selector = selector;
  24. }
  25. /**
  26. * Creates an Observable from an API based on addHandler/removeHandler
  27. * functions.
  28. *
  29. * <span class="informal">Converts any addHandler/removeHandler API to an
  30. * Observable.</span>
  31. *
  32. * <img src="./img/fromEventPattern.png" width="100%">
  33. *
  34. * Creates an Observable by using the `addHandler` and `removeHandler`
  35. * functions to add and remove the handlers, with an optional selector
  36. * function to project the event arguments to a result. The `addHandler` is
  37. * called when the output Observable is subscribed, and `removeHandler` is
  38. * called when the Subscription is unsubscribed.
  39. *
  40. * @example <caption>Emits clicks happening on the DOM document</caption>
  41. * function addClickHandler(handler) {
  42. * document.addEventListener('click', handler);
  43. * }
  44. *
  45. * function removeClickHandler(handler) {
  46. * document.removeEventListener('click', handler);
  47. * }
  48. *
  49. * var clicks = Rx.Observable.fromEventPattern(
  50. * addClickHandler,
  51. * removeClickHandler
  52. * );
  53. * clicks.subscribe(x => console.log(x));
  54. *
  55. * @see {@link from}
  56. * @see {@link fromEvent}
  57. *
  58. * @param {function(handler: Function): any} addHandler A function that takes
  59. * a `handler` function as argument and attaches it somehow to the actual
  60. * source of events.
  61. * @param {function(handler: Function, signal?: any): void} [removeHandler] An optional function that
  62. * takes a `handler` function as argument and removes it in case it was
  63. * previously attached using `addHandler`. if addHandler returns signal to teardown when remove,
  64. * removeHandler function will forward it.
  65. * @param {function(...args: any): T} [selector] An optional function to
  66. * post-process results. It takes the arguments from the event handler and
  67. * should return a single value.
  68. * @return {Observable<T>}
  69. * @static true
  70. * @name fromEventPattern
  71. * @owner Observable
  72. */
  73. FromEventPatternObservable.create = function (addHandler, removeHandler, selector) {
  74. return new FromEventPatternObservable(addHandler, removeHandler, selector);
  75. };
  76. /** @deprecated internal use only */ FromEventPatternObservable.prototype._subscribe = function (subscriber) {
  77. var _this = this;
  78. var removeHandler = this.removeHandler;
  79. var handler = !!this.selector ? function () {
  80. var args = [];
  81. for (var _i = 0; _i < arguments.length; _i++) {
  82. args[_i - 0] = arguments[_i];
  83. }
  84. _this._callSelector(subscriber, args);
  85. } : function (e) { subscriber.next(e); };
  86. var retValue = this._callAddHandler(handler, subscriber);
  87. if (!isFunction(removeHandler)) {
  88. return;
  89. }
  90. subscriber.add(new Subscription(function () {
  91. //TODO: determine whether or not to forward to error handler
  92. removeHandler(handler, retValue);
  93. }));
  94. };
  95. FromEventPatternObservable.prototype._callSelector = function (subscriber, args) {
  96. try {
  97. var result = this.selector.apply(this, args);
  98. subscriber.next(result);
  99. }
  100. catch (e) {
  101. subscriber.error(e);
  102. }
  103. };
  104. FromEventPatternObservable.prototype._callAddHandler = function (handler, errorSubscriber) {
  105. try {
  106. return this.addHandler(handler) || null;
  107. }
  108. catch (e) {
  109. errorSubscriber.error(e);
  110. }
  111. };
  112. return FromEventPatternObservable;
  113. }(Observable));
  114. //# sourceMappingURL=FromEventPatternObservable.js.map