Skip to content

Commit 60d6c40

Browse files
authoredDec 15, 2022
feat(skipUntil): notifier should support ObservableInput (#7091)
1 parent 8c4347c commit 60d6c40

File tree

3 files changed

+54
-14
lines changed

3 files changed

+54
-14
lines changed
 

‎spec-dtslint/operators/skipUntil-spec.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,9 @@ it('should infer correctly', () => {
77

88
it('should enforce types', () => {
99
const o = of('foo', 'bar', 'baz').pipe(skipUntil()); // $ExpectError
10-
const p = of('foo', 'bar', 'baz').pipe(skipUntil('7')); // $ExpectError
10+
const p = of('foo', 'bar', 'baz').pipe(skipUntil(7)); // $ExpectError
11+
});
12+
13+
it('should support Promises', () => {
14+
of(1, 2, 3).pipe(skipUntil(Promise.resolve('foo'))); // $ExpectType Observable<number>
1115
});

‎spec/operators/skipUntil-spec.ts

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from 'chai';
2-
import { concat, defer, of, Subject, Observable } from 'rxjs';
2+
import { concat, defer, of, Subject, Observable, interval } from 'rxjs';
33
import { skipUntil, mergeMap, take } from 'rxjs/operators';
44
import { asInteropObservable } from '../helpers/interop-helper';
55
import { TestScheduler } from 'rxjs/testing';
@@ -367,4 +367,38 @@ describe('skipUntil', () => {
367367

368368
expect(sideEffects).to.deep.equal([0, 1, 2]);
369369
});
370+
371+
it('should skip until Promise resolves', (done) => {
372+
const e1 = interval(3).pipe(take(5));
373+
const expected = [2, 3, 4];
374+
375+
e1.pipe(skipUntil(new Promise<void>((resolve) => setTimeout(() => resolve(), 8)))).subscribe({
376+
next: (x) => {
377+
expect(x).to.deep.equal(expected.shift());
378+
},
379+
error: () => done(new Error('should not be called')),
380+
complete: () => {
381+
expect(expected.length).to.equal(0);
382+
done();
383+
},
384+
});
385+
});
386+
387+
it('should raise error when Promise rejects', (done) => {
388+
const e1 = interval(1).pipe(take(5));
389+
const error = new Error('err');
390+
391+
e1.pipe(skipUntil(Promise.reject(error))).subscribe({
392+
next: () => {
393+
done(new Error('should not be called'));
394+
},
395+
error: (err: any) => {
396+
expect(err).to.be.an('error');
397+
done();
398+
},
399+
complete: () => {
400+
done(new Error('should not be called'));
401+
},
402+
});
403+
});
370404
});

‎src/internal/operators/skipUntil.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { Observable } from '../Observable';
2-
import { MonoTypeOperatorFunction } from '../types';
1+
import { MonoTypeOperatorFunction, ObservableInput } from '../types';
32
import { operate } from '../util/lift';
43
import { createOperatorSubscriber } from './OperatorSubscriber';
54
import { innerFrom } from '../observable/innerFrom';
@@ -8,19 +7,22 @@ import { noop } from '../util/noop';
87
/**
98
* Returns an Observable that skips items emitted by the source Observable until a second Observable emits an item.
109
*
11-
* The `skipUntil` operator causes the observable stream to skip the emission of values until the passed in observable emits the first value.
12-
* This can be particularly useful in combination with user interactions, responses of http requests or waiting for specific times to pass by.
10+
* The `skipUntil` operator causes the observable stream to skip the emission of values until the passed in observable
11+
* emits the first value. This can be particularly useful in combination with user interactions, responses of HTTP
12+
* requests or waiting for specific times to pass by.
1313
*
1414
* ![](skipUntil.png)
1515
*
16-
* Internally the `skipUntil` operator subscribes to the passed in observable (in the following called *notifier*) in order to recognize the emission
17-
* of its first value. When this happens, the operator unsubscribes from the *notifier* and starts emitting the values of the *source*
18-
* observable. It will never let the *source* observable emit any values if the *notifier* completes or throws an error without emitting
19-
* a value before.
16+
* Internally, the `skipUntil` operator subscribes to the passed in `notifier` `ObservableInput` (which gets converted
17+
* to an Observable) in order to recognize the emission of its first value. When `notifier` emits next, the operator
18+
* unsubscribes from it and starts emitting the values of the *source* observable until it completes or errors. It
19+
* will never let the *source* observable emit any values if the `notifier` completes or throws an error without
20+
* emitting a value before.
2021
*
2122
* ## Example
2223
*
23-
* In the following example, all emitted values of the interval observable are skipped until the user clicks anywhere within the page
24+
* In the following example, all emitted values of the interval observable are skipped until the user clicks anywhere
25+
* within the page
2426
*
2527
* ```ts
2628
* import { interval, fromEvent, skipUntil } from 'rxjs';
@@ -41,13 +43,13 @@ import { noop } from '../util/noop';
4143
* @see {@link skipWhile}
4244
* @see {@link skipLast}
4345
*
44-
* @param {Observable} notifier - The second Observable that has to emit an item before the source Observable's elements begin to
46+
* @param notifier An `ObservableInput` that has to emit an item before the source Observable elements begin to
4547
* be mirrored by the resulting Observable.
4648
* @return A function that returns an Observable that skips items from the
47-
* source Observable until the second Observable emits an item, then emits the
49+
* source Observable until the `notifier` Observable emits an item, then emits the
4850
* remaining items.
4951
*/
50-
export function skipUntil<T>(notifier: Observable<any>): MonoTypeOperatorFunction<T> {
52+
export function skipUntil<T>(notifier: ObservableInput<any>): MonoTypeOperatorFunction<T> {
5153
return operate((source, subscriber) => {
5254
let taking = false;
5355

0 commit comments

Comments
 (0)
Please sign in to comment.