FIX: when destroying a draft always ensure saving is done
There was a race condition where drafts could be either saving or queued to be saved and a user canceled draft leading to destroying it. This cancels debounce save and waits for save in the pipeline to be over prior to firing off a DELETE on the draft
This commit is contained in:
parent
f41fcad6c3
commit
1cf2d1f9f2
|
@ -1,7 +1,7 @@
|
||||||
import I18n from "I18n";
|
import I18n from "I18n";
|
||||||
import { isEmpty } from "@ember/utils";
|
import { isEmpty } from "@ember/utils";
|
||||||
import { and, or, alias, reads } from "@ember/object/computed";
|
import { and, or, alias, reads } from "@ember/object/computed";
|
||||||
import { debounce } from "@ember/runloop";
|
import { cancel, debounce } from "@ember/runloop";
|
||||||
import { inject as service } from "@ember/service";
|
import { inject as service } from "@ember/service";
|
||||||
import { inject } from "@ember/controller";
|
import { inject } from "@ember/controller";
|
||||||
import Controller from "@ember/controller";
|
import Controller from "@ember/controller";
|
||||||
|
@ -982,6 +982,10 @@ export default Controller.extend({
|
||||||
this.send("clearTopicDraft");
|
this.send("clearTopicDraft");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this._saveDraftPromise) {
|
||||||
|
return this._saveDraftPromise.then(() => this.destroyDraft());
|
||||||
|
}
|
||||||
|
|
||||||
return Draft.clear(key, this.get("model.draftSequence")).then(() =>
|
return Draft.clear(key, this.get("model.draftSequence")).then(() =>
|
||||||
this.appEvents.trigger("draft:destroyed", key)
|
this.appEvents.trigger("draft:destroyed", key)
|
||||||
);
|
);
|
||||||
|
@ -1028,6 +1032,10 @@ export default Controller.extend({
|
||||||
cancelComposer(differentDraft = false) {
|
cancelComposer(differentDraft = false) {
|
||||||
this.skipAutoSave = true;
|
this.skipAutoSave = true;
|
||||||
|
|
||||||
|
if (this._saveDraftDebounce) {
|
||||||
|
cancel(this._saveDraftDebounce);
|
||||||
|
}
|
||||||
|
|
||||||
const keyPrefix =
|
const keyPrefix =
|
||||||
this.model.action === "edit" ? "post.abandon_edit" : "post.abandon";
|
this.model.action === "edit" ? "post.abandon_edit" : "post.abandon";
|
||||||
|
|
||||||
|
@ -1102,11 +1110,12 @@ export default Controller.extend({
|
||||||
// in test debounce is Ember.run, this will cause
|
// in test debounce is Ember.run, this will cause
|
||||||
// an infinite loop
|
// an infinite loop
|
||||||
if (ENV.environment !== "test") {
|
if (ENV.environment !== "test") {
|
||||||
debounce(this, this._saveDraft, 2000);
|
this._saveDraftDebounce = debounce(this, this._saveDraft, 2000);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
model.saveDraft().finally(() => {
|
this._saveDraftPromise = model.saveDraft().finally(() => {
|
||||||
this._lastDraftSaved = Date.now();
|
this._lastDraftSaved = Date.now();
|
||||||
|
this._saveDraftPromise = null;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1127,7 +1136,7 @@ export default Controller.extend({
|
||||||
if (Date.now() - this._lastDraftSaved > 15000) {
|
if (Date.now() - this._lastDraftSaved > 15000) {
|
||||||
this._saveDraft();
|
this._saveDraft();
|
||||||
} else {
|
} else {
|
||||||
debounce(this, this._saveDraft, 2000);
|
this._saveDraftDebounce = debounce(this, this._saveDraft, 2000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue