From 9be87ed8678a2554288ef6fd99660f55d3bd172b Mon Sep 17 00:00:00 2001
From: bobzel <zzzman@gmail.com>
Date: Thu, 8 Oct 2020 09:32:30 -0400
Subject: changed sharing manager to update users in one action, not multiple
 and only once per session if the user list doesn't change

---
 src/client/util/SharingManager.tsx | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

(limited to 'src/client/util/SharingManager.tsx')

diff --git a/src/client/util/SharingManager.tsx b/src/client/util/SharingManager.tsx
index d2e25dc26..c022974da 100644
--- a/src/client/util/SharingManager.tsx
+++ b/src/client/util/SharingManager.tsx
@@ -123,19 +123,28 @@ export class SharingManager extends React.Component<{}> {
     populateUsers = async () => {
         if (!this.populating) {
             this.populating = true;
-            runInAction(() => this.users = []);
             const userList = await RequestPromise.get(Utils.prepend("/getUsers"));
             const raw = JSON.parse(userList) as User[];
+            const sharingDocs: ValidatedUser[] = [];
             const evaluating = raw.map(async user => {
                 const isCandidate = user.email !== Doc.CurrentUserEmail;
                 if (isCandidate) {
-                    const userSharingDoc = await DocServer.GetRefField(user.sharingDocumentId);
-                    if (userSharingDoc instanceof Doc) {
-                        runInAction(() => this.users.push({ user, sharingDoc: userSharingDoc, userColor: StrCast(userSharingDoc.userColor) }));
+                    const sharingDoc = await DocServer.GetRefField(user.sharingDocumentId);
+                    if (sharingDoc instanceof Doc) {
+                        sharingDocs.push({ user, sharingDoc, userColor: StrCast(sharingDoc.color) });
                     }
                 }
             });
-            return Promise.all(evaluating).then(() => this.populating = false);
+            return Promise.all(evaluating).then(() => {
+                runInAction(() => {
+                    for (let i = 0; i < sharingDocs.length; i++) {
+                        if (!this.users.find(user => user.user.email === sharingDocs[i].user.email)) {
+                            this.users.push(sharingDocs[i]);
+                        }
+                    }
+                });
+                this.populating = false;
+            });
         }
     }
 
-- 
cgit v1.2.3-70-g09d2

