Skip to content

Commit 84d3758

Browse files
committed
Fix: problems with large fluctuation (#19)
1 parent bdc991a commit 84d3758

File tree

4 files changed

+58
-26
lines changed

4 files changed

+58
-26
lines changed

web/src/routes/+page.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { error, type Load } from '@sveltejs/kit';
22
import { get } from 'svelte/store';
33
import { API_URL } from '../stores';
4+
import type { InstanceProperties } from '../types/instanceProperties';
45

56
export const load: Load = async ({ fetch, url, params }) => {
67
const resp = await fetch(`${get(API_URL)}/instance`);

web/src/routes/t/[timerId]/+page.svelte

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,69 @@
88
import { API_WS_URL } from '../../../stores';
99
import NoSleep from 'nosleep.js';
1010
import { goto } from '$app/navigation';
11-
import type { Segment } from '../../../types/segment';
11+
import { onDestroy, onMount } from 'svelte';
1212
1313
export let data: PageData;
1414
1515
const noSleep = new NoSleep();
1616
17+
let currentOffset: number | undefined;
18+
let currentFluctuation: number | undefined;
19+
let latestOffsets: number[] = [];
20+
let latestFluctuations: number[] = [];
21+
1722
let soundEnabled: boolean;
18-
let timeOffset: number | undefined;
1923
let timerData: TimerType | undefined;
2024
let lastGetTimeSent = 0;
21-
let latestOffsets: number[] = [];
2225
let socket: WebSocket | undefined;
2326
24-
const isOffsetInMargin = (offset: number) => {
25-
if (!timeOffset) {
26-
return true;
27+
const pushValueAndCaluclateAverage = (values: number[], newValue: number) => {
28+
values.push(newValue);
29+
if (values.length > 10) {
30+
values.shift();
2731
}
2832
29-
let margin = timeOffset * 0.3;
30-
return offset > timeOffset - margin && offset < timeOffset + margin;
33+
let sum = 0;
34+
for (let i = 0; i < values.length; i++) {
35+
sum += values[i];
36+
}
37+
38+
return sum / values.length;
3139
};
3240
33-
const handleNewOffset = (offset: number) => {
34-
// check if we are in a 30% margin
35-
if (!isOffsetInMargin(offset)) {
36-
return;
41+
const isOffsetInMargin = (newOffset: number) => {
42+
if (!currentOffset) {
43+
return true;
44+
}
45+
46+
const fluctuation = Math.abs(currentOffset - newOffset);
47+
console.log(fluctuation, currentFluctuation);
48+
49+
if (
50+
currentFluctuation &&
51+
latestFluctuations.length > 5 &&
52+
fluctuation > currentFluctuation * 4
53+
) {
54+
return false;
3755
}
3856
39-
latestOffsets.push(offset);
40-
if (latestOffsets.length > 10) {
41-
latestOffsets.shift();
57+
currentFluctuation = pushValueAndCaluclateAverage(latestFluctuations, fluctuation);
58+
59+
if (latestFluctuations.length < 10) {
60+
return true;
4261
}
4362
44-
let sum = 0;
45-
for (let i = 0; i < latestOffsets.length; i++) {
46-
sum += latestOffsets[i];
63+
return fluctuation < currentFluctuation * 2;
64+
};
65+
66+
const handleNewOffset = (newOffset: number) => {
67+
// check if we are in a 30% margin
68+
if (!isOffsetInMargin(newOffset)) {
69+
console.log('not in margin');
70+
return;
4771
}
4872
49-
timeOffset = sum / latestOffsets.length;
73+
currentOffset = pushValueAndCaluclateAverage(latestOffsets, newOffset);
5074
};
5175
5276
const enableSound = () => {
@@ -88,8 +112,8 @@
88112
case 'Timestamp':
89113
const now = performance.now();
90114
const getTimeRoundTrip = now - lastGetTimeSent;
91-
timeOffset = data.data + getTimeRoundTrip / 2 - now;
92-
handleNewOffset(timeOffset);
115+
let newOffset = data.data + getTimeRoundTrip / 2 - now;
116+
handleNewOffset(newOffset);
93117
break;
94118
case 'Error':
95119
throwError(data.data[0], data.data[1]);
@@ -134,6 +158,10 @@
134158
}
135159
}
136160
161+
onDestroy(() => {
162+
socket?.close();
163+
});
164+
137165
$: {
138166
if (timerData && !soundEnabled && get(modalStore).length == 0) {
139167
const d: ModalSettings = {
@@ -158,12 +186,15 @@
158186
}
159187
</script>
160188

161-
{#if timerData && timeOffset}
189+
{#if timerData && currentOffset}
162190
<Timer
163191
{timerData}
164192
{soundEnabled}
165-
{timeOffset}
166-
displayOptionsOverride={{ clock: data.url.searchParams.get('clock') != 'false' }}
193+
timeOffset={currentOffset}
194+
displayOptionsOverride={{
195+
...timerData.display_options,
196+
clock: data.url.searchParams.get('clock') != 'false'
197+
}}
167198
/>
168199
{:else}
169200
<div

web/src/routes/t/[timerId]/Timer.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
currentSegment = calculateCurrentSegment();
9696
const { timerText, label, color, seconds } = currentSegment;
9797
98-
if (timerSpan.innerText !== timerText) {
98+
if (timerSpan !== null && timerSpan.innerText !== timerText) {
9999
timerSpan.innerText = timerText;
100100
labelSpan.innerText = label;
101101
backgroundDiv.style.setProperty(

web/src/stores.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { browser, dev } from '$app/environment';
22
import { readable } from 'svelte/store';
33

4-
const API_HOST = dev ? 'localhost:3000' : browser ? window.location.host : '';
4+
const API_HOST = 'timer.itsblue.de'; //dev ? 'localhost:3000' : browser ? window.location.host : '';
55
const API_SECURE = dev ? false : browser ? window.location.protocol === 'https:' : false;
66
export const API_URL = readable(`${API_SECURE ? 'https' : 'http'}://${API_HOST}/api`);
77
export const API_WS_URL = readable(`${API_SECURE ? 'wss' : 'ws'}://${API_HOST}/api/ws`);

0 commit comments

Comments
 (0)