Add live updating of users

Provied a subscription for user updates and send updates whenever the
details of a user account is changed.
This commit is contained in:
Hornwitser committed 2022-07-08 01:00:54 +02:00
1 parent db562ce6a0
commit 096f338277
12 files changed
+292 -39

No files matched your search

+61
View File
@@ -86,6 +86,7 @@ export class Control extends libLink.Link {
this.slaveUpdateHandlers = new Map();
this.instanceUpdateHandlers = new Map();
this.saveListUpdateHandlers = new Map();
this.userUpdateHandlers = new Map();
this.logHandlers = new Map();
this.connector.on("connect", data => {
@@ -101,6 +102,9 @@ export class Control extends libLink.Link {
this.updateSaveListSubscriptions().catch(err => logger.error(
`Unexpected error updating save list subscriptions:\n${err.stack}`
));
this.updateUserSubscriptions().catch(err => logger.error(
`Unexpected error updating user subscriptions:\n${err.stack}`
));
this.updateLogSubscriptions().catch(err => logger.error(
`Unexpected error updating log subscriptions:\n${err.stack}`
));
@@ -315,6 +319,63 @@ export class Control extends libLink.Link {
});
}
async userUpdateEventHandler(message) {
let handlers = [].concat(
this.userUpdateHandlers.get(null) || [],
this.userUpdateHandlers.get(message.data.name) || [],
);
for (let handler of handlers) {
handler(message.data);
}
};
async onUserUpdate(name, handler) {
if (name !== null && typeof name !== "string") {
throw new Error("Invalid user name");
}
let handlers = this.userUpdateHandlers.get(name);
if (!handlers) {
handlers = [];
this.userUpdateHandlers.set(name, handlers);
}
handlers.push(handler);
if (handlers.length === 1) {
await this.updateUserSubscriptions();
}
}
async offUserUpdate(name, handler) {
let handlers = this.userUpdateHandlers.get(name);
if (!handlers || !handlers.length) {
throw new Error(`No handlers for user ${name} exist`);
}
let index = handlers.lastIndexOf(handler);
if (index === -1) {
throw new Error(`Given handler is not registered for user ${name}`);
}
handlers.splice(index, 1);
if (!handlers.length) {
this.userUpdateHandlers.delete(name);
await this.updateUserSubscriptions();
}
}
async updateUserSubscriptions() {
if (!this.connector.connected) {
return;
}
await libLink.messages.setUserSubscriptions.send(this, {
all: this.userUpdateHandlers.has(null),
names: [...this.userUpdateHandlers.keys()].filter(e => e !== null),
});
}
async logMessageEventHandler(message) {
let info = message.data.info;