Skip to content

Commit e5929d2

Browse files
Passing relevant event maps into DeviceConnection
1 parent 29257d6 commit e5929d2

File tree

8 files changed

+66
-55
lines changed

8 files changed

+66
-55
lines changed

lib/bluetooth.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ export interface MicrobitWebBluetoothConnectionOptions {
3333
logging?: Logging;
3434
}
3535

36-
export interface MicrobitWebBluetoothConnection extends DeviceConnection {
36+
export interface MicrobitWebBluetoothConnection
37+
extends DeviceConnection<ServiceConnectionEventMap> {
3738
/**
3839
* Sets micro:bit name filter for device requesting.
3940
*
@@ -143,7 +144,8 @@ export interface MicrobitWebBluetoothConnection extends DeviceConnection {
143144
*/
144145
export const createWebBluetoothConnection = (
145146
options?: MicrobitWebBluetoothConnectionOptions,
146-
) => new MicrobitWebBluetoothConnectionImpl(options);
147+
): MicrobitWebBluetoothConnection =>
148+
new MicrobitWebBluetoothConnectionImpl(options);
147149

148150
/**
149151
* A Bluetooth connection to a micro:bit device.

lib/device.ts

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
*
44
* SPDX-License-Identifier: MIT
55
*/
6-
import { TypedEventTarget } from "./events.js";
7-
import { UARTDataEvent } from "./uart.js";
6+
import { TypedEventTarget, ValueIsEvent } from "./events.js";
87

98
/**
109
* Specific identified error types.
@@ -134,30 +133,6 @@ export class ConnectionStatusEvent extends Event {
134133
}
135134
}
136135

137-
export class SerialDataEvent extends Event {
138-
constructor(public readonly data: string) {
139-
super("serialdata");
140-
}
141-
}
142-
143-
export class SerialResetEvent extends Event {
144-
constructor() {
145-
super("serialreset");
146-
}
147-
}
148-
149-
export class SerialErrorEvent extends Event {
150-
constructor(public readonly error: unknown) {
151-
super("serialerror");
152-
}
153-
}
154-
155-
export class FlashEvent extends Event {
156-
constructor() {
157-
super("flash");
158-
}
159-
}
160-
161136
export class BeforeRequestDevice extends Event {
162137
constructor() {
163138
super("beforerequestdevice");
@@ -178,18 +153,13 @@ export class BackgroundErrorEvent extends Event {
178153

179154
export class DeviceConnectionEventMap {
180155
"status": ConnectionStatusEvent;
181-
"serialdata": SerialDataEvent;
182-
"serialreset": Event;
183-
"serialerror": SerialErrorEvent;
184-
"uartdata": UARTDataEvent;
185-
"flash": Event;
156+
"backgrounderror": BackgroundErrorEvent;
186157
"beforerequestdevice": Event;
187158
"afterrequestdevice": Event;
188-
"backgrounderror": BackgroundErrorEvent;
189159
}
190160

191-
export interface DeviceConnection
192-
extends TypedEventTarget<DeviceConnectionEventMap> {
161+
export interface DeviceConnection<M extends ValueIsEvent<M>>
162+
extends TypedEventTarget<DeviceConnectionEventMap & M> {
193163
status: ConnectionStatus;
194164

195165
/**

lib/events.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export type TypedEventListenerOrEventListenerObject<M, T extends keyof M> =
4040
| TypedEventListener<M, T>
4141
| TypedEventListenerObject<M, T>;
4242

43-
type ValueIsEvent<T> = {
43+
export type ValueIsEvent<T> = {
4444
[key in keyof T]: Event;
4545
};
4646

lib/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,20 @@ import {
1919
DeviceErrorCode,
2020
FlashDataError,
2121
FlashDataSource,
22-
FlashEvent,
2322
FlashOptions,
24-
SerialDataEvent,
25-
SerialErrorEvent,
26-
SerialResetEvent,
2723
} from "./device.js";
2824
import { TypedEventTarget } from "./events.js";
2925
import { createUniversalHexFlashDataSource } from "./hex-flash-data-source.js";
3026
import { LedMatrix } from "./led.js";
3127
import { Logging, LoggingEvent } from "./logging.js";
3228
import { MagnetometerData, MagnetometerDataEvent } from "./magnetometer.js";
29+
import {
30+
FlashEvent,
31+
SerialConnectionEventMap,
32+
SerialDataEvent,
33+
SerialErrorEvent,
34+
SerialResetEvent,
35+
} from "./serial-events.js";
3336
import { ServiceConnectionEventMap } from "./service-events.js";
3437
import { UARTDataEvent } from "./uart.js";
3538
import {
@@ -58,6 +61,7 @@ export {
5861
DeviceError,
5962
FlashDataError,
6063
FlashEvent,
64+
SerialConnectionEventMap,
6165
SerialDataEvent,
6266
SerialErrorEvent,
6367
SerialResetEvent,

lib/serial-events.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
export class SerialDataEvent extends Event {
2+
constructor(public readonly data: string) {
3+
super("serialdata");
4+
}
5+
}
6+
7+
export class SerialResetEvent extends Event {
8+
constructor() {
9+
super("serialreset");
10+
}
11+
}
12+
13+
export class SerialErrorEvent extends Event {
14+
constructor(public readonly error: unknown) {
15+
super("serialerror");
16+
}
17+
}
18+
19+
export class FlashEvent extends Event {
20+
constructor() {
21+
super("flash");
22+
}
23+
}
24+
25+
export class SerialConnectionEventMap {
26+
"serialdata": SerialDataEvent;
27+
"serialreset": SerialResetEvent;
28+
"serialerror": SerialErrorEvent;
29+
"flash": FlashEvent;
30+
}

lib/usb-radio-bridge.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@ import {
1212
ConnectionStatusEvent,
1313
DeviceConnection,
1414
DeviceConnectionEventMap,
15-
SerialDataEvent,
16-
SerialErrorEvent,
1715
} from "./device.js";
1816
import { TypedEventTarget } from "./events.js";
1917
import { Logging, NullLogging } from "./logging.js";
18+
import { SerialDataEvent, SerialErrorEvent } from "./serial-events.js";
2019
import {
2120
ServiceConnectionEventMap,
2221
TypedServiceEventDispatcher,
@@ -41,7 +40,8 @@ interface ConnectCallbacks {
4140
onSuccess: () => void;
4241
}
4342

44-
export interface MicrobitRadioBridgeConnection extends DeviceConnection {
43+
export interface MicrobitRadioBridgeConnection
44+
extends DeviceConnection<ServiceConnectionEventMap> {
4545
/**
4646
* Sets remote device.
4747
*
@@ -56,7 +56,8 @@ export interface MicrobitRadioBridgeConnection extends DeviceConnection {
5656
export const createRadioBridgeConnection = (
5757
delegate: MicrobitWebUSBConnection,
5858
options?: MicrobitRadioBridgeConnectionOptions,
59-
) => new MicrobitRadioBridgeConnectionImpl(delegate, options);
59+
): MicrobitRadioBridgeConnection =>
60+
new MicrobitRadioBridgeConnectionImpl(delegate, options);
6061

6162
/**
6263
* Wraps around a USB connection to implement a subset of services over a serial protocol.

lib/usb.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,18 @@ import {
1515
DeviceError,
1616
FlashDataError,
1717
FlashDataSource,
18-
FlashEvent,
1918
FlashOptions,
20-
SerialDataEvent,
21-
SerialErrorEvent,
22-
SerialResetEvent,
2319
} from "./device.js";
2420
import { TypedEventTarget } from "./events.js";
2521
import { Logging, NullLogging } from "./logging.js";
2622
import { PromiseQueue } from "./promise-queue.js";
23+
import {
24+
FlashEvent,
25+
SerialConnectionEventMap,
26+
SerialDataEvent,
27+
SerialErrorEvent,
28+
SerialResetEvent,
29+
} from "./serial-events.js";
2730
import { DAPWrapper } from "./usb-device-wrapper.js";
2831
import { PartialFlashing } from "./usb-partial-flashing.js";
2932

@@ -41,7 +44,8 @@ export interface MicrobitWebUSBConnectionOptions {
4144
logging: Logging;
4245
}
4346

44-
export interface MicrobitWebUSBConnection extends DeviceConnection {
47+
export interface MicrobitWebUSBConnection
48+
extends DeviceConnection<SerialConnectionEventMap> {
4549
/**
4650
* Gets micro:bit deviceId.
4751
*
@@ -80,13 +84,13 @@ export interface MicrobitWebUSBConnection extends DeviceConnection {
8084
*/
8185
export const createWebUSBConnection = (
8286
options?: MicrobitWebUSBConnectionOptions,
83-
) => new MicrobitWebUSBConnectionImpl(options);
87+
): MicrobitWebUSBConnection => new MicrobitWebUSBConnectionImpl(options);
8488

