-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathdebounceSync.ts
41 lines (39 loc) · 1.19 KB
/
debounceSync.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
/**
* @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 {
asapScheduler,
MonoTypeOperatorFunction,
Observable,
Subscription,
} from "rxjs";
import { OperatorSubscriber } from "../OperatorSubscriber";
export function debounceSync<T>(): MonoTypeOperatorFunction<T> {
return (source) =>
new Observable<T>((subscriber) => {
let actionSubscription: Subscription | undefined;
let actionValue: T | undefined;
source.subscribe(
new OperatorSubscriber(subscriber, {
complete: () => {
if (actionSubscription) {
subscriber.next(actionValue);
}
subscriber.complete();
},
error: (error) => subscriber.error(error),
next: (value) => {
actionValue = value;
if (!actionSubscription) {
actionSubscription = asapScheduler.schedule(() => {
subscriber.next(actionValue);
actionSubscription = undefined;
});
subscriber.add(actionSubscription);
}
},
})
);
});
}