a zip code crypto-currency system good for red ONLY

FromEventPatternObservable.js 4.5KB

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