diff --git a/src/actions/TagOrderActions.js b/src/actions/TagOrderActions.js index 608fd6c4c5..dd4df6a9d4 100644 --- a/src/actions/TagOrderActions.js +++ b/src/actions/TagOrderActions.js @@ -42,9 +42,14 @@ TagOrderActions.moveTag = function(matrixClient, tag, destinationIx) { tags = tags.filter((t) => t !== tag); tags = [...tags.slice(0, destinationIx), tag, ...tags.slice(destinationIx)]; + const storeId = TagOrderStore.getStoreId(); + return asyncAction('TagOrderActions.moveTag', () => { Analytics.trackEvent('TagOrderActions', 'commitTagOrdering'); - return matrixClient.setAccountData('im.vector.web.tag_ordering', {tags}); + return matrixClient.setAccountData( + 'im.vector.web.tag_ordering', + {tags, _storeId: storeId}, + ); }, () => { // For an optimistic update return {tags}; diff --git a/src/stores/TagOrderStore.js b/src/stores/TagOrderStore.js index 27ce6cbb73..effd8287c7 100644 --- a/src/stores/TagOrderStore.js +++ b/src/stores/TagOrderStore.js @@ -63,6 +63,11 @@ class TagOrderStore extends Store { // Get ordering from account data case 'MatrixActions.accountData': { if (payload.event_type !== 'im.vector.web.tag_ordering') break; + + // Ignore remote echos caused by this store so as to avoid setting + // state back to old state. + if (payload.event_content._storeId === this.getStoreId()) break; + this._setState({ orderedTagsAccountData: payload.event_content ? payload.event_content.tags : null, }); @@ -176,6 +181,13 @@ class TagOrderStore extends Store { return this._state.orderedTags; } + getStoreId() { + // Generate a random ID to prevent this store from clobbering its + // state with redundant remote echos. + if (!this._id) this._id = Math.random().toString(16).slice(2, 10); + return this._id; + } + getSelectedTags() { return this._state.selectedTags; }