♻️ Makes IMU store handle data subscription

This commit is contained in:
Rune Harlyk
2026-01-03 17:45:31 +01:00
committed by nikguin04
parent a31e001eb5
commit b96ea51bd8
4 changed files with 25 additions and 33 deletions
-3
View File
@@ -150,9 +150,6 @@ importers:
vitest: vitest:
specifier: ^3.2.4 specifier: ^3.2.4
version: 3.2.4(@types/node@24.7.1)(jiti@2.6.1)(jsdom@27.0.0(postcss@8.5.6))(lightningcss@1.30.2)(yaml@2.8.1) version: 3.2.4(@types/node@24.7.1)(jiti@2.6.1)(jsdom@27.0.0(postcss@8.5.6))(lightningcss@1.30.2)(yaml@2.8.1)
ws:
specifier: ^8.18.3
version: 8.18.3
packages: packages:
+22 -6
View File
@@ -1,18 +1,34 @@
import { writable } from 'svelte/store' import { writable } from 'svelte/store'
import { IMUData } from '$lib/platform_shared/message' import { IMUData } from '$lib/platform_shared/message'
import { socket } from './socket'
const imu_data: IMUData[] = []
const maxIMUData = 100 const maxIMUData = 100
export const imu = (() => { export const imu = (() => {
const { subscribe, update } = writable(imu_data) const { subscribe, update } = writable<IMUData[]>([])
let unsubscribe: (() => void) | null = null
let listenerCount = 0
const addData = (content: IMUData) => {
update(data => [...data, content].slice(-maxIMUData))
}
return { return {
subscribe, subscribe,
addData: (content: IMUData) => { addData,
update(imu_data => { listen: () => {
return [...imu_data, content].slice(-maxIMUData) listenerCount++
}) if (!unsubscribe) {
unsubscribe = socket.on(IMUData, addData)
}
},
stop: () => {
listenerCount = Math.max(0, listenerCount - 1)
if (listenerCount === 0 && unsubscribe) {
unsubscribe()
unsubscribe = null
}
} }
} }
})() })()
-16
View File
@@ -8,21 +8,5 @@ export const servoAngles: Writable<AnglesData> = writable(
AnglesData.create({ angles: [0, 45, -90, 0, 45, -90, 0, 45, -90, 0, 45, -90] }) AnglesData.create({ angles: [0, 45, -90, 0, 45, -90, 0, 45, -90, 0, 45, -90] })
) )
export const logs = writable([] as string[])
export const mpu = writable({ heading: 0 }) export const mpu = writable({ heading: 0 })
export const sonar = writable([0, 0]) export const sonar = writable([0, 0])
export const distances = writable({})
export interface socketDataCollection {
angles: Writable<AnglesData>
logs: Writable<string[]>
mpu: Writable<unknown>
distances: Writable<unknown>
}
export const socketData = {
angles: servoAngles,
logs,
mpu,
distances
}
+3 -8
View File
@@ -9,7 +9,7 @@
import { useFeatureFlags } from '$lib/stores/featureFlags' import { useFeatureFlags } from '$lib/stores/featureFlags'
import { Rotate3d } from '$lib/components/icons' import { Rotate3d } from '$lib/components/icons'
import { type IMUCalibrateData, IMUData } from '$lib/platform_shared/message' import { type IMUCalibrateData } from '$lib/platform_shared/message'
Chart.register(...registerables) Chart.register(...registerables)
@@ -212,19 +212,14 @@
) )
} }
} }
let unsubscribeImu: (() => void) | undefined
onMount(() => { onMount(() => {
unsubscribeImu = socket.on(IMUData, data => { imu.listen()
console.log(data)
imu.addData(data)
})
initializeCharts() initializeCharts()
intervalId = setInterval(updateData, 200) intervalId = setInterval(updateData, 200)
}) })
onDestroy(() => { onDestroy(() => {
unsubscribeImu?.() imu.stop()
clearInterval(intervalId) clearInterval(intervalId)
}) })