1+ import { Operator } from '../Operator' ;
2+ import { Subscriber } from '../Subscriber' ;
3+ import { Observable } from '../Observable' ;
4+ import { OperatorFunction } from '../interfaces' ;
5+
6+ /**
7+ * Emits the given constant value on the output Observable every time the source
8+ * Observable emits a value.
9+ *
10+ * <span class="informal">Like {@link map}, but it maps every source value to
11+ * the same output value every time.</span>
12+ *
13+ * <img src="./img/mapTo.png" width="100%">
14+ *
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`.
18+ *
19+ * @example <caption>Map every click to the string 'Hi'</caption>
20+ * var clicks = Rx.Observable.fromEvent(document, 'click');
21+ * var greetings = clicks.mapTo('Hi');
22+ * greetings.subscribe(x => console.log(x));
23+ *
24+ * @see {@link map }
25+ *
26+ * @param {any } value The value to map each source value to.
27+ * @return {Observable } An Observable that emits the given `value` every time
28+ * the source Observable emits something.
29+ * @method mapTo
30+ * @owner Observable
31+ */
32+ export function mapTo < T , R > ( value : R ) : OperatorFunction < T , R > {
33+ return ( source : Observable < T > ) => source . lift ( new MapToOperator ( value ) ) ;
34+ }
35+
36+ class MapToOperator < T , R > implements Operator < T , R > {
37+
38+ value : R ;
39+
40+ constructor ( value : R ) {
41+ this . value = value ;
42+ }
43+
44+ call ( subscriber : Subscriber < R > , source : any ) : any {
45+ return source . subscribe ( new MapToSubscriber ( subscriber , this . value ) ) ;
46+ }
47+ }
48+
49+ /**
50+ * We need this JSDoc comment for affecting ESDoc.
51+ * @ignore
52+ * @extends {Ignored }
53+ */
54+ class MapToSubscriber < T , R > extends Subscriber < T > {
55+
56+ value : R ;
57+
58+ constructor ( destination : Subscriber < R > , value : R ) {
59+ super ( destination ) ;
60+ this . value = value ;
61+ }
62+
63+ protected _next ( x : T ) {
64+ this . destination . next ( this . value ) ;
65+ }
66+ }
0 commit comments