3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-17 15:15:02 +00:00

feat: add member cache; handle all role changes with RoleManagerPlugin; exit gracefully

This commit is contained in:
Dragory 2023-05-07 17:56:55 +03:00
parent fd60a09947
commit fa50110766
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
48 changed files with 755 additions and 264 deletions

40
backend/src/Blocker.ts Normal file
View file

@ -0,0 +1,40 @@
export type Block = {
count: number;
unblock: () => void;
getPromise: () => Promise<void>;
};
export class Blocker {
#blocks: Map<string, Block> = new Map();
block(key: string): void {
if (!this.#blocks.has(key)) {
const promise = new Promise<void>((resolve) => {
this.#blocks.set(key, {
count: 0, // Incremented to 1 further below
unblock() {
this.count--;
if (this.count === 0) {
resolve();
}
},
getPromise: () => promise, // :d
});
});
}
this.#blocks.get(key)!.count++;
}
unblock(key: string): void {
if (this.#blocks.has(key)) {
this.#blocks.get(key)!.unblock();
}
}
async waitToBeUnblocked(key: string): Promise<void> {
if (!this.#blocks.has(key)) {
return;
}
await this.#blocks.get(key)!.getPromise();
}
}