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