From 0e70a5a2210e4dfe8d2817df142313da0711e090 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Wed, 15 Mar 2023 09:22:09 +0100 Subject: [PATCH] FIX: add observed cache to undo changes mixin Prevents observer add / remove race conditions which can cause exceptions. --- .../javascripts/discourse/mixins/undo-changes.js.es6 | 10 +++++++++- plugin.rb | 2 +- .../fixtures/sprockets/require_tree_discourse_empty.js | 2 +- .../sprockets/require_tree_discourse_non_existant.js | 2 +- spec/fixtures/sprockets/require_tree_discourse_test.js | 2 +- 5 files changed, 13 insertions(+), 5 deletions(-) diff --git a/assets/javascripts/discourse/mixins/undo-changes.js.es6 b/assets/javascripts/discourse/mixins/undo-changes.js.es6 index b2ab322d..91497eae 100644 --- a/assets/javascripts/discourse/mixins/undo-changes.js.es6 +++ b/assets/javascripts/discourse/mixins/undo-changes.js.es6 @@ -4,6 +4,8 @@ import { get, set } from "@ember/object"; import Mixin from "@ember/object/mixin"; import { deepEqual } from "discourse-common/lib/object"; +var observedCache = []; + export default Mixin.create({ didInsertElement() { this._super(...arguments); @@ -32,7 +34,10 @@ export default Mixin.create({ }; listProperties(componentType, opts).forEach((property) => { - obj.removeObserver(property, this, this.toggleUndo); + if (observedCache.includes(property)) { + obj.removeObserver(property, this, this.toggleUndo); + observedCache = observedCache.filter(p => p !== property); + } }); }, @@ -45,6 +50,9 @@ export default Mixin.create({ }; listProperties(componentType, opts).forEach((property) => { + if (observedCache.indexOf(property) === -1) { + observedCache.push(property); + } obj.addObserver(property, this, this.toggleUndo); }); }, diff --git a/plugin.rb b/plugin.rb index d7ffc7f4..7f6829eb 100644 --- a/plugin.rb +++ b/plugin.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # name: discourse-custom-wizard # about: Forms for Discourse. Better onboarding, structured posting, data enrichment, automated actions and much more. -# version: 2.2.6 +# version: 2.2.7 # authors: Angus McLeod, Faizaan Gagan, Robert Barrow, Keegan George, Kaitlin Maddever # url: https://github.com/paviliondev/discourse-custom-wizard # contact_emails: development@pavilion.tech diff --git a/spec/fixtures/sprockets/require_tree_discourse_empty.js b/spec/fixtures/sprockets/require_tree_discourse_empty.js index 953c5ec4..4f034d13 100644 --- a/spec/fixtures/sprockets/require_tree_discourse_empty.js +++ b/spec/fixtures/sprockets/require_tree_discourse_empty.js @@ -1 +1 @@ -//= require_tree_discourse \ No newline at end of file +//= require_tree_discourse diff --git a/spec/fixtures/sprockets/require_tree_discourse_non_existant.js b/spec/fixtures/sprockets/require_tree_discourse_non_existant.js index d9b2be76..fc4752e5 100644 --- a/spec/fixtures/sprockets/require_tree_discourse_non_existant.js +++ b/spec/fixtures/sprockets/require_tree_discourse_non_existant.js @@ -1 +1 @@ -//= require_tree_discourse dummy_path \ No newline at end of file +//= require_tree_discourse dummy_path diff --git a/spec/fixtures/sprockets/require_tree_discourse_test.js b/spec/fixtures/sprockets/require_tree_discourse_test.js index a86aa0d7..56451213 100644 --- a/spec/fixtures/sprockets/require_tree_discourse_test.js +++ b/spec/fixtures/sprockets/require_tree_discourse_test.js @@ -1 +1 @@ -//= require_tree_discourse sptest \ No newline at end of file +//= require_tree_discourse sptest