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
|
import { getURLAndInit } from 'm/request-helper';
const endpoint = '/traffic';
const textDecoder = new TextDecoder('utf-8', { stream: true });
const Size = 150;
const traffic = {
labels: Array(Size),
// labels: [],
up: Array(Size),
down: Array(Size),
size: Size,
subscribers: [],
appendData(o) {
this.up.push(o.up);
this.down.push(o.down);
const t = new Date();
const l = '' + t.getMinutes() + t.getSeconds();
this.labels.push(l);
if (this.up.length > this.size) this.up.shift();
if (this.down.length > this.size) this.down.shift();
if (this.labels.length > this.size) this.labels.shift();
this.subscribers.forEach(f => f(o));
},
subscribe(listener) {
const me = this;
this.subscribers.push(listener);
return function unsubscribe() {
const idx = me.subscribers.indexOf(listener);
me.subscribers.splice(idx, 1);
};
}
};
let fetched = false;
function pump(reader) {
return reader.read().then(({ done, value }) => {
if (done) {
// eslint-disable-next-line no-console
console.log('GET /traffic streaming done');
fetched = false;
return;
}
const t = textDecoder.decode(value);
// console.log('response', t);
const o = JSON.parse(t);
traffic.appendData(o);
return pump(reader);
});
}
function fetchData(apiConfig) {
if (fetched) return traffic;
const { url, init } = getURLAndInit(apiConfig);
fetch(url + endpoint, init).then(response => {
if (response.ok) {
fetched = true;
const reader = response.body.getReader();
pump(reader);
}
});
return traffic;
}
export { fetchData };
|