Skip to content

Commit bb064e1

Browse files
committedOct 2, 2024··
chore(ui): bandwidth as composables
Signed-off-by: Neko Ayaka <[email protected]>
1 parent a226778 commit bb064e1

File tree

5 files changed

+61
-65
lines changed

5 files changed

+61
-65
lines changed
 

‎app/app.vue

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ body,
2020
height: 100vh;
2121
margin: 0;
2222
padding: 0;
23+
overscroll-behavior: none;
2324
}
2425
2526
html.dark {

‎app/components/Scan.vue

+3-62
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,8 @@ const props = withDefaults(defineProps<{
1212
height: 512,
1313
})
1414
15-
const kiloBytesFormatter = new Intl.NumberFormat('en-US', {
16-
style: 'unit',
17-
unit: 'kilobyte-per-second',
18-
unitDisplay: 'short',
19-
})
20-
21-
// All Bandwidth calculation variables
22-
const bytesReceivedInLastSecond = ref(0)
23-
const currentValidBandwidth = ref(0)
24-
const currentValidBandwidthFormatted = computed(() => {
25-
return kiloBytesFormatter.format(currentValidBandwidth.value)
26-
})
27-
// Valid Bandwidth calculation variables
28-
const validBytesReceivedInLastSecond = ref(0)
29-
const currentBandwidth = ref(0)
30-
const currentBandwidthFormatted = computed(() => {
31-
return kiloBytesFormatter.format(currentBandwidth.value)
32-
})
33-
34-
let lastUpdateTime = 0
35-
let animationFrameId: number | null = null
15+
const { bytesReceived: validBytesReceivedInLastSecond, currentFormatted: currentValidBandwidthFormatted } = useBandwidth()
16+
const { bytesReceived: bytesReceivedInLastSecond, currentFormatted: currentBandwidthFormatted } = useBandwidth()
3617
3718
const { devices } = useDevicesList({
3819
requestPermissions: true,
@@ -78,16 +59,6 @@ onMounted(async () => {
7859
)
7960
})
8061
81-
onMounted(() => {
82-
animationFrameId = requestAnimationFrame(updateBandwidth)
83-
})
84-
85-
onUnmounted(() => {
86-
if (animationFrameId !== null) {
87-
cancelAnimationFrame(animationFrameId)
88-
}
89-
})
90-
9162
function disconnectCamera() {
9263
stream?.getTracks().forEach(track => track.stop())
9364
stream = undefined
@@ -112,36 +83,6 @@ async function connectCamera() {
11283
}
11384
}
11485
115-
function updateBandwidth(timestamp: number) {
116-
const now = timestamp
117-
const elapsedTime = now - lastUpdateTime
118-
119-
if (elapsedTime >= 1000) {
120-
// Calculate bandwidth for the last second
121-
currentValidBandwidth.value = Number.parseFloat(
122-
(
123-
(validBytesReceivedInLastSecond.value / 1024)
124-
/ (elapsedTime / 1000)
125-
).toFixed(2),
126-
)
127-
128-
currentBandwidth.value = Number.parseFloat(
129-
(
130-
(bytesReceivedInLastSecond.value / 1024)
131-
/ (elapsedTime / 1000)
132-
).toFixed(2),
133-
)
134-
135-
// Reset for the next second
136-
validBytesReceivedInLastSecond.value = 0
137-
bytesReceivedInLastSecond.value = 0
138-
139-
lastUpdateTime = now
140-
}
141-
142-
requestAnimationFrame(updateBandwidth)
143-
}
144-
14586
const chunks: SliceData[] = reactive([])
14687
14788
const length = computed(() => chunks.find(i => i?.[1])?.[1] || 0)
@@ -268,7 +209,7 @@ watch(() => results.value.size, (size) => {
268209
</template>
269210
</div>
270211
<p absolute right-1 top-1 border border-gray:50 rounded-md bg-black:75 px2 py1 text-sm text-white font-mono shadow>
271-
{{ shutterCount }} | {{ fps.toFixed(0) }} hz | {{ currentValidBandwidthFormatted }} (<span text-neutral-400>{{ currentBandwidthFormatted }}</span>)
212+
{{ shutterCount }} | {{ fps.toFixed(0) }} hz | {{ currentValidBandwidthFormatted }} <span text-neutral-400>({{ currentBandwidthFormatted }})</span>
272213
</p>
273214
</div>
274215
</div>

‎app/composables/bandwidth.ts

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
export function useBandwidth() {
2+
const kiloBytesFormatter = new Intl.NumberFormat('en-US', {
3+
style: 'unit',
4+
unit: 'kilobyte-per-second',
5+
unitDisplay: 'short',
6+
})
7+
8+
const bytesReceived = ref(0)
9+
const current = ref(0)
10+
const animationFrameId = ref<number | null>(null)
11+
const lastUpdateTime = ref(0)
12+
13+
const currentFormatted = computed(() => {
14+
return kiloBytesFormatter.format(current.value)
15+
})
16+
17+
function updateBandwidth(timestamp: number) {
18+
const now = timestamp
19+
const elapsedTime = now - lastUpdateTime.value
20+
21+
if (elapsedTime >= 1000) {
22+
// Calculate bandwidth for the last second
23+
current.value = Number.parseFloat(
24+
(
25+
(bytesReceived.value / 1024)
26+
/ (elapsedTime / 1000)
27+
).toFixed(2),
28+
)
29+
30+
// Reset for the next second
31+
bytesReceived.value = 0
32+
33+
lastUpdateTime.value = now
34+
}
35+
36+
requestAnimationFrame(updateBandwidth)
37+
}
38+
39+
onMounted(() => {
40+
animationFrameId.value = requestAnimationFrame(updateBandwidth)
41+
})
42+
43+
onUnmounted(() => {
44+
if (animationFrameId.value !== null) {
45+
cancelAnimationFrame(animationFrameId.value)
46+
}
47+
})
48+
49+
return {
50+
bytesReceived,
51+
current,
52+
currentFormatted,
53+
}
54+
}

‎app/layouts/default.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<main h-full>
3-
<div flex flex-col gap-4 px-4 pb-2 pt-4>
3+
<div flex flex-col gap-4 px-4 pb-4 pt-4>
44
<h1 text-4xl>
55
Qrs
66
</h1>

‎app/pages/index.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ async function onFileChange(file?: File) {
6262
</div>
6363
</div>
6464
<div v-if="readPhase === ReadPhase.Ready" h-full w-full flex justify-center>
65-
<Generate :speed="speed" :data="data" min-h="[calc(100vh-224px)]" max-w="[calc(100vh-224px)]" h-full w-full />
65+
<Generate :speed="speed" :data="data" min-h="[calc(100vh-250px)]" max-w="[calc(100vh-250px)]" h-full w-full />
6666
</div>
6767
<InputFile
6868
v-else
69-
min-h="[calc(100vh-224px)]" h-full w-full
69+
min-h="[calc(100vh-250px)]" h-full w-full
7070
text="neutral-600 dark:neutral-400"
7171
@file="onFileChange"
7272
>

0 commit comments

Comments
 (0)
Please sign in to comment.