Support cancel for sending operation.
This commit is contained in:
parent
0602c4c72f
commit
bb3aaf1bed
2 changed files with 90 additions and 57 deletions
|
|
@ -375,7 +375,7 @@
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import Vue from "vue";
|
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 util from "../plugins/utils";
|
||||||
import MessageOperations from "./messages/MessageOperations.vue";
|
import MessageOperations from "./messages/MessageOperations.vue";
|
||||||
import AvatarOperations from "./messages/AvatarOperations.vue";
|
import AvatarOperations from "./messages/AvatarOperations.vue";
|
||||||
|
|
@ -1078,7 +1078,11 @@ export default {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
this.currentSendError = err.toLocaleString();
|
if (err instanceof AbortError || err === "Abort") {
|
||||||
|
this.currentSendError = null;
|
||||||
|
} else {
|
||||||
|
this.currentSendError = err.LocaleString();
|
||||||
|
}
|
||||||
this.currentSendOperation = null;
|
this.currentSendOperation = null;
|
||||||
this.currentSendProgress = null;
|
this.currentSendProgress = null;
|
||||||
});
|
});
|
||||||
|
|
@ -1088,7 +1092,7 @@ export default {
|
||||||
cancelSendAttachment() {
|
cancelSendAttachment() {
|
||||||
this.$refs.attachment.value = null;
|
this.$refs.attachment.value = null;
|
||||||
if (this.currentSendOperation) {
|
if (this.currentSendOperation) {
|
||||||
this.currentSendOperation.reject("Canceled");
|
this.currentSendOperation.abort();
|
||||||
}
|
}
|
||||||
this.currentSendOperation = null;
|
this.currentSendOperation = null;
|
||||||
this.currentImageInput = null;
|
this.currentImageInput = null;
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,28 @@ var _browserCanRecordAudioF = function () {
|
||||||
}
|
}
|
||||||
var _browserCanRecordAudio = _browserCanRecordAudioF();
|
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 {
|
class Util {
|
||||||
getAttachment(matrixClient, event, progressCallback, asBlob = false) {
|
getAttachment(matrixClient, event, progressCallback, asBlob = false) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
|
@ -285,9 +307,14 @@ class Util {
|
||||||
}
|
}
|
||||||
|
|
||||||
sendImage(matrixClient, roomId, file, onUploadProgress) {
|
sendImage(matrixClient, roomId, file, onUploadProgress) {
|
||||||
return new Promise((resolve, reject) => {
|
return new AbortablePromise((resolve, reject, aborter) => {
|
||||||
|
const abortionController = aborter;
|
||||||
var reader = new FileReader();
|
var reader = new FileReader();
|
||||||
reader.onload = (e) => {
|
reader.onload = (e) => {
|
||||||
|
if (abortionController.aborted) {
|
||||||
|
reject("Aborted");
|
||||||
|
return;
|
||||||
|
}
|
||||||
const fileContents = e.target.result;
|
const fileContents = e.target.result;
|
||||||
var data = new Uint8Array(fileContents);
|
var data = new Uint8Array(fileContents);
|
||||||
|
|
||||||
|
|
@ -326,7 +353,8 @@ class Util {
|
||||||
|
|
||||||
if (!matrixClient.isRoomEncrypted(roomId)) {
|
if (!matrixClient.isRoomEncrypted(roomId)) {
|
||||||
// Not encrypted.
|
// Not encrypted.
|
||||||
matrixClient.uploadContent(data, opts)
|
abortionController.abortablePromise = matrixClient.uploadContent(data, opts);
|
||||||
|
abortionController.abortablePromise
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
messageContent.url = response.content_uri;
|
messageContent.url = response.content_uri;
|
||||||
return this.sendMessage(matrixClient, roomId, "m.room.message", messageContent)
|
return this.sendMessage(matrixClient, roomId, "m.room.message", messageContent)
|
||||||
|
|
@ -373,7 +401,8 @@ class Util {
|
||||||
// Encrypted data sent as octet-stream!
|
// Encrypted data sent as octet-stream!
|
||||||
opts.type = "application/octet-stream";
|
opts.type = "application/octet-stream";
|
||||||
|
|
||||||
matrixClient.uploadContent(data, opts)
|
abortionController.abortablePromise = matrixClient.uploadContent(data, opts);
|
||||||
|
abortionController.abortablePromise
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
if (response.error) {
|
if (response.error) {
|
||||||
return reject(response.error);
|
return reject(response.error);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue