forked from ReactiveX/rxjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
endWith.ts
58 lines (57 loc) · 1.79 KB
/
endWith.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/** prettier */
import { fromArrayLike } from '../observable/innerFrom';
import { OperatorFunction, ValueFromArray } from '../types';
import { operate } from '../util/lift';
import { concatAll } from './concatAll';
/**
* Returns an observable that will emit all values from the source, then synchronously emit
* the provided value(s) immediately after the source completes.
*
* This is useful for knowing when an observable ends. Particularly when paired with an
* operator like {@link takeUntil}
*
* ![](endWith.png)
*
* ## Example
*
* Emit values to know when an interval starts and stops. The interval will
* stop when a user clicks anywhere on the document.
*
* ```ts
* import { interval, map, fromEvent, startWith, takeUntil, endWith } from 'rxjs';
*
* const ticker$ = interval(5000).pipe(
* map(() => 'tick')
* );
*
* const documentClicks$ = fromEvent(document, 'click');
*
* ticker$.pipe(
* startWith('interval started'),
* takeUntil(documentClicks$),
* endWith('interval ended by click')
* )
* .subscribe(x => console.log(x));
*
* // Result (assuming a user clicks after 15 seconds)
* // 'interval started'
* // 'tick'
* // 'tick'
* // 'tick'
* // 'interval ended by click'
* ```
*
* @see {@link startWith}
* @see {@link concat}
* @see {@link takeUntil}
*
* @param values Items you want the modified Observable to emit last.
* @return A function that returns an Observable that emits all values from the
* source, then synchronously emits the provided value(s) immediately after the
* source completes.
*/
export function endWith<T, A extends readonly unknown[] = T[]>(...values: A): OperatorFunction<T, T | ValueFromArray<A>> {
return operate((source, subscriber) => {
concatAll()(fromArrayLike([source, fromArrayLike(values)])).subscribe(subscriber);
});
}