Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion js/common/web-file-transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ class FileTransferClient {

async readOnly() {
await this._checkConnection();
return !this._allowedMethods.includes('DELETE');
console.log("Checking read only");
const response = await this._fetch("/fs/", {method: "GET", headers: {"Accept": "application/json"}})
const result = await response.json();
//TODO: Tyeth: cache this value until reconnection, as listdir / connect already fetch it
return result.writable === undefined || result.writable === false || !this._allowedMethods.includes("DELETE");
}

async _checkConnection() {
if (!this.connectionStatus() && this._allowedMethods !== null) {
throw new Error("Unable to perform file operation. Not Connected.");
}

//TODO: Tyeth: reset this on reconnection
if (this._allowedMethods === null) {
const status = await this._fetch("/fs/", {method: "OPTIONS"});
this._allowedMethods = status.headers.get("Access-Control-Allow-Methods").split(/,/).map(method => {return method.trim().toUpperCase();});
Expand Down
24 changes: 20 additions & 4 deletions js/workflows/usb.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,28 @@ class USBWorkflow extends Workflow {

async onDisconnected(e, reconnect = true) {
if (this.reader) {
await this.reader.cancel();
try {
await this.reader.cancel();
} catch (error) {
console.warn("Error calling reader.cancel:", error);
}
this.reader = null;
}
if (this.writer) {
await this.writer.releaseLock();
try {
await this.writer.releaseLock();
} catch (error) {
console.warn("Error calling writer.releaseLock:", error);
}
this.writer = null;
}

if (this._serialDevice) {
await this._serialDevice.close();
try {
await this._serialDevice.close();
} catch (error) {
console.warn("Error calling _serialDevice.close:", error);
}
this._serialDevice = null;
}

Expand Down Expand Up @@ -273,7 +285,11 @@ class USBWorkflow extends Workflow {
device.addEventListener("message", this._messageCallback);

let onDisconnect = async (e) => {
await this.onDisconnected(e, false);
try {
await this.onDisconnected(e, false);
} catch (error) {
console.warn("Error calling onDisconnected (maybe already disconnected):", error);
}
};
device.removeEventListener("disconnect", onDisconnect);
device.addEventListener("disconnect", onDisconnect);
Expand Down