mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-12-27 03:44:19 +00:00
54 lines
1.2 KiB
JavaScript
54 lines
1.2 KiB
JavaScript
nopaque.UsersExtension = class UsersExtension {
|
|
#data;
|
|
#promises;
|
|
|
|
constructor(app) {
|
|
this.app = app;
|
|
|
|
this.#data = {};
|
|
this.app.data.users = this.#data;
|
|
|
|
this.#promises = {
|
|
get: {},
|
|
subscribe: {}
|
|
};
|
|
}
|
|
|
|
async #get(userId) {
|
|
const response = await this.app.socket.emitWithAck('users.get', userId);
|
|
|
|
if (response.status != 200) {
|
|
throw new Error(`[${response.status}] ${response.statusText}`);
|
|
}
|
|
|
|
this.#data[userId] = response.body;
|
|
return this.#data[userId];
|
|
}
|
|
|
|
get(userId) {
|
|
if (userId in this.#promises.get) {
|
|
return this.#promises.get[userId];
|
|
}
|
|
|
|
this.#promises.get[userId] = this.#get(userId);
|
|
return this.#promises.get[userId];
|
|
}
|
|
|
|
async #subscribe(userId) {
|
|
const response = await this.app.socket.emitWithAck('users.subscribe', userId);
|
|
|
|
if (response.status != 200) {
|
|
throw new Error(`[${response.status}] ${response.statusText}`);
|
|
}
|
|
}
|
|
|
|
subscribe(userId) {
|
|
if (userId in this.#promises.subscribe) {
|
|
return this.#promises.subscribe[userId];
|
|
}
|
|
|
|
this.#promises.subscribe[userId] = this.#subscribe(userId);
|
|
return this.#promises.subscribe[userId];
|
|
}
|
|
}
|