From 372272546243397d024f41b914f08b122ff9f3c4 Mon Sep 17 00:00:00 2001 From: yflory Date: Thu, 23 Apr 2020 17:22:36 +0200 Subject: [PATCH] Fix race condition when adding a new comment --- www/pad/comment.js | 2 +- www/pad/comments.js | 22 ++++++++++------------ 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/www/pad/comment.js b/www/pad/comment.js index 549b348f3..7f8da3021 100644 --- a/www/pad/comment.js +++ b/www/pad/comment.js @@ -125,7 +125,7 @@ if (editor.ui.addButton) { editor.ui.addButton('Comment', { label: 'COMMENT', - command: pluginName, + command: 'comment', icon : '/pad/icons/comment.png', toolbar: 'insert,10' }); diff --git a/www/pad/comments.js b/www/pad/comments.js index 52d627a26..f0f0e9cfc 100644 --- a/www/pad/comments.js +++ b/www/pad/comments.js @@ -73,7 +73,6 @@ define([ data.avatar = userData.avatar; data.profile = userData.profile; data.curvePublic = userData.curvePublic; - console.log(data); return myAuthorId; }; @@ -143,7 +142,8 @@ define([ var redrawComments = function (Env) { // Don't redraw if there were no change - var str = Sortify(Env.comments || {}); + var str = Sortify((Env.comments || {}).data || {}); + if (str === Env.oldComments) { return; } Env.oldComments = str; @@ -164,7 +164,6 @@ define([ }).toArray(); var done = []; - order.forEach(function (key) { // Avoir duplicates if (done.indexOf(key) !== -1) { return; } @@ -414,11 +413,18 @@ define([ v: canonicalize(Env.editor.getSelection().getSelectedText()) }] }; + // There may be a race condition between updateMetadata and addMark that causes + // * updateMetadata first: comment not rendered (redrawComments called + // before addMark) + // * addMark first: comment deleted (checkDeleted called before updateMetadata) + // ==> we're going to call updateMetadata first, and we'll invalidate the cache + // of rendered comments to display them properly in redrawComments updateMetadata(Env); - addMark(); Env.framework.localChange(); + + Env.oldComments = undefined; }); Env.$container.prepend(form).show(); }; @@ -430,11 +436,6 @@ define([ onChange(Env); checkDeleted(Env); }; - var localChange = function (Env) { - if (!Env.ready) { return; } - // Check deleted - checkDeleted(Env); - }; var ready = function (Env) { Env.ready = 0; @@ -453,14 +454,12 @@ define([ } Env.$container.find('.cp-comment-active').removeClass('cp-comment-active'); Env.$inner.find('comment.active').removeClass('active'); - Env.$container.find('.cp-comment-form').remove(); }); // Unselect comment when clicking on another part of the doc Env.$inner.on('click', function (e) { if ($(e.target).closest('comment').length) { return; } Env.$container.find('.cp-comment-active').removeClass('cp-comment-active'); Env.$inner.find('comment.active').removeClass('active'); - Env.$container.find('.cp-comment-form').remove(); }); Env.$inner.on('click', 'comment', function (e) { var $comment = $(e.target); @@ -484,7 +483,6 @@ define([ return { onContentUpdate: call(onContentUpdate), - localChange: call(localChange), ready: call(ready) }; };