Skip to content

Commit a55e140

Browse files
committed
tdev(webserial): add onConnectionStateChange trigger for subscriber
1 parent 7e10db1 commit a55e140

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

packages/tdev/webserial/models/SerialDevice.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,19 @@ const DEFAULT_CONFIG: Config = {
3535

3636
export interface iSubscriber {
3737
id: string;
38+
/**
39+
* called when a complete line (ending with a newline) is received from the serial device.
40+
*/
3841
onNewLines: (newLines: string[]) => void;
42+
/**
43+
* called when the reset trigger is received or when the replay is reset/started.
44+
* Subscribers should clear any state that depends on the received lines when this is called.
45+
*/
3946
reset: () => void;
47+
/**
48+
* called when the connection state changes (e.g. from 'connected' to 'disconnected').
49+
*/
50+
onConnectionStateChange?: (state: ConnectionState) => void;
4051
}
4152

4253
export default class SerialDevice {
@@ -196,6 +207,9 @@ export default class SerialDevice {
196207
@action
197208
setConnectionState(state: ConnectionState) {
198209
this.connectionState = state;
210+
for (const subscriber of this.subscriptions.values()) {
211+
subscriber.onConnectionStateChange?.(state);
212+
}
199213
}
200214

201215
@action

0 commit comments

Comments
 (0)