-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
218 lines (185 loc) · 6.06 KB
/
index.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const root = typeof window === 'undefined' ? global : window;
function SynchronousUnsubscription() { throw SynchronousUnsubscription; }
function noop() { }
function obs(fn) { return new Observable(fn); }
function catchAll(fn, ...args) { try { return fn(...args); } catch(e) { /* ignore */ }}
export class Scheduler {
schedule(fn) {
throw new Error('Not implemented');
}
scheduleFuture() {
throw new Error('Not implemented');
}
}
Scheduler.immediate = new class ImmediateScheduler extends Scheduler {
schedule(fn) {
fn();
return noop;
}
};
Scheduler.currentThread = new class CurrentThreadScheduler extends Scheduler {
schedule(fn) {
let {q}=this;
if(q)
q.push(fn);
else {
q=this.q=[fn];
while(q.length>0)
q.shift()();
this.q=null;
}
return ()=>{const idx=q.indexOf(); if(idx>=0) q.splice(idx,1);};
}
};
Scheduler.default = new class DefaultScheduler extends Scheduler {
schedule(fn) {
const t=root.setTimeout(fn,0);
return ()=>root.clearTimeout(t);
}
scheduleFuture(fn, delay) {
delay = Math.max(0, typeof delay === 'number' ? delay : delay - Date.now());
const t=root.setTimeout(fn, delay);
return ()=>root.clearTimeout(t);
}
schedulePeriodic(fn, interval, delay) {
interval = Math.max(0, interval);
delay = Math.max(0, typeof delay === 'number' ? delay : delay - Date.now());
if(delay) {
let i;
const t = this.scheduleFuture(()=>i=this.schedulePeriodic(fn,interval), delay);
return ()=>{t();i && i();};
} else {
const i = root.setInterval(fn, interval);
return ()=>root.clearInterval(i);
}
}
};
export class Observable {
static empty() { return obs((n,e,c)=>c()); }
static of(...args) {
return this.from(args);
}
static from(x) {
if(typeof x[Symbol.iterator] === 'function') {
return obs((n,e,c)=>{
for(const y of x) {
n(y);
}
c();
})
} else if(x instanceof Observable) {
return x;
} else if(x instanceof Promise) {
return obs((n,e,c)=>{
let cancelled=false;
x.then(y=>{if(!cancelled){n(y);c();}}, e);
return ()=>cancelled=true;
});
} else if(typeof x === 'function') {
return this.from(x());
}
}
static range(start, len) {
if(typeof len !== 'number')
len=Infinity;
return this.from(function*() {
const end=start+len;
for(let i=start; i<end; ++i) yield i;
});
}
static interval(intv) {
return obs((n,e,c)=>{
let i=0;
return Scheduler.default.schedulePeriodic(()=>n(i++), intv);
});
}
constructor(_subFn) {
this._subscribe = _subFn;
}
_subscribe() { throw new Error('Not implemented'); }
subscribe(n=noop,e=noop,c=noop) {
const n2=x=>{try{return n(x);}catch(e){err(e);}};
const e2=x=>{try{e(x);unsubscribe();}catch(e){err(e);}};
const c2=()=>{try{c();unsubscribe();}catch(e){err(e);}};
let unsubscribe = SynchronousUnsubscription;
let res=noop;
try {
res=this._subscribe(n2,e2,c2)||noop;
} catch(e) {
if(e===SynchronousUnsubscription) {
realUnsubscribe();
} else {
throw e;
}
}
unsubscribe = realUnsubscribe;
return {unsubscribe};
function err(e) { unsubscribe(); throw e; }
function realUnsubscribe() { n=e=c=noop; try{res();}catch(e){/*ignore*/}finally{res=noop;} }
}
_intercept(nfn) { return obs((n,e,c)=>this._subscribe(nfn(n,e,c),e,c)); }
map(fn) { return this._intercept(n=>x=>n(fn(x))); }
filter(fn) { return this._intercept(n=>x=>fn(x)&&n(x)); }
take(num) { return num>=0 ? this._intercept((n,e,c)=>{let i=num; return x=>{n(x);if(--i<=0) c();}}) : Observable.empty(); }
drop(num) { return num>=0 ? this._intercept((n,e,c)=>{let i=num; return x=>{if(i>0) i--; else n(x);}}) : this; }
merge(num) {
return obs((n,e,c)=>{
let done=false;
let active=0;
const q=[];
const resources = [];
function pump() {
while(active<num && q.length>0) {
const o=Observable.from(q.shift());
let sync=false;
try {
++active;
const res=o.subscribe(n, e, ()=>{
--active;
if(!sync) {
sync=true;
} else {
const idx=resources.indexOf(res);
if(idx >= 0)
resources.splice(idx,1);
pump();
}
})||noop;
if(!sync) {
sync=true;
resources.push(res);
}
} catch(err) {
e(err);
}
}
if(active===0 && q.length===0 && done)
c();
}
resources.push(this._subscribe(
x=>{q.push(x);pump();},
e,
()=>{done=true;pump();}
));
return ()=>{
done=true;
active=0;
q.length=0;
resources.forEach(catchAll);
resources.length=0;
};
});
}
mergeAll() {
return this.merge(Infinity);
}
concat() {
return this.merge(1)
}
flatMap(fn) {
return this.map(fn).mergeAll();
}
concatMap(fn) {
return this.map(fn).merge(1);
}
}