FIX: Upload events happened outside of the runloop

This could cause test failures when checking for the result of the
events if Ember hasn't applied them yet.
This commit is contained in:
Robin Ward 2020-11-20 14:21:26 -05:00
parent 8aa912c885
commit 66ecefba52
1 changed files with 48 additions and 38 deletions

View File

@ -1,6 +1,6 @@
import getURL from "discourse-common/lib/get-url"; import getURL from "discourse-common/lib/get-url";
import I18n from "I18n"; import I18n from "I18n";
import { debounce, later, next, schedule, throttle } from "@ember/runloop"; import { run, debounce, later, next, schedule, throttle } from "@ember/runloop";
import Component from "@ember/component"; import Component from "@ember/component";
import userSearch from "discourse/lib/user-search"; import userSearch from "discourse/lib/user-search";
import discourseComputed, { import discourseComputed, {
@ -693,63 +693,73 @@ export default Component.extend({
const isUploading = validateUploadedFiles(data.files, opts); const isUploading = validateUploadedFiles(data.files, opts);
this.setProperties({ uploadProgress: 0, isUploading }); run(() => {
this.setProperties({ uploadProgress: 0, isUploading });
});
return isUploading; return isUploading;
}); });
$element.on("fileuploadprogressall", (e, data) => { $element.on("fileuploadprogressall", (e, data) => {
this.set( run(() => {
"uploadProgress", this.set(
parseInt((data.loaded / data.total) * 100, 10) "uploadProgress",
); parseInt((data.loaded / data.total) * 100, 10)
);
});
}); });
$element.on("fileuploadsend", (e, data) => { $element.on("fileuploadsend", (e, data) => {
this._pasted = false; run(() => {
this._validUploads++; this._pasted = false;
this._validUploads++;
this._setUploadPlaceholderSend(data); this._setUploadPlaceholderSend(data);
this.appEvents.trigger("composer:insert-text", this.uploadPlaceholder); this.appEvents.trigger("composer:insert-text", this.uploadPlaceholder);
if (data.xhr && data.originalFiles.length === 1) { if (data.xhr && data.originalFiles.length === 1) {
this.set("isCancellable", true); this.set("isCancellable", true);
this._xhr = data.xhr(); this._xhr = data.xhr();
} }
});
}); });
$element.on("fileuploaddone", (e, data) => { $element.on("fileuploaddone", (e, data) => {
let upload = data.result; run(() => {
this._setUploadPlaceholderDone(data); let upload = data.result;
if (!this._xhr || !this._xhr._userCancelled) { this._setUploadPlaceholderDone(data);
const markdown = uploadMarkdownResolvers.reduce( if (!this._xhr || !this._xhr._userCancelled) {
(md, resolver) => resolver(upload) || md, const markdown = uploadMarkdownResolvers.reduce(
getUploadMarkdown(upload) (md, resolver) => resolver(upload) || md,
); getUploadMarkdown(upload)
);
cacheShortUploadUrl(upload.short_url, upload); cacheShortUploadUrl(upload.short_url, upload);
this.appEvents.trigger( this.appEvents.trigger(
"composer:replace-text", "composer:replace-text",
this.uploadPlaceholder.trim(), this.uploadPlaceholder.trim(),
markdown markdown
); );
this._resetUpload(false); this._resetUpload(false);
} else { } else {
this._resetUpload(true); this._resetUpload(true);
} }
});
}); });
$element.on("fileuploadfail", (e, data) => { $element.on("fileuploadfail", (e, data) => {
this._setUploadPlaceholderDone(data); run(() => {
this._resetUpload(true); this._setUploadPlaceholderDone(data);
this._resetUpload(true);
const userCancelled = this._xhr && this._xhr._userCancelled; const userCancelled = this._xhr && this._xhr._userCancelled;
this._xhr = null; this._xhr = null;
if (!userCancelled) { if (!userCancelled) {
displayErrorForUpload(data, this.siteSettings); displayErrorForUpload(data, this.siteSettings);
} }
});
}); });
if (this.site.mobileView) { if (this.site.mobileView) {