refactor: throughput display logic to component

This commit is contained in:
cha0s 2019-04-13 17:08:58 -05:00
parent c5d0909dd5
commit d2dddde902
3 changed files with 33 additions and 27 deletions

View File

@ -150,18 +150,6 @@ const AugmentedParser = augmentParserWithThroughput(SocketIoParser);
const socket = createClient(window.location.href, { const socket = createClient(window.location.href, {
parser: AugmentedParser, parser: AugmentedParser,
}); });
// Throughput ticker.
const throughputSampleTime = 0.5;
const throughputTicker = new Ticker(throughputSampleTime);
const {Decoder, Encoder} = AugmentedParser;
throughputTicker.on('tick', () => {
throughputTicker.emit('updateThroughput', Vector.scale([
Decoder.throughput,
Encoder.throughput,
], 1 / throughputSampleTime));
Decoder.throughput = 0;
Encoder.throughput = 0;
});
// Mouse/touch movement. // Mouse/touch movement.
const pointerMovementHandle = setInterval(() => { const pointerMovementHandle = setInterval(() => {
do { do {
@ -203,8 +191,6 @@ const predictionHandle = setInterval(() => {
state = synchronizer.state; state = synchronizer.state;
// Render timing. // Render timing.
renderTicker.tick(elapsed); renderTicker.tick(elapsed);
// Throughput ticker.
throughputTicker.tick(elapsed);
// Apply environmental lighting. // Apply environmental lighting.
let intensity = 0; let intensity = 0;
if (worldTime.hour >= 21 || worldTime.hour < 4) { if (worldTime.hour >= 21 || worldTime.hour < 4) {
@ -261,7 +247,7 @@ const renderHandle = setAnimation(render);
// UI. // UI.
const UiComponent = <Ui const UiComponent = <Ui
worldTime={worldTime} worldTime={worldTime}
throughputTicker={throughputTicker} Parser={AugmentedParser}
/>; />;
ReactDOM.render(UiComponent, stage.ui); ReactDOM.render(UiComponent, stage.ui);
// Eval. // Eval.

View File

@ -5,10 +5,10 @@ import {hot} from 'react-hot-loader/root';
import Throughput from './throughput'; import Throughput from './throughput';
import WorldTime from './world-time'; import WorldTime from './world-time';
const Ui = ({throughputTicker, worldTime}) => { const Ui = ({Parser, worldTime}) => {
return <React.Fragment> return <React.Fragment>
<WorldTime worldTime={worldTime} /> <WorldTime worldTime={worldTime} />
<Throughput ticker={throughputTicker} /> <Throughput Parser={Parser} />
</React.Fragment>; </React.Fragment>;
}; };

View File

@ -2,6 +2,7 @@
import React, {useEffect, useState} from 'react'; import React, {useEffect, useState} from 'react';
// 2nd party. // 2nd party.
import {compose} from '@avocado/core'; import {compose} from '@avocado/core';
import {Vector} from '@avocado/math';
import contempo from 'contempo'; import contempo from 'contempo';
// 1st party. // 1st party.
import {WorldTime} from '../../common/world-time'; import {WorldTime} from '../../common/world-time';
@ -20,11 +21,15 @@ const decorate = compose(
padding: 0.5em; padding: 0.5em;
font-family: monospace; font-family: monospace;
} }
.throughput .kbps:before {
color: rgb(180, 180, 180);
content: 'kb/s ';
}
.throughput .input:before { .throughput .input:before {
content: 'kbps \\a0in: '; content: '\\a0in: ';
} }
.throughput .output:before { .throughput .output:before {
content: 'kbps out: '; content: 'out: ';
} }
.throughput p { .throughput p {
margin: 0.25em; margin: 0.25em;
@ -38,21 +43,36 @@ const formatKbps = (bytes) => {
return Math.floor(kbps * 100) / 100; return Math.floor(kbps * 100) / 100;
} }
const ThroughputComponent = ({ticker}) => { const ThroughputComponent = ({Parser}) => {
const [throughput, setThroughput] = useState([0, 0]); const [throughput, setThroughput] = useState([0, 0]);
useEffect(() => { useEffect(() => {
const updater = (throughput) => { const {Decoder, Encoder} = Parser;
const throughputSampleTime = 0.5;
// Throughput ticker.
const throughputHandle = setInterval(() => {
const throughput = Vector.scale([
Decoder.throughput || 0,
Encoder.throughput || 0,
], 1 / throughputSampleTime);
setThroughput(throughput); setThroughput(throughput);
}; Decoder.throughput = 0;
ticker.on('updateThroughput', updater); Encoder.throughput = 0;
}, throughputSampleTime * 1000);
return () => { return () => {
ticker.off('updateThroughput', updater); clearInterval(throughputHandle);
}; };
}, []); }, []);
return <div className="throughput unselectable"> return <div className="throughput unselectable">
<p className="input">{formatKbps(throughput[0])}</p> <p className="unit kbps">
<p className="output">{formatKbps(throughput[1])}</p> <span className="input">
{formatKbps(throughput[0])}
</span>
</p>
<p className="unit kbps">
<span className="output">
{formatKbps(throughput[1])}
</span>
</p>
</div>; </div>;
} }