bufferWhen.js 1.1 KB

1234567891011121314151617181920212223
  1. import { operate } from '../util/lift';
  2. import { noop } from '../util/noop';
  3. import { createOperatorSubscriber } from './OperatorSubscriber';
  4. import { innerFrom } from '../observable/innerFrom';
  5. export function bufferWhen(closingSelector) {
  6. return operate((source, subscriber) => {
  7. let buffer = null;
  8. let closingSubscriber = null;
  9. const openBuffer = () => {
  10. closingSubscriber === null || closingSubscriber === void 0 ? void 0 : closingSubscriber.unsubscribe();
  11. const b = buffer;
  12. buffer = [];
  13. b && subscriber.next(b);
  14. innerFrom(closingSelector()).subscribe((closingSubscriber = createOperatorSubscriber(subscriber, openBuffer, noop)));
  15. };
  16. openBuffer();
  17. source.subscribe(createOperatorSubscriber(subscriber, (value) => buffer === null || buffer === void 0 ? void 0 : buffer.push(value), () => {
  18. buffer && subscriber.next(buffer);
  19. subscriber.complete();
  20. }, undefined, () => (buffer = closingSubscriber = null)));
  21. });
  22. }
  23. //# sourceMappingURL=bufferWhen.js.map