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:
parent
fd60a09947
commit
fa50110766
48 changed files with 755 additions and 264 deletions
40
backend/src/Blocker.ts
Normal file
40
backend/src/Blocker.ts
Normal 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();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue