-
Notifications
You must be signed in to change notification settings - Fork 0
/
observer.ts
45 lines (39 loc) · 860 Bytes
/
observer.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
import { CompleteMethod, ErrorMethod, NextMethod } from "./types";
import { Subscriber } from "./subscriber";
export class Observer<T> extends Subscriber<T> {
private nextCb?: NextMethod<T>;
private errorCb?: ErrorMethod<T>;
private completeCb?: CompleteMethod<T>;
constructor(next?: NextMethod<T>, error?: ErrorMethod<T>, complete?: CompleteMethod<T>) {
super();
this.nextCb = next;
this.errorCb = error;
this.completeCb = complete;
}
next(x: T) {
if (!this.completed) {
if (this.nextCb) {
this.nextCb(x);
}
else {
super.next(x);
}
}
}
error(err: any) {
if (this.errorCb) {
this.errorCb(err);
}
else {
super.error(err);
}
}
complete() {
if (this.completeCb) {
this.completeCb();
}
else {
super.complete();
}
}
}