From ff514368c313760609a06795a0e5b149f703251d Mon Sep 17 00:00:00 2001 From: Patrick Jentsch Date: Wed, 1 Dec 2021 17:07:05 +0100 Subject: [PATCH] Try to avoid requesting user data multiple times --- app/static/js/nopaque/App.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/app/static/js/nopaque/App.js b/app/static/js/nopaque/App.js index aff90f3a..d6c72255 100644 --- a/app/static/js/nopaque/App.js +++ b/app/static/js/nopaque/App.js @@ -51,8 +51,16 @@ class App { } getUserById(userId) { - return new Promise((resolve, reject) => { - if (userId in this.data.users) {resolve(this.users[userId]);} + let promise; + + if (userId in this.data.users) { + if (this.data.users[userId] instanceof Promise) { + return this.data.users[userId]; + } else { + return new Promise((resolve, reject) => {resolve(userValue);}); + } + } + promise = new Promise((resolve, reject) => { this.socket.emit('users.user.get', userId, response => { if (response.code === 200) { this.data.users[userId] = response.payload; @@ -62,12 +70,13 @@ class App { } }); }); + this.data.users[userId] = promise; + return promise; } usersPatchHandler(patch) { let listener; - console.log(patch); this.data = jsonpatch.apply_patch(this.data, patch); for (listener of this.eventListeners['users.patch']) {listener(patch);} }