8589
/**
8690
* A WebUSB connection to a micro:bit device.
8791
*/
8892
class MicrobitWebUSBConnectionImpl
89-
extends TypedEventTarget<DeviceConnectionEventMap>
93+
extends TypedEventTarget<DeviceConnectionEventMap & SerialConnectionEventMap>
9094
implements MicrobitWebUSBConnection
9195
{
9296
status: ConnectionStatus =
@@ -477,7 +481,7 @@ class MicrobitWebUSBConnectionImpl
477481
}
478482

479483
protected eventActivated(type: string): void {
480-
switch (type as keyof DeviceConnectionEventMap) {
484+
switch (type as keyof SerialConnectionEventMap) {
481485
case "serialdata": {
482486
this.startSerialInternal();
483487
break;
@@ -486,7 +490,7 @@ class MicrobitWebUSBConnectionImpl
486490
}
487491

488492
protected async eventDeactivated(type: string) {
489-
switch (type as keyof DeviceConnectionEventMap) {
493+
switch (type as keyof SerialConnectionEventMap) {
490494
case "serialdata": {
491495
this.stopSerialInternal();
492496
break;

src/demo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import {
1414
BackgroundErrorEvent,
1515
ConnectionStatus,
1616
ConnectionStatusEvent,
17-
SerialDataEvent,
1817
} from "../lib/device";
1918
import { createUniversalHexFlashDataSource } from "../lib/hex-flash-data-source";
2019
import { MagnetometerDataEvent } from "../lib/magnetometer";
20+
import { SerialDataEvent } from "../lib/serial-events";
2121
import { UARTDataEvent } from "../lib/uart";
2222
import { createWebUSBConnection, MicrobitWebUSBConnection } from "../lib/usb";
2323
import {

0 commit comments

Comments
 (0)