a zip code crypto-currency system good for red ONLY

sampleTime.js 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { Subscriber } from '../Subscriber';
  2. import { async } from '../scheduler/async';
  3. /**
  4. * Emits the most recently emitted value from the source Observable within
  5. * periodic time intervals.
  6. *
  7. * <span class="informal">Samples the source Observable at periodic time
  8. * intervals, emitting what it samples.</span>
  9. *
  10. * <img src="./img/sampleTime.png" width="100%">
  11. *
  12. * `sampleTime` periodically looks at the source Observable and emits whichever
  13. * value it has most recently emitted since the previous sampling, unless the
  14. * source has not emitted anything since the previous sampling. The sampling
  15. * happens periodically in time every `period` milliseconds (or the time unit
  16. * defined by the optional `scheduler` argument). The sampling starts as soon as
  17. * the output Observable is subscribed.
  18. *
  19. * @example <caption>Every second, emit the most recent click at most once</caption>
  20. * var clicks = Rx.Observable.fromEvent(document, 'click');
  21. * var result = clicks.sampleTime(1000);
  22. * result.subscribe(x => console.log(x));
  23. *
  24. * @see {@link auditTime}
  25. * @see {@link debounceTime}
  26. * @see {@link delay}
  27. * @see {@link sample}
  28. * @see {@link throttleTime}
  29. *
  30. * @param {number} period The sampling period expressed in milliseconds or the
  31. * time unit determined internally by the optional `scheduler`.
  32. * @param {Scheduler} [scheduler=async] The {@link IScheduler} to use for
  33. * managing the timers that handle the sampling.
  34. * @return {Observable<T>} An Observable that emits the results of sampling the
  35. * values emitted by the source Observable at the specified time interval.
  36. * @method sampleTime
  37. * @owner Observable
  38. */
  39. export function sampleTime(period, scheduler = async) {
  40. return (source) => source.lift(new SampleTimeOperator(period, scheduler));
  41. }
  42. class SampleTimeOperator {
  43. constructor(period, scheduler) {
  44. this.period = period;
  45. this.scheduler = scheduler;
  46. }
  47. call(subscriber, source) {
  48. return source.subscribe(new SampleTimeSubscriber(subscriber, this.period, this.scheduler));
  49. }
  50. }
  51. /**
  52. * We need this JSDoc comment for affecting ESDoc.
  53. * @ignore
  54. * @extends {Ignored}
  55. */
  56. class SampleTimeSubscriber extends Subscriber {
  57. constructor(destination, period, scheduler) {
  58. super(destination);
  59. this.period = period;
  60. this.scheduler = scheduler;
  61. this.hasValue = false;
  62. this.add(scheduler.schedule(dispatchNotification, period, { subscriber: this, period }));
  63. }
  64. _next(value) {
  65. this.lastValue = value;
  66. this.hasValue = true;
  67. }
  68. notifyNext() {
  69. if (this.hasValue) {
  70. this.hasValue = false;
  71. this.destination.next(this.lastValue);
  72. }
  73. }
  74. }
  75. function dispatchNotification(state) {
  76. let { subscriber, period } = state;
  77. subscriber.notifyNext();
  78. this.schedule(state, period);
  79. }
  80. //# sourceMappingURL=sampleTime.js.map