-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathobservable
39 lines (37 loc) · 837 Bytes
/
observable
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
function Observer(onNext, onError = () => {}, onComplete = () => {}) {
const def = () => {};
if (typeof onNext === 'object') {
onError = onNext.error || def;
onComplete = onNext.complete || def;
onNext = onNext.next || def;
}
this.completed = false;
this.withError = false;
this.next = (val) => {
if (!this.completed) onNext(val);
};
this.error = (err) => {
if (!this.completed) {
this.completed = true;
this.withError = true;
onError(err);
}
};
this.complete = () => {
if (!this.completed) {
this.completed = true;
onComplete();
}
};
}
function Observable(observer) {
this.subscribe = (...fns) => {
if(observer) {
const obs = new Observer(...fns);
observer(obs);
}
};
this.unsubscribe = () => {
observer = null;
}
}