Support cancel for sending operation.

This commit is contained in:
N-Pex 2023-01-09 21:12:17 +01:00
parent 0602c4c72f
commit bb3aaf1bed
2 changed files with 90 additions and 57 deletions

View file

@ -375,7 +375,7 @@
<script>
import Vue from "vue";
import { TimelineWindow, EventTimeline } from "matrix-js-sdk";
import { TimelineWindow, EventTimeline, AbortError } from "matrix-js-sdk";
import util from "../plugins/utils";
import MessageOperations from "./messages/MessageOperations.vue";
import AvatarOperations from "./messages/AvatarOperations.vue";
@ -1078,7 +1078,11 @@ export default {
}
})
.catch((err) => {
this.currentSendError = err.toLocaleString();
if (err instanceof AbortError || err === "Abort") {
this.currentSendError = null;
} else {
this.currentSendError = err.LocaleString();
}
this.currentSendOperation = null;
this.currentSendProgress = null;
});
@ -1088,7 +1092,7 @@ export default {
cancelSendAttachment() {
this.$refs.attachment.value = null;
if (this.currentSendOperation) {
this.currentSendOperation.reject("Canceled");
this.currentSendOperation.abort();
}
this.currentSendOperation = null;
this.currentImageInput = null;

View file

@ -26,6 +26,28 @@ var _browserCanRecordAudioF = function () {
}
var _browserCanRecordAudio = _browserCanRecordAudioF();
class AbortablePromise extends Promise {
constructor(executor) {
const aborter = {
aborted: false,
abortablePromise: undefined,
}
const normalExecutor = function (resolve, reject) {
executor(resolve, reject, aborter);
};
super(normalExecutor);
this.abort = () => {
aborter.aborted = true;
if (aborter.abortablePromise) {
aborter.abortablePromise.abort();
aborter.abortablePromise = undefined;
}
};
}
}
class Util {
getAttachment(matrixClient, event, progressCallback, asBlob = false) {
return new Promise((resolve, reject) => {
@ -285,9 +307,14 @@ class Util {
}
sendImage(matrixClient, roomId, file, onUploadProgress) {
return new Promise((resolve, reject) => {
return new AbortablePromise((resolve, reject, aborter) => {
const abortionController = aborter;
var reader = new FileReader();
reader.onload = (e) => {
if (abortionController.aborted) {
reject("Aborted");
return;
}
const fileContents = e.target.result;
var data = new Uint8Array(fileContents);
@ -326,7 +353,8 @@ class Util {
if (!matrixClient.isRoomEncrypted(roomId)) {
// Not encrypted.
matrixClient.uploadContent(data, opts)
abortionController.abortablePromise = matrixClient.uploadContent(data, opts);
abortionController.abortablePromise
.then((response) => {
messageContent.url = response.content_uri;
return this.sendMessage(matrixClient, roomId, "m.room.message", messageContent)
@ -373,7 +401,8 @@ class Util {
// Encrypted data sent as octet-stream!
opts.type = "application/octet-stream";
matrixClient.uploadContent(data, opts)
abortionController.abortablePromise = matrixClient.uploadContent(data, opts);
abortionController.abortablePromise
.then((response) => {
if (response.error) {
return reject(response.error);