1 | import { SimpleOuterSubscriber, innerSubscribe, SimpleInnerSubscriber } from '../innerSubscribe';
|
---|
2 | export function audit(durationSelector) {
|
---|
3 | return function auditOperatorFunction(source) {
|
---|
4 | return source.lift(new AuditOperator(durationSelector));
|
---|
5 | };
|
---|
6 | }
|
---|
7 | class AuditOperator {
|
---|
8 | constructor(durationSelector) {
|
---|
9 | this.durationSelector = durationSelector;
|
---|
10 | }
|
---|
11 | call(subscriber, source) {
|
---|
12 | return source.subscribe(new AuditSubscriber(subscriber, this.durationSelector));
|
---|
13 | }
|
---|
14 | }
|
---|
15 | class AuditSubscriber extends SimpleOuterSubscriber {
|
---|
16 | constructor(destination, durationSelector) {
|
---|
17 | super(destination);
|
---|
18 | this.durationSelector = durationSelector;
|
---|
19 | this.hasValue = false;
|
---|
20 | }
|
---|
21 | _next(value) {
|
---|
22 | this.value = value;
|
---|
23 | this.hasValue = true;
|
---|
24 | if (!this.throttled) {
|
---|
25 | let duration;
|
---|
26 | try {
|
---|
27 | const { durationSelector } = this;
|
---|
28 | duration = durationSelector(value);
|
---|
29 | }
|
---|
30 | catch (err) {
|
---|
31 | return this.destination.error(err);
|
---|
32 | }
|
---|
33 | const innerSubscription = innerSubscribe(duration, new SimpleInnerSubscriber(this));
|
---|
34 | if (!innerSubscription || innerSubscription.closed) {
|
---|
35 | this.clearThrottle();
|
---|
36 | }
|
---|
37 | else {
|
---|
38 | this.add(this.throttled = innerSubscription);
|
---|
39 | }
|
---|
40 | }
|
---|
41 | }
|
---|
42 | clearThrottle() {
|
---|
43 | const { value, hasValue, throttled } = this;
|
---|
44 | if (throttled) {
|
---|
45 | this.remove(throttled);
|
---|
46 | this.throttled = undefined;
|
---|
47 | throttled.unsubscribe();
|
---|
48 | }
|
---|
49 | if (hasValue) {
|
---|
50 | this.value = undefined;
|
---|
51 | this.hasValue = false;
|
---|
52 | this.destination.next(value);
|
---|
53 | }
|
---|
54 | }
|
---|
55 | notifyNext() {
|
---|
56 | this.clearThrottle();
|
---|
57 | }
|
---|
58 | notifyComplete() {
|
---|
59 | this.clearThrottle();
|
---|
60 | }
|
---|
61 | }
|
---|
62 | //# sourceMappingURL=audit.js.map |
---|