Skip to content

Commit

Permalink
feat(buffer): closingNotifier should support ObservableInput (#7073)
Browse files Browse the repository at this point in the history
* feat(buffer): closingNotifier should support ObservableInput

* chore(buffer): remove bad test
  • Loading branch information
jakovljevic-mladen committed Dec 15, 2022
1 parent d501961 commit 61b877a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
4 changes: 4 additions & 0 deletions spec-dtslint/operators/buffer-spec.ts
Expand Up @@ -9,3 +9,7 @@ it('should enforce types', () => {
const o = of(1, 2, 3).pipe(buffer()); // $ExpectError
const p = of(1, 2, 3).pipe(buffer(6)); // $ExpectError
});

it('should support Promises', () => {
const o = of(1, 2, 3).pipe(buffer(Promise.resolve('foo'))); // $ExpectType Observable<number[]>
});
39 changes: 38 additions & 1 deletion spec/operators/buffer-spec.ts
@@ -1,5 +1,5 @@
import { buffer, mergeMap, take, window, toArray } from 'rxjs/operators';
import { EMPTY, NEVER, throwError, of, Subject } from 'rxjs';
import { EMPTY, NEVER, throwError, of, Subject, interval } from 'rxjs';
import { TestScheduler } from 'rxjs/testing';
import { observableMatcher } from '../helpers/observableMatcher';
import { expect } from 'chai';
Expand Down Expand Up @@ -324,6 +324,43 @@ describe('Observable.prototype.buffer', () => {
expect(results).to.deep.equal([[1], [2], [], 'complete']);
});

it('should buffer when Promise resolves', (done) => {
const e1 = interval(3).pipe(take(5));
const expected = [
[0, 1],
[2, 3, 4],
];

e1.pipe(buffer(new Promise<void>((resolve) => setTimeout(() => resolve(), 8)))).subscribe({
next: (x) => {
expect(x).to.deep.equal(expected.shift());
},
error: () => done(new Error('should not be called')),
complete: () => {
expect(expected.length).to.equal(0);
done();
},
});
});

it('should raise error when Promise rejects', (done) => {
const e1 = interval(1).pipe(take(5));
const error = new Error('err');

e1.pipe(buffer(Promise.reject(error))).subscribe({
next: () => {
done(new Error('should not be called'));
},
error: (err: any) => {
expect(err).to.be.an('error');
done();
},
complete: () => {
done(new Error('should not be called'));
},
});
});

describe('equivalence with the window operator', () => {
const cases = [
{
Expand Down
13 changes: 7 additions & 6 deletions src/internal/operators/buffer.ts
@@ -1,8 +1,8 @@
import { Observable } from '../Observable';
import { OperatorFunction } from '../types';
import { OperatorFunction, ObservableInput } from '../types';
import { operate } from '../util/lift';
import { noop } from '../util/noop';
import { createOperatorSubscriber } from './OperatorSubscriber';
import { innerFrom } from '../observable/innerFrom';

/**
* Buffers the source Observable values until `closingNotifier` emits.
Expand All @@ -13,7 +13,8 @@ import { createOperatorSubscriber } from './OperatorSubscriber';
* ![](buffer.png)
*
* Buffers the incoming Observable values until the given `closingNotifier`
* Observable emits a value, at which point it emits the buffer on the output
* `ObservableInput` (that internally gets converted to an Observable)
* emits a value, at which point it emits the buffer on the output
* Observable and starts a new buffer internally, awaiting the next time
* `closingNotifier` emits.
*
Expand All @@ -36,12 +37,12 @@ import { createOperatorSubscriber } from './OperatorSubscriber';
* @see {@link bufferWhen}
* @see {@link window}
*
* @param {Observable<any>} closingNotifier An Observable that signals the
* @param closingNotifier An `ObservableInput` that signals the
* buffer to be emitted on the output Observable.
* @return A function that returns an Observable of buffers, which are arrays
* of values.
*/
export function buffer<T>(closingNotifier: Observable<any>): OperatorFunction<T, T[]> {
export function buffer<T>(closingNotifier: ObservableInput<any>): OperatorFunction<T, T[]> {
return operate((source, subscriber) => {
// The current buffered values.
let currentBuffer: T[] = [];
Expand All @@ -59,7 +60,7 @@ export function buffer<T>(closingNotifier: Observable<any>): OperatorFunction<T,
);

// Subscribe to the closing notifier.
closingNotifier.subscribe(
innerFrom(closingNotifier).subscribe(
createOperatorSubscriber(
subscriber,
() => {
Expand Down

0 comments on commit 61b877a

Please sign in to comment.