From b96ea51bd8f1a759eea20e2466da96b2a8dc1c87 Mon Sep 17 00:00:00 2001 From: Rune Harlyk Date: Sat, 3 Jan 2026 17:45:31 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Makes=20IMU=20store=20hand?= =?UTF-8?q?le=20data=20subscription?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/pnpm-lock.yaml | 3 --- app/src/lib/stores/imu.ts | 28 ++++++++++++++++++----- app/src/lib/stores/socket-store.ts | 16 ------------- app/src/routes/peripherals/imu/imu.svelte | 11 +++------ 4 files changed, 25 insertions(+), 33 deletions(-) diff --git a/app/pnpm-lock.yaml b/app/pnpm-lock.yaml index 603edba..6b9250c 100644 --- a/app/pnpm-lock.yaml +++ b/app/pnpm-lock.yaml @@ -150,9 +150,6 @@ importers: vitest: 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) - ws: - specifier: ^8.18.3 - version: 8.18.3 packages: diff --git a/app/src/lib/stores/imu.ts b/app/src/lib/stores/imu.ts index 37ffc1c..2baafa9 100644 --- a/app/src/lib/stores/imu.ts +++ b/app/src/lib/stores/imu.ts @@ -1,18 +1,34 @@ import { writable } from 'svelte/store' import { IMUData } from '$lib/platform_shared/message' +import { socket } from './socket' -const imu_data: IMUData[] = [] const maxIMUData = 100 export const imu = (() => { - const { subscribe, update } = writable(imu_data) + const { subscribe, update } = writable([]) + + let unsubscribe: (() => void) | null = null + let listenerCount = 0 + + const addData = (content: IMUData) => { + update(data => [...data, content].slice(-maxIMUData)) + } return { subscribe, - addData: (content: IMUData) => { - update(imu_data => { - return [...imu_data, content].slice(-maxIMUData) - }) + addData, + listen: () => { + listenerCount++ + if (!unsubscribe) { + unsubscribe = socket.on(IMUData, addData) + } + }, + stop: () => { + listenerCount = Math.max(0, listenerCount - 1) + if (listenerCount === 0 && unsubscribe) { + unsubscribe() + unsubscribe = null + } } } })() diff --git a/app/src/lib/stores/socket-store.ts b/app/src/lib/stores/socket-store.ts index e7f4dde..7816a62 100644 --- a/app/src/lib/stores/socket-store.ts +++ b/app/src/lib/stores/socket-store.ts @@ -8,21 +8,5 @@ export const servoAngles: Writable = writable( 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 sonar = writable([0, 0]) -export const distances = writable({}) - -export interface socketDataCollection { - angles: Writable - logs: Writable - mpu: Writable - distances: Writable -} - -export const socketData = { - angles: servoAngles, - logs, - mpu, - distances -} diff --git a/app/src/routes/peripherals/imu/imu.svelte b/app/src/routes/peripherals/imu/imu.svelte index ffaf0a6..665be3b 100644 --- a/app/src/routes/peripherals/imu/imu.svelte +++ b/app/src/routes/peripherals/imu/imu.svelte @@ -9,7 +9,7 @@ import { useFeatureFlags } from '$lib/stores/featureFlags' 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) @@ -212,19 +212,14 @@ ) } } - let unsubscribeImu: (() => void) | undefined onMount(() => { - unsubscribeImu = socket.on(IMUData, data => { - console.log(data) - imu.addData(data) - }) - + imu.listen() initializeCharts() intervalId = setInterval(updateData, 200) }) onDestroy(() => { - unsubscribeImu?.() + imu.stop() clearInterval(intervalId) })