Просмотр исходного кода

fix: actually render/highlight comment fixing asyncronous bug

AI explained it correctly (fix done by myself though): This is especially important as this callback is used in the comment posting flow, where the document is refreshed right after posting a comment and then the posted comment should be highlighted. Thus, we need to ensure that the callback is called after the new content is rendered, which can be achieved by calling it in a new JS event loop using setTimeout with a delay of 0. This allows the browser to complete the rendering of the new content before executing the callback, ensuring that any DOM manipulations or interactions within the callback will work correctly with the updated content. In other words, using setTimeout with a delay of 0 ensures that the callback is executed asynchronously, allowing for proper rendering and interaction with the newly loaded content before any further actions are taken in the callback function.

Fixes https://github.com/PrivateBin/PrivateBin/issues/1834
rugk 3 месяцев назад
Родитель
Сommit
438ad0eeb6
1 измененных файлов с 7 добавлено и 4 удалено
  1. 7 4
      js/privatebin.js

+ 7 - 4
js/privatebin.js

@@ -5922,10 +5922,13 @@ window.PrivateBin = (function () {
                     // restore position
                     window.scrollTo(0, orgPosition);
 
-                    // NOTE: could create problems as callback may be called
-                    // asyncronously if PasteDecrypter e.g. needs to wait for a
-                    // password being entered
-                    callback();
+                    // NOTE: callback needs to be called (with setTimeout) after a JS
+                    // rendering cycle, as otherwise it would be called before the new content
+                    // is actually rendered, which would cause problems when trying to e.g.
+                    // highlight a comment right after refreshing the document, as the comment would not be in the DOM at that time.
+                    setTimeout(() => {
+                        callback();
+                    }, 0);
                 });
                 ServerInteraction.run();
             }, false); // this false is important as it circumvents the cache