1 import { Operator } from '../Operator';
2 import { Subscriber } from '../Subscriber';
3 import { Observable } from '../Observable';
4 import { OperatorFunction } from '../types';
7 * Emits the given constant value on the output Observable every time the source
8 * Observable emits a value.
10 * <span class="informal">Like {@link map}, but it maps every source value to
11 * the same output value every time.</span>
15 * Takes a constant `value` as argument, and emits that whenever the source
16 * Observable emits a value. In other words, ignores the actual source value,
17 * and simply uses the emission moment to know when to emit the given `value`.
20 * Map every click to the string 'Hi'
22 * import { fromEvent } from 'rxjs';
23 * import { mapTo } from 'rxjs/operators';
25 * const clicks = fromEvent(document, 'click');
26 * const greetings = clicks.pipe(mapTo('Hi'));
27 * greetings.subscribe(x => console.log(x));
32 * @param {any} value The value to map each source value to.
33 * @return {Observable} An Observable that emits the given `value` every time
34 * the source Observable emits something.
38 export function mapTo<T, R>(value: R): OperatorFunction<T, R> {
39 return (source: Observable<T>) => source.lift(new MapToOperator(value));
42 class MapToOperator<T, R> implements Operator<T, R> {
46 constructor(value: R) {
50 call(subscriber: Subscriber<R>, source: any): any {
51 return source.subscribe(new MapToSubscriber(subscriber, this.value));
56 * We need this JSDoc comment for affecting ESDoc.
60 class MapToSubscriber<T, R> extends Subscriber<T> {
64 constructor(destination: Subscriber<R>, value: R) {
69 protected _next(x: T) {
70 this.destination.next(this.value);