bufferTime.js 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /** PURE_IMPORTS_START .._scheduler_async,.._util_isScheduler,.._operators_bufferTime PURE_IMPORTS_END */
  2. import { async } from '../scheduler/async';
  3. import { isScheduler } from '../util/isScheduler';
  4. import { bufferTime as higherOrder } from '../operators/bufferTime';
  5. /* tslint:enable:max-line-length */
  6. /**
  7. * Buffers the source Observable values for a specific time period.
  8. *
  9. * <span class="informal">Collects values from the past as an array, and emits
  10. * those arrays periodically in time.</span>
  11. *
  12. * <img src="./img/bufferTime.png" width="100%">
  13. *
  14. * Buffers values from the source for a specific time duration `bufferTimeSpan`.
  15. * Unless the optional argument `bufferCreationInterval` is given, it emits and
  16. * resets the buffer every `bufferTimeSpan` milliseconds. If
  17. * `bufferCreationInterval` is given, this operator opens the buffer every
  18. * `bufferCreationInterval` milliseconds and closes (emits and resets) the
  19. * buffer every `bufferTimeSpan` milliseconds. When the optional argument
  20. * `maxBufferSize` is specified, the buffer will be closed either after
  21. * `bufferTimeSpan` milliseconds or when it contains `maxBufferSize` elements.
  22. *
  23. * @example <caption>Every second, emit an array of the recent click events</caption>
  24. * var clicks = Rx.Observable.fromEvent(document, 'click');
  25. * var buffered = clicks.bufferTime(1000);
  26. * buffered.subscribe(x => console.log(x));
  27. *
  28. * @example <caption>Every 5 seconds, emit the click events from the next 2 seconds</caption>
  29. * var clicks = Rx.Observable.fromEvent(document, 'click');
  30. * var buffered = clicks.bufferTime(2000, 5000);
  31. * buffered.subscribe(x => console.log(x));
  32. *
  33. * @see {@link buffer}
  34. * @see {@link bufferCount}
  35. * @see {@link bufferToggle}
  36. * @see {@link bufferWhen}
  37. * @see {@link windowTime}
  38. *
  39. * @param {number} bufferTimeSpan The amount of time to fill each buffer array.
  40. * @param {number} [bufferCreationInterval] The interval at which to start new
  41. * buffers.
  42. * @param {number} [maxBufferSize] The maximum buffer size.
  43. * @param {Scheduler} [scheduler=async] The scheduler on which to schedule the
  44. * intervals that determine buffer boundaries.
  45. * @return {Observable<T[]>} An observable of arrays of buffered values.
  46. * @method bufferTime
  47. * @owner Observable
  48. */
  49. export function bufferTime(bufferTimeSpan) {
  50. var length = arguments.length;
  51. var scheduler = async;
  52. if (isScheduler(arguments[arguments.length - 1])) {
  53. scheduler = arguments[arguments.length - 1];
  54. length--;
  55. }
  56. var bufferCreationInterval = null;
  57. if (length >= 2) {
  58. bufferCreationInterval = arguments[1];
  59. }
  60. var maxBufferSize = Number.POSITIVE_INFINITY;
  61. if (length >= 3) {
  62. maxBufferSize = arguments[2];
  63. }
  64. return higherOrder(bufferTimeSpan, bufferCreationInterval, maxBufferSize, scheduler)(this);
  65. }
  66. //# sourceMappingURL=bufferTime.js.map