66import {
77 BoardVersion ,
88 ConnectionStatus ,
9- DeviceConnectionEventMap ,
10- TypedEventTarget ,
9+ ConnectionStatusChange ,
1110} from "@microbit/microbit-connection" ;
12- import { SerialConnectionEventMap } from "@microbit/microbit-connection/usb" ;
1311import { MicrobitUSBConnection } from "@microbit/microbit-connection/usb" ;
12+ import { SimpleEventTarget } from "./simple-event-target" ;
1413import { Logging } from "../logging/logging" ;
1514
1615// Simulator-only events.
1716
18- export interface LogDataEvent {
17+ export interface LogData {
1918 log : DataLog ;
2019}
2120
22- export interface RadioDataEvent {
21+ export interface RadioData {
2322 text : string ;
2423}
2524
26- export interface StateChangeEvent {
25+ export interface StateChange {
2726 state : SimulatorState ;
2827}
2928
@@ -136,12 +135,19 @@ const initialDataLog = (): DataLog => ({
136135 data : [ ] ,
137136} ) ;
138137
139- interface SimulatorEventMap extends DeviceConnectionEventMap {
140- log_data : LogDataEvent ;
141- radio_data : RadioDataEvent ;
142- radio_reset : void ;
143- state_change : StateChangeEvent ;
144- request_flash : void ;
138+ interface SimulatorEventMap {
139+ status : ConnectionStatusChange ;
140+ beforerequestdevice : void ;
141+ afterrequestdevice : void ;
142+ flash : void ;
143+ serialdata : { data : string } ;
144+ serialreset : void ;
145+ // Simulator-specific
146+ logdata : LogData ;
147+ radiodata : RadioData ;
148+ radioreset : void ;
149+ statechange : StateChange ;
150+ requestflash : void ;
145151}
146152
147153/**
@@ -150,9 +156,10 @@ interface SimulatorEventMap extends DeviceConnectionEventMap {
150156 * This communicates with the iframe that is used to embed the simulator.
151157 */
152158export class SimulatorDeviceConnection
153- extends TypedEventTarget < SimulatorEventMap & SerialConnectionEventMap >
159+ extends SimpleEventTarget < SimulatorEventMap >
154160 implements MicrobitUSBConnection
155161{
162+ readonly type = "usb" as const ;
156163 status : ConnectionStatus = ConnectionStatus . NoAuthorizedDevice ;
157164 state : SimulatorState | undefined ;
158165
@@ -168,26 +175,26 @@ export class SimulatorDeviceConnection
168175 case "ready" : {
169176 const newState = event . data . state ;
170177 this . state = newState ;
171- this . dispatchEvent ( "state_change " , { state : newState } ) ;
178+ this . dispatchEvent ( "statechange " , { state : newState } ) ;
172179 if ( this . status !== ConnectionStatus . Connected ) {
173180 this . setStatus ( ConnectionStatus . Connected ) ;
174181 }
175182 break ;
176183 }
177- case "request_flash " : {
178- this . dispatchEvent ( "request_flash " ) ;
184+ case "requestflash " : {
185+ this . dispatchEvent ( "requestflash " ) ;
179186 this . logging . event ( {
180187 type : "sim-user-start" ,
181188 } ) ;
182189 break ;
183190 }
184- case "state_change " : {
191+ case "statechange " : {
185192 const updated = {
186193 ...this . state ,
187194 ...event . data . change ,
188195 } ;
189196 this . state = updated ;
190- this . dispatchEvent ( "state_change " , { state : updated } ) ;
197+ this . dispatchEvent ( "statechange " , { state : updated } ) ;
191198 break ;
192199 }
193200 case "radio_output" : {
@@ -200,7 +207,7 @@ export class SimulatorDeviceConnection
200207 // eslint-disable-next-line no-control-regex
201208 . replace ( / ^ \x01 \x00 \x01 / , "" ) ;
202209 if ( message instanceof Uint8Array ) {
203- this . dispatchEvent ( "radio_data " , { text } ) ;
210+ this . dispatchEvent ( "radiodata " , { text } ) ;
204211 }
205212 break ;
206213 }
@@ -218,12 +225,12 @@ export class SimulatorDeviceConnection
218225 result . data . push ( { data : entry . data } ) ;
219226 }
220227 this . log = result ;
221- this . dispatchEvent ( "log_data " , { log : this . log } ) ;
228+ this . dispatchEvent ( "logdata " , { log : this . log } ) ;
222229 break ;
223230 }
224231 case "log_delete" : {
225232 this . log = initialDataLog ( ) ;
226- this . dispatchEvent ( "log_data " , { log : this . log } ) ;
233+ this . dispatchEvent ( "logdata " , { log : this . log } ) ;
227234 break ;
228235 }
229236 case "serial_output" : {
@@ -300,7 +307,7 @@ export class SimulatorDeviceConnection
300307 private notifyResetComms ( ) {
301308 // Might be nice to rework so this was all about connection state changes.
302309 this . dispatchEvent ( "serialreset" ) ;
303- this . dispatchEvent ( "radio_reset " ) ;
310+ this . dispatchEvent ( "radioreset " ) ;
304311 }
305312
306313 async disconnect ( ) : Promise < void > {
@@ -340,7 +347,7 @@ export class SimulatorDeviceConnection
340347 value,
341348 } ,
342349 } ;
343- this . dispatchEvent ( "state_change " , { state : this . state } ) ;
350+ this . dispatchEvent ( "statechange " , { state : this . state } ) ;
344351 this . postMessage ( "set_value" , {
345352 id,
346353 value,
0 commit comments