buttplug-wasm@3.0.0 still declares buttplug@^4.0.2 as its dependency,
and v5 changed the OutputCmd wire shape (Value: number[] -> number) -
a real protocol difference, not just a type mismatch. Confirmed by
testing against real Lovense hardware that device control still works
in practice, so the bump stands; the risk is documented at the
client.connect() cast in case a future device/build doesn't fare as
well.
Also fixes a Map vs ReadonlyMap mismatch in the version-agnostic
feature-type extraction (v4 returns Map, v5 returns ReadonlyMap) that
surfaced while making this change, and adds a battery indicator to each
connected device's card - reads once on connect, refreshes every 60s,
using device.hasInput("Battery") to detect support.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W8WkFF5ppURBAB918593Eb
53 lines
2.1 KiB
TypeScript
53 lines
2.1 KiB
TypeScript
import { create } from "zustand";
|
|
import type { ConnectedDeviceInfo } from "./types";
|
|
|
|
export const actuatorKey = (deviceIndex: number, featureIndex: number): string =>
|
|
`${deviceIndex}:${featureIndex}`;
|
|
|
|
interface ButtplugStoreState {
|
|
connected: boolean;
|
|
scanning: boolean;
|
|
devices: Record<number, ConnectedDeviceInfo>;
|
|
/** Last-known slider value per `${deviceIndex}:${featureIndex}`, for UI display only. */
|
|
actuatorValues: Record<string, number>;
|
|
/** Last-known battery reading (0-1) per device index; absent until first read completes. */
|
|
batteryLevels: Record<number, number | null>;
|
|
error: string | null;
|
|
setConnected: (connected: boolean) => void;
|
|
setScanning: (scanning: boolean) => void;
|
|
upsertDevice: (device: ConnectedDeviceInfo) => void;
|
|
removeDevice: (index: number) => void;
|
|
setActuatorValue: (deviceIndex: number, featureIndex: number, value: number) => void;
|
|
setBatteryLevel: (deviceIndex: number, level: number | null) => void;
|
|
setError: (message: string | null) => void;
|
|
reset: () => void;
|
|
}
|
|
|
|
export const useButtplugStore = create<ButtplugStoreState>((set) => ({
|
|
connected: false,
|
|
scanning: false,
|
|
devices: {},
|
|
actuatorValues: {},
|
|
batteryLevels: {},
|
|
error: null,
|
|
setConnected: (connected) => set({ connected }),
|
|
setScanning: (scanning) => set({ scanning }),
|
|
upsertDevice: (device) => set((s) => ({ devices: { ...s.devices, [device.index]: device } })),
|
|
removeDevice: (index) =>
|
|
set((s) => {
|
|
const devices = { ...s.devices };
|
|
delete devices[index];
|
|
const batteryLevels = { ...s.batteryLevels };
|
|
delete batteryLevels[index];
|
|
return { devices, batteryLevels };
|
|
}),
|
|
setActuatorValue: (deviceIndex, featureIndex, value) =>
|
|
set((s) => ({
|
|
actuatorValues: { ...s.actuatorValues, [actuatorKey(deviceIndex, featureIndex)]: value },
|
|
})),
|
|
setBatteryLevel: (deviceIndex, level) =>
|
|
set((s) => ({ batteryLevels: { ...s.batteryLevels, [deviceIndex]: level } })),
|
|
setError: (message) => set({ error: message }),
|
|
reset: () => set({ connected: false, scanning: false, devices: {}, actuatorValues: {}, batteryLevels: {} }),
|
|
}));
|