Skip to content

A platform for community discussion. Free, open, simple.

FIX: Keep edits made while a workflow save is in flight (#44546)

Régis Hanol60ca3ce756f7
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/editor/index.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/editor/index.gjs
index 1b8bdd24760..7a1cc598060 100644
--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/editor/index.gjs
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/editor/index.gjs
@@ -6,6 +6,7 @@ import Form from "discourse/components/form";
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { bind } from "discourse/lib/decorators";
+import { deepEqual } from "discourse/lib/object";
import DiscourseURL from "discourse/lib/url";
import { i18n } from "discourse-i18n";
import WorkflowEditorSession from "../../../lib/workflows/editor-session";
@@ -158,7 +159,6 @@ export default class WorkflowsEditor extends Component {
ignoreDirty = () => false;
undoManager = new UndoManager();
currentSavePromise = null;
- pendingGraphSnapshot = null;
pendingSaveOptions = null;
initialNodeHandled = false;
workflowSession = new WorkflowEditorSession({
@@ -1053,11 +1053,10 @@ export default class WorkflowsEditor extends Component {
async handleSubmit(options = {}) {
if (this.saving) {
if (options.throwOnError) {
- await this.currentSavePromise;
+ await this.currentSavePromise?.catch(() => {});
return this.handleSubmit(options);
} else {
this.pendingSave = true;
- this.pendingGraphSnapshot = this.#captureGraphSnapshot();
this.pendingSaveOptions = this.#mergePendingSaveOptions(
this.pendingSaveOptions,
options
@@ -1080,7 +1079,6 @@ export default class WorkflowsEditor extends Component {
@action
replaceWorkflow(workflow) {
this.pendingSave = false;
- this.pendingGraphSnapshot = null;
this.pendingSaveOptions = null;
this.args.workflow.setProperties({
@@ -1516,8 +1514,11 @@ export default class WorkflowsEditor extends Component {
saveProperties.static_data = options.staticData;
}
+ const sentGraph = this.#captureGraphSnapshot();
await this.args.workflow.save(saveProperties);
- this.#syncFromServer();
+ if (deepEqual(this.#captureGraphSnapshot(), sentGraph)) {
+ this.#syncFromServer();
+ }
if (!this.isUndoRedo) {
this.undoManager.commitAction(this.#captureGraphSnapshot());
@@ -1539,7 +1540,6 @@ export default class WorkflowsEditor extends Component {
} finally {
this.saving = false;
if (this.pendingSave) {
- const pendingSnapshot = this.pendingGraphSnapshot;
const pendingOptions = this.pendingSaveOptions || {};
if (
options.staticData !== undefined &&
@@ -1551,11 +1551,7 @@ export default class WorkflowsEditor extends Component {
);
}
this.pendingSave = false;
- this.pendingGraphSnapshot = null;
this.pendingSaveOptions = null;
- if (pendingSnapshot) {
- this.#applyGraphSnapshot(pendingSnapshot);
- }
this.handleSubmit(pendingOptions);
}
}
diff --git a/plugins/discourse-workflows/test/javascripts/integration/components/workflows/editor-save-test.gjs b/plugins/discourse-workflows/test/javascripts/integration/components/workflows/editor-save-test.gjs
new file mode 100644
index 00000000000..d08f31aafe0
--- /dev/null
+++ b/plugins/discourse-workflows/test/javascripts/integration/components/workflows/editor-save-test.gjs
@@ -0,0 +1,79 @@
+import { render, waitUntil } from "@ember/test-helpers";
+import { module, test } from "qunit";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import WorkflowsEditor from "discourse/plugins/discourse-workflows/admin/components/workflows/editor";
+import WorkflowNode from "discourse/plugins/discourse-workflows/admin/models/workflow-node";
+
+let editor;
+
+class TestEditor extends WorkflowsEditor {
+ constructor() {
+ super(...arguments);
+ editor = this;
+ }
+
+ <template></template>
+}
+
+function savingWorkflow(nodes) {
+ const saves = [];
+ const workflow = {
+ nodes,
+ connections: {},
+ setProperties(properties) {
+ Object.assign(this, properties);
+ },
+ graphProperties() {
+ return { nodes: this.nodes };
+ },
+ save() {
+ return new Promise((resolve) =>
+ saves.push({ nodes: structuredClone(this.nodes), resolve })
+ );
+ },
+ };
+ return { workflow, saves };
+}
+
+async function renderEditor(workflow) {
+ await render(<template><TestEditor @workflow={{workflow}} /></template>);
+
+ const data = {
+ nodes: workflow.nodes.map((node) => WorkflowNode.create(node)),
+ connections: [],
+ stickyNotes: [],
+ };
+ editor.registerApi({
+ get: (key) => data[key],
+ set: (key, value) => (data[key] = value),
+ });
+}
+
+module("Integration | Component | workflows editor saves", function (hooks) {
+ setupRenderingTest(hooks);
+
+ test("a waiting save keeps edits made while another save is in flight", async function (assert) {
+ const { workflow, saves } = savingWorkflow([
+ { clientId: "n1", type: "action:log", name: "Log", configuration: {} },
+ ]);
+ await renderEditor(workflow);
+
+ const first = editor.handleSubmit();
+ editor.formApi.set("nodes", [
+ WorkflowNode.create({
+ clientId: "n1",
+ type: "action:log",
+ name: "Log",
+ configuration: { message: "Y" },
+ }),
+ ]);
+ const second = editor.handleSubmit({ throwOnError: true });
+ saves[0].resolve();
+ await first;
+ await waitUntil(() => saves.length === 2);
+ saves[1].resolve();
+ await second;
+
+ assert.strictEqual(saves[1].nodes[0].configuration.message, "Y");
+ });
+});