From d6b119d3eb258f26d7aab7361939366221909b9a Mon Sep 17 00:00:00 2001 From: Matt Hillsdon Date: Mon, 8 Jul 2024 12:22:44 +0100 Subject: [PATCH] Fix event name, inline type-safe constants. --- src/device/device-hooks.tsx | 33 ++++++++----------- src/device/device.ts | 14 ++------ src/device/mock.ts | 4 +-- src/device/simulator.ts | 9 +++-- src/device/webusb.test.ts | 4 +-- src/device/webusb.ts | 6 ++-- src/editor/codemirror/language-server/view.ts | 6 ++-- src/serial/XTerm.tsx | 14 +++----- src/workbench/connect-dialogs/Overlay.tsx | 12 +++---- 9 files changed, 42 insertions(+), 60 deletions(-) diff --git a/src/device/device-hooks.tsx b/src/device/device-hooks.tsx index 4635ca136..ef6bc8907 100644 --- a/src/device/device-hooks.tsx +++ b/src/device/device-hooks.tsx @@ -16,13 +16,8 @@ import { useLogging } from "../logging/logging-hooks"; import { ConnectionStatus, DeviceConnection, - EVENT_FLASH, - EVENT_SERIAL_DATA, - EVENT_SERIAL_ERROR, - EVENT_SERIAL_RESET, - EVENT_STATUS, SerialDataEvent, - StatusEvent, + ConnectionStatusEvent, } from "./device"; import { SimulatorDeviceConnection } from "./simulator"; @@ -61,12 +56,12 @@ export const useConnectionStatus = () => { const device = useDevice(); const [status, setStatus] = useState(device.status); useEffect(() => { - const statusListener = (event: StatusEvent) => { + const statusListener = (event: ConnectionStatusEvent) => { setStatus(event.status); }; - device.addEventListener(EVENT_STATUS, statusListener); + device.addEventListener("status", statusListener); return () => { - device.removeEventListener(EVENT_STATUS, statusListener); + device.removeEventListener("status", statusListener); }; }, [device, setStatus]); @@ -205,13 +200,13 @@ export const useDeviceTraceback = () => { buffer.clear(); setRuntimeError(undefined); }; - device.addEventListener(EVENT_SERIAL_DATA, dataListener); - device.addEventListener(EVENT_SERIAL_RESET, clearListener); - device.addEventListener(EVENT_SERIAL_ERROR, clearListener); + device.addEventListener("serial_data", dataListener); + device.addEventListener("serial_reset", clearListener); + device.addEventListener("serial_error", clearListener); return () => { - device.removeEventListener(EVENT_SERIAL_ERROR, clearListener); - device.removeEventListener(EVENT_SERIAL_RESET, clearListener); - device.removeEventListener(EVENT_SERIAL_DATA, dataListener); + device.removeEventListener("serial_error", clearListener); + device.removeEventListener("serial_reset", clearListener); + device.removeEventListener("serial_data", dataListener); }; }, [device, setRuntimeError, logging]); @@ -250,13 +245,13 @@ export const DeviceContextProvider = ({ const moveToInSync = () => setSyncStatus(SyncStatus.IN_SYNC); fs.addEventListener("file_text_updated", moveToOutOfSync); fs.addEventListener("project_updated", moveToOutOfSync); - device.addEventListener(EVENT_FLASH, moveToInSync); - device.addEventListener(EVENT_STATUS, moveToOutOfSync); + device.addEventListener("flash", moveToInSync); + device.addEventListener("status", moveToOutOfSync); return () => { fs.removeEventListener("file_text_updated", moveToOutOfSync); fs.removeEventListener("project_updated", moveToOutOfSync); - device.removeEventListener(EVENT_STATUS, moveToOutOfSync); - device.removeEventListener(EVENT_FLASH, moveToInSync); + device.removeEventListener("status", moveToOutOfSync); + device.removeEventListener("flash", moveToInSync); }; }, [fs, device, setSyncStatus]); return ( diff --git a/src/device/device.ts b/src/device/device.ts index 6576e3ec4..a432501b7 100644 --- a/src/device/device.ts +++ b/src/device/device.ts @@ -96,14 +96,6 @@ export enum ConnectionAction { DISCONNECT = "DISCONNECT", } -export const EVENT_STATUS = "status"; -export const EVENT_SERIAL_DATA = "serial_data"; -export const EVENT_SERIAL_RESET = "serial_reset"; -export const EVENT_SERIAL_ERROR = "serial_error"; -export const EVENT_FLASH = "flash"; -export const EVENT_START_USB_SELECT = "start_usb_select"; -export const EVENT_END_USB_SELECT = "end_usb_select"; - export class HexGenerationError extends Error {} export interface FlashDataSource { @@ -135,7 +127,7 @@ export interface ConnectOptions { export type BoardVersion = "V1" | "V2"; -export class StatusEvent extends Event { +export class ConnectionStatusEvent extends Event { constructor(public readonly status: ConnectionStatus) { super("status"); } @@ -173,12 +165,12 @@ export class StartUSBSelect extends Event { export class EndUSBSelect extends Event { constructor() { - super("start_usb_select"); + super("end_usb_select"); } } export class DeviceConnectionEventMap { - "status": StatusEvent; + "status": ConnectionStatusEvent; "serial_data": SerialDataEvent; "serial_reset": Event; "serial_error": Event; diff --git a/src/device/mock.ts b/src/device/mock.ts index 65d94a46f..824374a21 100644 --- a/src/device/mock.ts +++ b/src/device/mock.ts @@ -12,7 +12,7 @@ import { FlashDataSource, FlashEvent, SerialDataEvent, - StatusEvent, + ConnectionStatusEvent, WebUSBError, WebUSBErrorCode, } from "./device"; @@ -102,7 +102,7 @@ export class MockDeviceConnection private setStatus(newStatus: ConnectionStatus) { this.status = newStatus; - this.dispatchTypedEvent("status", new StatusEvent(this.status)); + this.dispatchTypedEvent("status", new ConnectionStatusEvent(this.status)); } clearDevice(): void { diff --git a/src/device/simulator.ts b/src/device/simulator.ts index ed060ac88..8345da05c 100644 --- a/src/device/simulator.ts +++ b/src/device/simulator.ts @@ -14,7 +14,7 @@ import { FlashEvent, SerialDataEvent, SerialResetEvent, - StatusEvent, + ConnectionStatusEvent, } from "./device"; // Simulator-only events. @@ -196,7 +196,10 @@ export class SimulatorDeviceConnection switch (event.data.kind) { case "ready": { this.state = event.data.state; - this.dispatchTypedEvent("status", new StatusEvent(this.status)); + this.dispatchTypedEvent( + "status", + new ConnectionStatusEvent(this.status) + ); if (this.status !== ConnectionStatus.CONNECTED) { this.setStatus(ConnectionStatus.CONNECTED); } @@ -405,7 +408,7 @@ export class SimulatorDeviceConnection private setStatus(newStatus: ConnectionStatus) { this.status = newStatus; - this.dispatchTypedEvent("status", new StatusEvent(newStatus)); + this.dispatchTypedEvent("status", new ConnectionStatusEvent(newStatus)); } clearDevice(): void { diff --git a/src/device/webusb.test.ts b/src/device/webusb.test.ts index c293e4a3e..6d5374aab 100644 --- a/src/device/webusb.test.ts +++ b/src/device/webusb.test.ts @@ -9,7 +9,7 @@ * It might be we could create a custom environment that was web but * with a tweak to Buffer. */ -import { ConnectionStatus, EVENT_STATUS, StatusEvent } from "./device"; +import { ConnectionStatus, ConnectionStatusEvent } from "./device"; import { NullLogging } from "../deployment/default/logging"; import { MicrobitWebUSBConnection } from "./webusb"; import { vi } from "vitest"; @@ -59,7 +59,7 @@ describeDeviceOnly("MicrobitWebUSBConnection (WebUSB supported)", () => { it("connects and disconnects updating status and events", async () => { const events: ConnectionStatus[] = []; const connection = new MicrobitWebUSBConnection(); - connection.addEventListener(EVENT_STATUS, (event: StatusEvent) => { + connection.addEventListener("status", (event: ConnectionStatusEvent) => { events.push(event.status); }); diff --git a/src/device/webusb.ts b/src/device/webusb.ts index fa386ed45..02ebc23f8 100644 --- a/src/device/webusb.ts +++ b/src/device/webusb.ts @@ -23,7 +23,7 @@ import { SerialErrorEvent, SerialResetEvent, StartUSBSelect, - StatusEvent, + ConnectionStatusEvent, WebUSBError, } from "./device"; import { TypedEventTarget } from "../common/events"; @@ -318,7 +318,7 @@ export class MicrobitWebUSBConnection this.status = newStatus; this.visibilityReconnect = false; this.log("Device status " + newStatus); - this.dispatchTypedEvent("status", new StatusEvent(newStatus)); + this.dispatchTypedEvent("status", new ConnectionStatusEvent(newStatus)); } private async withEnrichedErrors(f: () => Promise): Promise { @@ -406,7 +406,7 @@ export class MicrobitWebUSBConnection this.device = await navigator.usb.requestDevice({ filters: [{ vendorId: 0x0d28, productId: 0x0204 }], }); - this.dispatchTypedEvent("start_usb_select", new EndUSBSelect()); + this.dispatchTypedEvent("end_usb_select", new EndUSBSelect()); return this.device; } } diff --git a/src/editor/codemirror/language-server/view.ts b/src/editor/codemirror/language-server/view.ts index 9cd74100f..35acc4fbc 100644 --- a/src/editor/codemirror/language-server/view.ts +++ b/src/editor/codemirror/language-server/view.ts @@ -17,7 +17,7 @@ import { autocompletion } from "./autocompletion"; import { BaseLanguageServerView, clientFacet, uriFacet } from "./common"; import { diagnosticsMapping } from "./diagnostics"; import { signatureHelp } from "./signatureHelp"; -import { DeviceConnection, EVENT_STATUS } from "../../../device/device"; +import { DeviceConnection } from "../../../device/device"; /** * The main extension. This synchronises the diagnostics between the client @@ -68,7 +68,7 @@ class LanguageServerView extends BaseLanguageServerView implements PluginValue { super(view); this.client.addEventListener("diagnostics", this.diagnosticsListener); - this.device.addEventListener(EVENT_STATUS, this.onDeviceStatusChanged); + this.device.addEventListener("status", this.onDeviceStatusChanged); // Is there a better way to do this? We can 't dispatch at this point. // It would be best to do this with initial state and avoid the dispatch. @@ -99,7 +99,7 @@ class LanguageServerView extends BaseLanguageServerView implements PluginValue { destroy() { this.destroyed = true; this.client.removeEventListener("diagnostics", this.diagnosticsListener); - this.device.removeEventListener(EVENT_STATUS, this.onDeviceStatusChanged); + this.device.removeEventListener("status", this.onDeviceStatusChanged); // We don't own the client/connection which might outlive us, just our notifications. } } diff --git a/src/serial/XTerm.tsx b/src/serial/XTerm.tsx index 22e30592f..0c1f61b50 100644 --- a/src/serial/XTerm.tsx +++ b/src/serial/XTerm.tsx @@ -12,11 +12,7 @@ import "xterm/css/xterm.css"; import useActionFeedback from "../common/use-action-feedback"; import useIsUnmounted from "../common/use-is-unmounted"; import { backgroundColorTerm } from "../deployment/misc"; -import { - EVENT_SERIAL_DATA, - EVENT_SERIAL_RESET, - SerialDataEvent, -} from "../device/device"; +import { SerialDataEvent } from "../device/device"; import { parseTraceLine, useDevice } from "../device/device-hooks"; import { useSelection } from "../workbench/use-selection"; import { WebLinkProvider } from "./link-provider"; @@ -110,8 +106,8 @@ const useManagedTermimal = ( terminal.reset(); } }; - device.addEventListener(EVENT_SERIAL_DATA, serialListener); - device.addEventListener(EVENT_SERIAL_RESET, resetListener); + device.addEventListener("serial_data", serialListener); + device.addEventListener("serial_reset", resetListener); terminal.onData((data: string) => { if (!isUnmounted()) { // Async for internal error handling, we don't need to wait. @@ -181,8 +177,8 @@ const useManagedTermimal = ( return () => { currentTerminalRef.current = undefined; - device.removeEventListener(EVENT_SERIAL_RESET, resetListener); - device.removeEventListener(EVENT_SERIAL_DATA, serialListener); + device.removeEventListener("serial_reset", resetListener); + device.removeEventListener("serial_data", serialListener); resizeObserver.disconnect(); terminal.dispose(); }; diff --git a/src/workbench/connect-dialogs/Overlay.tsx b/src/workbench/connect-dialogs/Overlay.tsx index 26cc83369..5d92f2e0d 100644 --- a/src/workbench/connect-dialogs/Overlay.tsx +++ b/src/workbench/connect-dialogs/Overlay.tsx @@ -6,10 +6,6 @@ import { Box, useDisclosure } from "@chakra-ui/react"; import { useCallback, useEffect } from "react"; import { zIndexOverlay } from "../../common/zIndex"; -import { - EVENT_END_USB_SELECT, - EVENT_START_USB_SELECT, -} from "../../device/device"; import { useDevice } from "../../device/device-hooks"; const Overlay = () => { @@ -22,11 +18,11 @@ const Overlay = () => { selectingDevice.onClose(); }, [selectingDevice]); useEffect(() => { - device.addEventListener(EVENT_START_USB_SELECT, showOverlay); - device.addEventListener(EVENT_END_USB_SELECT, hideOverlay); + device.addEventListener("start_usb_select", showOverlay); + device.addEventListener("end_usb_select", hideOverlay); return () => { - device.removeEventListener(EVENT_START_USB_SELECT, showOverlay); - device.removeEventListener(EVENT_END_USB_SELECT, hideOverlay); + device.removeEventListener("start_usb_select", showOverlay); + device.removeEventListener("end_usb_select", hideOverlay); }; }, [device, showOverlay, hideOverlay]); return (