Abort file upload if ChatInput component is destroyed

This commit is contained in:
Pavel Djundik 2019-03-03 20:10:35 +02:00
parent f84e4199e9
commit 12cf5ed070
2 changed files with 20 additions and 1 deletions

View File

@ -153,6 +153,7 @@ export default {
}, },
destroyed() { destroyed() {
require("../js/autocompletion").disable(); require("../js/autocompletion").disable();
upload.abort();
}, },
methods: { methods: {
setPendingMessage(e) { setPendingMessage(e) {

View File

@ -5,6 +5,7 @@ const updateCursor = require("undate").update;
class Uploader { class Uploader {
init() { init() {
this.xhr = null;
this.fileQueue = []; this.fileQueue = [];
this.overlay = document.getElementById("upload-overlay"); this.overlay = document.getElementById("upload-overlay");
this.uploadInput = document.getElementById("upload-input"); this.uploadInput = document.getElementById("upload-input");
@ -106,6 +107,7 @@ class Uploader {
uploadSingleFile(file, token) { uploadSingleFile(file, token) {
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
this.xhr = xhr;
xhr.upload.addEventListener("progress", (e) => { xhr.upload.addEventListener("progress", (e) => {
const percent = Math.floor(e.loaded / e.total * 1000) / 10; const percent = Math.floor(e.loaded / e.total * 1000) / 10;
@ -114,6 +116,8 @@ class Uploader {
xhr.onreadystatechange = () => { xhr.onreadystatechange = () => {
if (xhr.readyState === XMLHttpRequest.DONE) { if (xhr.readyState === XMLHttpRequest.DONE) {
this.xhr = null;
let response; let response;
try { try {
@ -177,6 +181,16 @@ class Uploader {
// Set the cursor after the link and a space // Set the cursor after the link and a space
textbox.selectionStart = textbox.selectionEnd = textBeforeTail.length; textbox.selectionStart = textbox.selectionEnd = textBeforeTail.length;
} }
// TODO: This is a temporary hack while Vue porting is finalized
abort() {
this.fileQueue = [];
if (this.xhr) {
this.xhr.abort();
this.xhr = null;
}
}
} }
const instance = new Uploader(); const instance = new Uploader();
@ -194,4 +208,8 @@ function setMaxFileSize(kb) {
instance.maxFileSize = kb; instance.maxFileSize = kb;
} }
module.exports = {initialize, setMaxFileSize}; module.exports = {
abort: () => instance.abort(),
initialize,
setMaxFileSize,
};