-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
52 lines (50 loc) · 1.67 KB
/
index.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
import {
asyncScheduler,
defer,
Observable,
ObservableInput,
OperatorFunction,
scheduled,
Subject,
} from "rxjs"
import {exhaustMap, finalize, throttle} from "rxjs/operators"
/**
* Like exhaustMap, but also includes the trailing value emitted from the source observable while waiting for the preceding inner observable to complete
*
* Original code adapted from https://github.com/ReactiveX/rxjs/issues/5004
* @param {function<T, K>(value: T, ?index: number): ObservableInput<K>} project - A function that, when applied to an item emitted by the
* source Observable, returns a projected Observable.
*/
export function exhaustMapWithTrailing<T, R>(
project: (value: T, index: number) => ObservableInput<R>
): OperatorFunction<T, R> {
return (source): Observable<R> =>
defer(() => {
const release = new Subject<void>()
return source.pipe(
throttle(() => release, {
leading: true,
trailing: true,
}),
exhaustMap((value, index) =>
scheduled(project(value, index), asyncScheduler).pipe(
finalize(() => {
release.next()
})
)
)
)
})
}
/**
* Like exhaustMap, but also includes the trailing value emitted from the source observable while waiting for the preceding inner observable to complete
*
* Original code adapted from https://github.com/ReactiveX/rxjs/issues/5004
* @param {ObservableInput} innerObservable An Observable to replace each value from
* the source Observable.
*/
export function exhaustMapToWithTrailing<T, R>(
innerObservable: Observable<R>
): OperatorFunction<T, R> {
return exhaustMapWithTrailing(() => innerObservable)
}