Skip to content

Commit

Permalink
feat(mapTo): add higher-order lettable version of mapTo
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonaden committed Aug 28, 2017
1 parent bf33b97 commit e97530f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 35 deletions.
37 changes: 2 additions & 35 deletions src/operator/mapTo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Operator } from '../Operator';
import { Subscriber } from '../Subscriber';
import { Observable } from '../Observable';
import { mapTo as higherOrder } from '../operators';

/**
* Emits the given constant value on the output Observable every time the source
Expand Down Expand Up @@ -29,37 +28,5 @@ import { Observable } from '../Observable';
* @owner Observable
*/
export function mapTo<T, R>(this: Observable<T>, value: R): Observable<R> {
return this.lift(new MapToOperator(value));
}

class MapToOperator<T, R> implements Operator<T, R> {

value: R;

constructor(value: R) {
this.value = value;
}

call(subscriber: Subscriber<R>, source: any): any {
return source.subscribe(new MapToSubscriber(subscriber, this.value));
}
}

/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class MapToSubscriber<T, R> extends Subscriber<T> {

value: R;

constructor(destination: Subscriber<R>, value: R) {
super(destination);
this.value = value;
}

protected _next(x: T) {
this.destination.next(this.value);
}
return higherOrder(value)(this);
}
1 change: 1 addition & 0 deletions src/operators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export { ignoreElements } from './ignoreElements';
export { isEmpty } from './isEmpty';
export { last } from './last';
export { map } from './map';
export { mapTo } from './mapTo';
export { materialize } from './materialize';
export { max } from './max';
export { merge } from './merge';
Expand Down
66 changes: 66 additions & 0 deletions src/operators/mapTo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Operator } from '../Operator';
import { Subscriber } from '../Subscriber';
import { Observable } from '../Observable';
import { OperatorFunction } from '../interfaces';

/**
* Emits the given constant value on the output Observable every time the source
* Observable emits a value.
*
* <span class="informal">Like {@link map}, but it maps every source value to
* the same output value every time.</span>
*
* <img src="./img/mapTo.png" width="100%">
*
* Takes a constant `value` as argument, and emits that whenever the source
* Observable emits a value. In other words, ignores the actual source value,
* and simply uses the emission moment to know when to emit the given `value`.
*
* @example <caption>Map every click to the string 'Hi'</caption>
* var clicks = Rx.Observable.fromEvent(document, 'click');
* var greetings = clicks.mapTo('Hi');
* greetings.subscribe(x => console.log(x));
*
* @see {@link map}
*
* @param {any} value The value to map each source value to.
* @return {Observable} An Observable that emits the given `value` every time
* the source Observable emits something.
* @method mapTo
* @owner Observable
*/
export function mapTo<T, R>(value: R): OperatorFunction<T, R> {
return (source: Observable<T>) => source.lift(new MapToOperator(value));
}

class MapToOperator<T, R> implements Operator<T, R> {

value: R;

constructor(value: R) {
this.value = value;
}

call(subscriber: Subscriber<R>, source: any): any {
return source.subscribe(new MapToSubscriber(subscriber, this.value));
}
}

/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class MapToSubscriber<T, R> extends Subscriber<T> {

value: R;

constructor(destination: Subscriber<R>, value: R) {
super(destination);
this.value = value;
}

protected _next(x: T) {
this.destination.next(this.value);
}
}

0 comments on commit e97530f

Please sign in to comment.