Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

map.ts 3.0KB

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