FIX: Debounce user identities lookup

FIX: Debounce user identities lookup

diff --git a/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6 b/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6
index 915d4e1..a799ac9 100644
--- a/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6
+++ b/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6
@@ -19,10 +19,10 @@ import showModal from "discourse/lib/show-modal";
 import { cookAsync } from "discourse/lib/text";
 import {
   ENCRYPT_DISABLED,
+  getDebouncedUserIdentities,
   getEncryptionStatus,
   getIdentity,
   getTopicKey,
-  getUserIdentities,
   hasTopicKey,
   hasTopicTitle
 } from "discourse/plugins/discourse-encrypt/lib/discourse";
@@ -339,14 +339,14 @@ export default {
                   .then(key => decrypt(key, ciphertext))
                   .then(plaintext => {
                     if (plaintext.signature) {
-                      getUserIdentities([plaintext.signed_by_name])
-                        .then(identities =>
-                          verify(
+                      getDebouncedUserIdentities([plaintext.signed_by_name])
+                        .then(identities => {
+                          return verify(
                             identities[plaintext.signed_by_name].signPublic,
                             plaintext,
                             ciphertext
-                          )
-                        )
+                          );
+                        })
                         .then(result => {
                           verified[attrs.id] = checkMetadata(attrs, plaintext);
                           if (!result) {
diff --git a/assets/javascripts/discourse/initializers/hook-decrypt-topic.js.es6 b/assets/javascripts/discourse/initializers/hook-decrypt-topic.js.es6
index 5c16b61..b38e6d3 100644
--- a/assets/javascripts/discourse/initializers/hook-decrypt-topic.js.es6
+++ b/assets/javascripts/discourse/initializers/hook-decrypt-topic.js.es6
@@ -1,3 +1,4 @@
+import { debounce, scheduleOnce } from "@ember/runloop";
 import { iconHTML } from "discourse-common/lib/icon-library";
 import { withPluginApi } from "discourse/lib/plugin-api";
 import { emojiUnescape } from "discourse/lib/text";
@@ -75,8 +76,8 @@ export default {
     var self = this;
     Ember.Component.reopen({
       didRender() {
-        Ember.run.scheduleOnce("afterRender", self, () => {
-          Ember.run.debounce(self, self.decryptTitles, 500);
+        scheduleOnce("afterRender", self, () => {
+          debounce(self, self.decryptTitles, 500);
         });
         return this._super(...arguments);
       }
@@ -87,7 +88,7 @@ export default {
       api.decorateWidget("header:after", helper => {
         if (helper.widget.state.userVisible) {
           self.decryptTitles();
-          Ember.run.debounce(self, self.decryptTitles, 500);
+          debounce(self, self.decryptTitles, 500);
         }
       });
     });
diff --git a/assets/javascripts/lib/discourse.js.es6 b/assets/javascripts/lib/discourse.js.es6
index 7492286..0b14a08 100644
--- a/assets/javascripts/lib/discourse.js.es6
+++ b/assets/javascripts/lib/discourse.js.es6
@@ -1,3 +1,4 @@
+import { debounce } from "@ember/runloop";
 import { ajax } from "discourse/lib/ajax";
 import { Promise } from "rsvp";
 import {
@@ -94,6 +95,31 @@ export function getUserIdentities(usernames) {
   });
 }
 
+const debouncedUsernames = new Set();
+
+function _getDebouncedUserIdentities(resolve, reject) {
+  getUserIdentities(Array.from(debouncedUsernames))
+    .then(identities => {
+      Object.keys(identities).forEach(u => debouncedUsernames.delete(u));
+      return identities;
+    })
+    .then(resolve, reject);
+}
+
+export function getDebouncedUserIdentities(usernames) {
+  usernames.forEach(u => debouncedUsernames.add(u));
+
+  return new Ember.RSVP.Promise((resolve, reject) => {
+    debounce(
+      debouncedUsernames,
+      _getDebouncedUserIdentities,
+      resolve,
+      reject,
+      500
+    );
+  });
+}
+
 /**
  * Puts a topic key into storage.
  *

GitHub sha: 0ebbd390