mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-12-25 02:44:18 +00:00
Even better solution for concurrent promise handling
This commit is contained in:
parent
ff514368c3
commit
f977c808ec
@ -2,6 +2,7 @@ class App {
|
|||||||
constructor() {
|
constructor() {
|
||||||
this.data = {users: {}};
|
this.data = {users: {}};
|
||||||
this.eventListeners = {'users.patch': []};
|
this.eventListeners = {'users.patch': []};
|
||||||
|
this.promises = {users: {}};
|
||||||
this.socket = io({transports: ['websocket'], upgrade: false});
|
this.socket = io({transports: ['websocket'], upgrade: false});
|
||||||
this.socket.on('users.patch', patch => this.usersPatchHandler(patch));
|
this.socket.on('users.patch', patch => this.usersPatchHandler(patch));
|
||||||
}
|
}
|
||||||
@ -51,16 +52,10 @@ class App {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getUserById(userId) {
|
getUserById(userId) {
|
||||||
let promise;
|
if (userId in this.promises.users) {
|
||||||
|
return this.promises.users[userId];
|
||||||
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);});
|
|
||||||
}
|
}
|
||||||
}
|
this.promises.users[userId] = new Promise((resolve, reject) => {
|
||||||
promise = new Promise((resolve, reject) => {
|
|
||||||
this.socket.emit('users.user.get', userId, response => {
|
this.socket.emit('users.user.get', userId, response => {
|
||||||
if (response.code === 200) {
|
if (response.code === 200) {
|
||||||
this.data.users[userId] = response.payload;
|
this.data.users[userId] = response.payload;
|
||||||
@ -70,8 +65,7 @@ class App {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
this.data.users[userId] = promise;
|
return this.promises.users[userId];
|
||||||
return promise;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
usersPatchHandler(patch) {
|
usersPatchHandler(patch) {
|
||||||
|
Loading…
Reference in New Issue
Block a user