A platform for community discussion. Free, open, simple.
DEV: Reduce unnecessary UI updates when posts change (#33918)
Sérgio Saquetim216df43502e2
diff --git a/app/assets/javascripts/discourse/app/models/post-stream.js b/app/assets/javascripts/discourse/app/models/post-stream.jsindex e6e1febad15..7532d8a99b9 100644--- a/app/assets/javascripts/discourse/app/models/post-stream.js+++ b/app/assets/javascripts/discourse/app/models/post-stream.js@@ -843,27 +843,42 @@ export default class PostStream extends RestModel {return Promise.resolve();}- triggerChangedPost(postId, updatedAt, opts) {- opts = opts || {};+ /**+ * Updates a post in the stream when it has been changed on the server.+ *+ * @param {number} postId - The ID of the post to update+ * @param {string} updatedAt - The timestamp when the post was last updated+ * @param {Object} opts - Additional options for updating the post+ * @param {boolean} [opts.preserveCooked] - Whether to preserve the cooked HTML content+ * @returns {Promise} A promise that resolves when the post has been updated+ */+ async triggerChangedPost(postId, updatedAt, opts = {}) {+ opts ||= {};- const resolved = Promise.resolve();if (!postId) {- return resolved;+ return;}const existing = this._identityMap[postId];++ // Only fetch and update if the post exists and has a different updated timestampif (existing && existing.updated_at !== updatedAt) {- const url = "/posts/" + postId;- const store = this.store;- return ajax(url).then((p) => {- if (opts.preserveCooked) {- p.cooked = existing.get("cooked");- }+ // Fetch the latest post data from the server+ const updatedData = await ajax(`/posts/${postId}`);- this.storePost(store.createRecord("post", p));- });+ // Preserve the existing cooked HTML content if requested+ if (opts.preserveCooked) {+ updatedData.cooked = existing.cooked;+ }++ // Create a new post record with updated data and store it in the identity map.+ // Creating a new record will update the existing one in the map, which will then+ // trigger re-rendering of UI components that use the tracked data that was updated.+ const updatedPost = this.store.createRecord("post", updatedData);++ // Update the post in the post stream's identity map+ this.storePost(updatedPost);}- return resolved;}triggerLikedPost(postId, likesCount, userID, eventType) {@@ -1098,7 +1113,9 @@ export default class PostStream extends RestModel {return existing;}- post.set("topic", this.topic);+ if (post.topic !== this.topic) {+ post.topic = this.topic;+ }this._identityMap[post.get("id")] = post;}return post;diff --git a/app/assets/javascripts/discourse/app/services/store.js b/app/assets/javascripts/discourse/app/services/store.jsindex bc7544a8410..3f1880a40cf 100644--- a/app/assets/javascripts/discourse/app/services/store.js+++ b/app/assets/javascripts/discourse/app/services/store.js@@ -1,3 +1,4 @@+import { untrack } from "@glimmer/validator";import { warn } from "@ember/debug";import { set } from "@ember/object";import Service from "@ember/service";@@ -418,8 +419,26 @@ export default class StoreService extends Service {klass = RestModel;}- existing.setProperties(klass.munge(obj));+ const updatedProperties = klass.munge(obj);++ // When running property comparisons, we need to prevent Glimmer from creating tracking contexts+ // which would otherwise cause "already used in same computation" errors when the values update+ untrack(() => {+ // Only update properties whose values actually changed to optimize rerenders+ // If a property value is unchanged, remove it from the update list+ updatedProperties &&+ Object.keys(updatedProperties).forEach((key) => {+ if (existing[key] === updatedProperties[key]) {+ delete updatedProperties[key];+ }+ });+ });++ // Apply all property updates in a single batch to trigger just one rerender+ existing.setProperties(updatedProperties);+obj[adapter.primaryKey] = id;+return existing;}