Skip to content

Commit 90bf3f1

Browse files
committed
perf(debounce): remove tryCatch/errorObject for custom tryCatching
1 parent 2515cfb commit 90bf3f1

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

src/operator/debounce.ts

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ import {Observable} from '../Observable';
33
import {Subscriber} from '../Subscriber';
44
import {Subscription} from '../Subscription';
55

6-
import {tryCatch} from '../util/tryCatch';
7-
import {errorObject} from '../util/errorObject';
8-
96
import {OuterSubscriber} from '../OuterSubscriber';
107
import {InnerSubscriber} from '../InnerSubscriber';
118
import {subscribeToResult} from '../util/subscribeToResult';
@@ -34,7 +31,6 @@ class DebounceOperator<T> implements Operator<T, T> {
3431
}
3532

3633
class DebounceSubscriber<T, R> extends OuterSubscriber<T, R> {
37-
3834
private value: T;
3935
private hasValue: boolean = false;
4036
private durationSubscription: Subscription = null;
@@ -44,31 +40,38 @@ class DebounceSubscriber<T, R> extends OuterSubscriber<T, R> {
4440
super(destination);
4541
}
4642

47-
protected _next(value: T) {
48-
let subscription = this.durationSubscription;
49-
const duration = tryCatch(this.durationSelector)(value);
43+
protected _next(value: T): void {
44+
try {
45+
const result = this.durationSelector.call(this, value);
5046

51-
if (duration === errorObject) {
52-
this.destination.error(errorObject.e);
53-
} else {
54-
this.value = value;
55-
this.hasValue = true;
56-
if (subscription) {
57-
subscription.unsubscribe();
58-
this.remove(subscription);
59-
}
60-
subscription = subscribeToResult(this, duration);
61-
if (!subscription.isUnsubscribed) {
62-
this.add(this.durationSubscription = subscription);
47+
if (result) {
48+
this._tryNext(value, result);
6349
}
50+
} catch (err) {
51+
this.destination.error(err);
6452
}
6553
}
6654

67-
protected _complete() {
55+
protected _complete(): void {
6856
this.emitValue();
6957
this.destination.complete();
7058
}
7159

60+
private _tryNext(value: T, duration: Observable<number> | Promise<number>): void {
61+
let subscription = this.durationSubscription;
62+
this.value = value;
63+
this.hasValue = true;
64+
if (subscription) {
65+
subscription.unsubscribe();
66+
this.remove(subscription);
67+
}
68+
69+
subscription = subscribeToResult(this, duration);
70+
if (!subscription.isUnsubscribed) {
71+
this.add(this.durationSubscription = subscription);
72+
}
73+
}
74+
7275
notifyNext(outerValue: T, innerValue: R,
7376
outerIndex: number, innerIndex: number,
7477
innerSub: InnerSubscriber<T, R>): void {
@@ -79,7 +82,7 @@ class DebounceSubscriber<T, R> extends OuterSubscriber<T, R> {
7982
this.emitValue();
8083
}
8184

82-
emitValue() {
85+
emitValue(): void {
8386
if (this.hasValue) {
8487
const value = this.value;
8588
const subscription = this.durationSubscription;

0 commit comments

Comments
 (0)