-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubscriptionObserver.ts
46 lines (46 loc) · 1.39 KB
/
SubscriptionObserver.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
import { Observer } from "./Observer.ts";
import { Subscription } from "./Subscription.ts";
import { assertIsSubscription } from "./utils.ts";
/**
* A SubscriptionObserver is a normalized Observer which wraps the
* observer object supplied to subscribe.
*/
export class SubscriptionObserver<Value = unknown>
implements Required<Omit<Observer<Value>, "start">> {
private _subscription: Subscription<Value>;
constructor(subscription: Subscription<Value>) {
assertIsSubscription(subscription);
this._subscription = subscription;
}
next(value: Value): void {
if (this.closed) return;
try {
Subscription.getObserver(this._subscription).next?.(value);
} catch (error) {
Subscription.getErrorReporter(this._subscription)(error);
}
}
error(error: unknown): void {
if (this.closed) return;
try {
Subscription.getObserver(this._subscription).error?.(error);
} catch (error) {
Subscription.getErrorReporter(this._subscription)(error);
}
}
complete(): void {
if (this.closed) return;
try {
Subscription.getObserver(this._subscription).complete?.();
this._subscription.unsubscribe();
} catch (error) {
Subscription.getErrorReporter(this._subscription)(error);
}
}
/**
* A boolean value indicating whether the subscription is closed
*/
get closed(): boolean {
return this._subscription.closed;
}
}