Transport disconnect method (#753)
* Prevent reads from piling up * Implement disconnect() method for all transports
This commit is contained in:
@@ -6,6 +6,8 @@ export class TransportHTTP implements Types.Transport {
|
||||
private url: string;
|
||||
private receiveBatchRequests: boolean;
|
||||
private fetchInterval: number;
|
||||
private fetching: boolean;
|
||||
private interval: number | undefined;
|
||||
|
||||
public static async create(
|
||||
address: string,
|
||||
@@ -23,6 +25,7 @@ export class TransportHTTP implements Types.Transport {
|
||||
this.url = url;
|
||||
this.receiveBatchRequests = false;
|
||||
this.fetchInterval = 3000;
|
||||
this.fetching = false;
|
||||
|
||||
this._toDevice = new WritableStream<Uint8Array>({
|
||||
write: async (chunk) => {
|
||||
@@ -38,8 +41,18 @@ export class TransportHTTP implements Types.Transport {
|
||||
},
|
||||
});
|
||||
|
||||
setInterval(async () => {
|
||||
await this.readFromRadio(controller);
|
||||
this.interval = setInterval(async () => {
|
||||
if (this.fetching) {
|
||||
// We still have the previous request open
|
||||
return;
|
||||
}
|
||||
this.fetching = true;
|
||||
try {
|
||||
await this.readFromRadio(controller);
|
||||
} catch (e) {
|
||||
// TODO: Emit disconnection events for certain types of errors
|
||||
}
|
||||
this.fetching = false;
|
||||
}, this.fetchInterval);
|
||||
}
|
||||
|
||||
@@ -88,4 +101,13 @@ export class TransportHTTP implements Types.Transport {
|
||||
get fromDevice(): ReadableStream<Types.DeviceOutput> {
|
||||
return this._fromDevice;
|
||||
}
|
||||
|
||||
disconnect() : Promise<void> {
|
||||
this.fetching = false;
|
||||
if (this.interval) {
|
||||
clearInterval(this.interval);
|
||||
}
|
||||
this.interval = undefined;
|
||||
return Promise.resolve();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user