map.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /** PURE_IMPORTS_START .._Subscriber 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 { Subscriber } from '../Subscriber';
  10. /**
  11. * Applies a given `project` function to each value emitted by the source
  12. * Observable, and emits the resulting values as an Observable.
  13. *
  14. * <span class="informal">Like [Array.prototype.map()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map),
  15. * it passes each source value through a transformation function to get
  16. * corresponding output values.</span>
  17. *
  18. * <img src="./img/map.png" width="100%">
  19. *
  20. * Similar to the well known `Array.prototype.map` function, this operator
  21. * applies a projection to each value and emits that projection in the output
  22. * Observable.
  23. *
  24. * @example <caption>Map every click to the clientX position of that click</caption>
  25. * var clicks = Rx.Observable.fromEvent(document, 'click');
  26. * var positions = clicks.map(ev => ev.clientX);
  27. * positions.subscribe(x => console.log(x));
  28. *
  29. * @see {@link mapTo}
  30. * @see {@link pluck}
  31. *
  32. * @param {function(value: T, index: number): R} project The function to apply
  33. * to each `value` emitted by the source Observable. The `index` parameter is
  34. * the number `i` for the i-th emission that has happened since the
  35. * subscription, starting from the number `0`.
  36. * @param {any} [thisArg] An optional argument to define what `this` is in the
  37. * `project` function.
  38. * @return {Observable<R>} An Observable that emits the values from the source
  39. * Observable transformed by the given `project` function.
  40. * @method map
  41. * @owner Observable
  42. */
  43. export function map(project, thisArg) {
  44. return function mapOperation(source) {
  45. if (typeof project !== 'function') {
  46. throw new TypeError('argument is not a function. Are you looking for `mapTo()`?');
  47. }
  48. return source.lift(new MapOperator(project, thisArg));
  49. };
  50. }
  51. export var MapOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  52. function MapOperator(project, thisArg) {
  53. this.project = project;
  54. this.thisArg = thisArg;
  55. }
  56. MapOperator.prototype.call = function (subscriber, source) {
  57. return source.subscribe(new MapSubscriber(subscriber, this.project, this.thisArg));
  58. };
  59. return MapOperator;
  60. }());
  61. /**
  62. * We need this JSDoc comment for affecting ESDoc.
  63. * @ignore
  64. * @extends {Ignored}
  65. */
  66. var MapSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  67. __extends(MapSubscriber, _super);
  68. function MapSubscriber(destination, project, thisArg) {
  69. _super.call(this, destination);
  70. this.project = project;
  71. this.count = 0;
  72. this.thisArg = thisArg || this;
  73. }
  74. // NOTE: This looks unoptimized, but it's actually purposefully NOT
  75. // using try/catch optimizations.
  76. MapSubscriber.prototype._next = function (value) {
  77. var result;
  78. try {
  79. result = this.project.call(this.thisArg, value, this.count++);
  80. }
  81. catch (err) {
  82. this.destination.error(err);
  83. return;
  84. }
  85. this.destination.next(result);
  86. };
  87. return MapSubscriber;
  88. }(Subscriber));
  89. //# sourceMappingURL=map.js.map