diff options
author | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-06-28 18:43:22 +0200 |
---|---|---|
committer | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-06-28 18:43:22 +0200 |
commit | 229e741bfdd4a6d8b7b2617eec5e88c626d29e00 (patch) | |
tree | 3d85df3f10054678108826993da596b1315093e5 /src/util | |
parent | 1.3.20 (diff) | |
download | server-229e741bfdd4a6d8b7b2617eec5e88c626d29e00.tar.xz |
:sparkles: Rate Limit model
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/Database.ts | 28 |
1 files changed, 17 insertions, 11 deletions
diff --git a/src/util/Database.ts b/src/util/Database.ts index 1b77629f..0732cb4e 100644 --- a/src/util/Database.ts +++ b/src/util/Database.ts @@ -46,6 +46,7 @@ export interface MongooseCache { export class MongooseCache extends EventEmitter { public stream: ChangeStream; public data: any; + public initalizing?: Promise<void>; constructor( public collection: Collection, @@ -59,19 +60,24 @@ export class MongooseCache extends EventEmitter { if (this.opts.array == null) this.opts.array = true; } - init = async () => { - // @ts-ignore - this.stream = this.collection.watch(this.pipeline, { fullDocument: "updateLookup" }); + init = () => { + if (this.initalizing) return this.initalizing; + this.initalizing = new Promise(async (resolve, reject) => { + // @ts-ignore + this.stream = this.collection.watch(this.pipeline, { fullDocument: "updateLookup" }); - this.stream.on("change", this.change); - this.stream.on("close", this.destroy); - this.stream.on("error", console.error); + this.stream.on("change", this.change); + this.stream.on("close", this.destroy); + this.stream.on("error", console.error); - if (!this.opts.onlyEvents) { - const arr = await this.collection.aggregate(this.pipeline).toArray(); - if (this.opts.array) this.data = arr || []; - else this.data = arr?.[0]; - } + if (!this.opts.onlyEvents) { + const arr = await this.collection.aggregate(this.pipeline).toArray(); + if (this.opts.array) this.data = arr || []; + else this.data = arr?.[0]; + } + resolve(); + }); + return this.initalizing; }; changeStream = (pipeline: any) => { |