-
Notifications
You must be signed in to change notification settings - Fork 0
/
Observable.js
146 lines (130 loc) · 3.17 KB
/
Observable.js
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const IDLE = 0;
const ACTIVE = 1;
const DONE = 2;
const noop = () => {};
class Observable {
constructor(producer) {
this.producer = producer;
}
map(fn) {
return new Observable((open, next, fail, done, external) => {
this.listen(open, value => next(fn(value)), fail, done, external);
});
}
tap(fn) {
return this.map(value => {
fn(value);
return value;
});
}
filter(fn) {
return new Observable((open, next, fail, done, external) => {
this.listen(
open,
value => fn(value) && next(value),
fail,
done,
external
);
});
}
take(amount) {
return new Observable((open, next, fail, done, external) => {
let count = 0;
const cancellation = new CancelInterceptor(external);
this.listen(
open,
value => {
next(value);
if (++count >= amount) cancellation.run();
},
fail,
done,
cancellation
);
});
}
listen(
open = noop,
next = noop,
fail = noop,
done = noop,
external = new OpenObservable()
) {
const cancellation = new ExternalCancelInterceptor(external);
let state = IDLE;
this.producer(
() => {
if (state === IDLE) {
state = ACTIVE;
open();
}
},
value => {
if (state === ACTIVE) {
try {
next(value);
} catch (error) {
fail(error);
}
}
},
error => {
if (state === ACTIVE) fail(error);
},
cancelled => {
if (state === ACTIVE) {
cancellation.run();
done(cancelled);
state = DONE;
}
},
cancellation
);
}
}
// just an open observable that do nothing
class OpenObservable extends Observable {
constructor() {
super(open => open())
}
}
// an observable that just emits cancel signal
class CancelSignal extends OpenObservable {
constructor() {
super();
this.run = noop;
}
listen(open, next, fail, done, external) {
this.run = () => next(Observable.CANCEL);
super.listen(open, next, fail, done, external);
}
}
// use internally to intercept observable
// to emit cancel signal to the source via run.
class CancelInterceptor extends Observable {
constructor(observable) {
super((open, next, fail, done, external) => observable.listen(open, next, fail, done, external));
this.run = noop;
}
listen(open, next, fail, done, external) {
this.run = () => next(Observable.CANCEL);
super.listen(open, next, fail, done, external);
}
}
// observable that intercepts external observable
// for cancellation
class ExternalCancelInterceptor extends Observable {
constructor(observable) {
super((open, next, fail, done, external) => observable.listen(open, next, fail, done, external));
this.run = noop;
}
listen(open, next, fail, done, external) {
const cancellation = new CancelInterceptor(external);
this.run = () => cancellation.run();
super.listen(open, next, fail, done, cancellation);
}
}
Observable.CANCEL = Symbol("CANCEL");
Observable.CancelSignal = CancelSignal;
module.exports = Observable;