-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
delayUntil.ts
46 lines (44 loc) · 1.35 KB
/
delayUntil.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
/**
* @license Use of this source code is governed by an MIT-style license that
* can be found in the LICENSE file at https://github.com/cartant/rxjs-etc
*/
import { concat, Observable, OperatorFunction, Subscription } from "rxjs";
import { publish } from "rxjs/operators";
export function delayUntil<T>(
notifier: Observable<any>
): OperatorFunction<T, T> {
return (source) =>
source.pipe(
publish((published) => {
const delayed = new Observable<T>((subscriber) => {
let buffering = true;
const buffer: T[] = [];
const subscription = new Subscription();
subscription.add(
notifier.subscribe(
() => {
buffer.forEach((value) => subscriber.next(value));
subscriber.complete();
},
(error) => subscriber.error(error),
() => {
buffering = false;
buffer.length = 0;
}
)
);
subscription.add(() => {
buffer.length = 0;
});
subscription.add(
published.subscribe(
(value) => buffering && buffer.push(value),
(error) => subscriber.error(error)
)
);
return subscription;
});
return concat(delayed, published);
})
);
}