Skip to content

Commit

Permalink
Fix event name, inline type-safe constants.
Browse files Browse the repository at this point in the history
  • Loading branch information
microbit-matt-hillsdon committed Jul 8, 2024
1 parent 2c2e7cf commit d6b119d
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 60 deletions.
33 changes: 14 additions & 19 deletions src/device/device-hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -61,12 +56,12 @@ export const useConnectionStatus = () => {
const device = useDevice();
const [status, setStatus] = useState<ConnectionStatus>(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]);

Expand Down Expand Up @@ -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]);

Expand Down Expand Up @@ -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 (
Expand Down
14 changes: 3 additions & 11 deletions src/device/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/device/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
FlashDataSource,
FlashEvent,
SerialDataEvent,
StatusEvent,
ConnectionStatusEvent,
WebUSBError,
WebUSBErrorCode,
} from "./device";
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 6 additions & 3 deletions src/device/simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
FlashEvent,
SerialDataEvent,
SerialResetEvent,
StatusEvent,
ConnectionStatusEvent,
} from "./device";

// Simulator-only events.
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/device/webusb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
});

Expand Down
6 changes: 3 additions & 3 deletions src/device/webusb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
SerialErrorEvent,
SerialResetEvent,
StartUSBSelect,
StatusEvent,
ConnectionStatusEvent,
WebUSBError,
} from "./device";
import { TypedEventTarget } from "../common/events";
Expand Down Expand Up @@ -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<T>(f: () => Promise<T>): Promise<T> {
Expand Down Expand Up @@ -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;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/editor/codemirror/language-server/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
}
}
Expand Down
14 changes: 5 additions & 9 deletions src/serial/XTerm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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();
};
Expand Down
12 changes: 4 additions & 8 deletions src/workbench/connect-dialogs/Overlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand All @@ -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 (
Expand Down

0 comments on commit d6b119d

Please sign in to comment.