Front end of the Slack clone application.

map.js 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 Subscriber_1 = require('../Subscriber');
  8. /**
  9. * Applies a given `project` function to each value emitted by the source
  10. * Observable, and emits the resulting values as an Observable.
  11. *
  12. * <span class="informal">Like [Array.prototype.map()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map),
  13. * it passes each source value through a transformation function to get
  14. * corresponding output values.</span>
  15. *
  16. * <img src="./img/map.png" width="100%">
  17. *
  18. * Similar to the well known `Array.prototype.map` function, this operator
  19. * applies a projection to each value and emits that projection in the output
  20. * Observable.
  21. *
  22. * @example <caption>Map every click to the clientX position of that click</caption>
  23. * var clicks = Rx.Observable.fromEvent(document, 'click');
  24. * var positions = clicks.map(ev => ev.clientX);
  25. * positions.subscribe(x => console.log(x));
  26. *
  27. * @see {@link mapTo}
  28. * @see {@link pluck}
  29. *
  30. * @param {function(value: T, index: number): R} project The function to apply
  31. * to each `value` emitted by the source Observable. The `index` parameter is
  32. * the number `i` for the i-th emission that has happened since the
  33. * subscription, starting from the number `0`.
  34. * @param {any} [thisArg] An optional argument to define what `this` is in the
  35. * `project` function.
  36. * @return {Observable<R>} An Observable that emits the values from the source
  37. * Observable transformed by the given `project` function.
  38. * @method map
  39. * @owner Observable
  40. */
  41. function map(project, thisArg) {
  42. return function mapOperation(source) {
  43. if (typeof project !== 'function') {
  44. throw new TypeError('argument is not a function. Are you looking for `mapTo()`?');
  45. }
  46. return source.lift(new MapOperator(project, thisArg));
  47. };
  48. }
  49. exports.map = map;
  50. var MapOperator = (function () {
  51. function MapOperator(project, thisArg) {
  52. this.project = project;
  53. this.thisArg = thisArg;
  54. }
  55. MapOperator.prototype.call = function (subscriber, source) {
  56. return source.subscribe(new MapSubscriber(subscriber, this.project, this.thisArg));
  57. };
  58. return MapOperator;
  59. }());
  60. exports.MapOperator = MapOperator;
  61. /**
  62. * We need this JSDoc comment for affecting ESDoc.
  63. * @ignore
  64. * @extends {Ignored}
  65. */
  66. var MapSubscriber = (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_1.Subscriber));
  89. //# sourceMappingURL=map.js.map