mapTo.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { Subscriber } from '../Subscriber';
  2. /**
  3. * Emits the given constant value on the output Observable every time the source
  4. * Observable emits a value.
  5. *
  6. * <span class="informal">Like {@link map}, but it maps every source value to
  7. * the same output value every time.</span>
  8. *
  9. * <img src="./img/mapTo.png" width="100%">
  10. *
  11. * Takes a constant `value` as argument, and emits that whenever the source
  12. * Observable emits a value. In other words, ignores the actual source value,
  13. * and simply uses the emission moment to know when to emit the given `value`.
  14. *
  15. * @example <caption>Map every click to the string 'Hi'</caption>
  16. * var clicks = Rx.Observable.fromEvent(document, 'click');
  17. * var greetings = clicks.mapTo('Hi');
  18. * greetings.subscribe(x => console.log(x));
  19. *
  20. * @see {@link map}
  21. *
  22. * @param {any} value The value to map each source value to.
  23. * @return {Observable} An Observable that emits the given `value` every time
  24. * the source Observable emits something.
  25. * @method mapTo
  26. * @owner Observable
  27. */
  28. export function mapTo(value) {
  29. return (source) => source.lift(new MapToOperator(value));
  30. }
  31. class MapToOperator {
  32. constructor(value) {
  33. this.value = value;
  34. }
  35. call(subscriber, source) {
  36. return source.subscribe(new MapToSubscriber(subscriber, this.value));
  37. }
  38. }
  39. /**
  40. * We need this JSDoc comment for affecting ESDoc.
  41. * @ignore
  42. * @extends {Ignored}
  43. */
  44. class MapToSubscriber extends Subscriber {
  45. constructor(destination, value) {
  46. super(destination);
  47. this.value = value;
  48. }
  49. _next(x) {
  50. this.destination.next(this.value);
  51. }
  52. }
  53. //# sourceMappingURL=mapTo.js.map