summaryrefslogtreecommitdiff
path: root/src/api/traffic.js
diff options
context:
space:
mode:
authorHaishan <[email protected]>2020-10-31 18:18:04 +0800
committerHaishan <[email protected]>2020-11-01 17:42:52 +0800
commitff1a39d04e53b428e34d46c55ecd6689189b5443 (patch)
tree94a60abe3d28a1d729b877356bdd38d75ce655a5 /src/api/traffic.js
parente62c9165481ef12ee2310dee1c32f890b3fe4b78 (diff)
chore: run ts-migrate
Diffstat (limited to 'src/api/traffic.js')
-rw-r--r--src/api/traffic.js117
1 files changed, 0 insertions, 117 deletions
diff --git a/src/api/traffic.js b/src/api/traffic.js
deleted file mode 100644
index e50ec5e..0000000
--- a/src/api/traffic.js
+++ /dev/null
@@ -1,117 +0,0 @@
-import { buildWebSocketURL, getURLAndInit } from '../misc/request-helper';
-const endpoint = '/traffic';
-const textDecoder = new TextDecoder('utf-8');
-
-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) {
- this.subscribers.push(listener);
- return () => {
- const idx = this.subscribers.indexOf(listener);
- this.subscribers.splice(idx, 1);
- };
- },
-};
-
-let fetched = false;
-let decoded = '';
-
-function parseAndAppend(x) {
- traffic.appendData(JSON.parse(x));
-}
-
-function pump(reader) {
- return reader.read().then(({ done, value }) => {
- const str = textDecoder.decode(value, { stream: !done });
- decoded += str;
-
- const splits = decoded.split('\n');
-
- const lastSplit = splits[splits.length - 1];
-
- for (let i = 0; i < splits.length - 1; i++) {
- parseAndAppend(splits[i]);
- }
-
- if (done) {
- parseAndAppend(lastSplit);
- decoded = '';
-
- // eslint-disable-next-line no-console
- console.log('GET /traffic streaming done');
- fetched = false;
- return;
- } else {
- decoded = lastSplit;
- }
- return pump(reader);
- });
-}
-
-// 1 OPEN
-// other value CLOSED
-// similar to ws readyState but not the same
-// https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/readyState
-let wsState;
-function fetchData(apiConfig) {
- if (fetched || wsState === 1) return traffic;
- wsState = 1;
- const url = buildWebSocketURL(apiConfig, endpoint);
- const ws = new WebSocket(url);
- ws.addEventListener('error', function (_ev) {
- wsState = 3;
- });
- ws.addEventListener('close', function (_ev) {
- wsState = 3;
- fetchDataWithFetch(apiConfig);
- });
- ws.addEventListener('message', function (event) {
- parseAndAppend(event.data);
- });
- return traffic;
-}
-
-function fetchDataWithFetch(apiConfig) {
- if (fetched) return traffic;
- fetched = true;
- const { url, init } = getURLAndInit(apiConfig);
- fetch(url + endpoint, init).then(
- (response) => {
- if (response.ok) {
- const reader = response.body.getReader();
- pump(reader);
- } else {
- fetched = false;
- }
- },
- (err) => {
- // eslint-disable-next-line no-console
- console.log('fetch /traffic error', err);
- fetched = false;
- }
- );
- return traffic;
-}
-
-export { fetchData };