* Add en event to the end of the queue. * * @param event Event function to add. * @param returnPromise If a promise should be returned. * @returns Nothing or a promise that resolves after the event is executed, * or rejects if it rejects or throws an error.
(event: EventFunction, returnPromise?: boolean)
| 29 | * or rejects if it rejects or throws an error. |
| 30 | */ |
| 31 | push(event: EventFunction, returnPromise?: boolean): Promise<void> | void { |
| 32 | // Wrap the event, and create a promise, if a promise should be returned |
| 33 | const [wrappedEvent, promise] = returnPromise ? wrapEvent(event) : [undefined, undefined]; |
| 34 | // Push end off early if max size is reached |
| 35 | if (this.queue.length === this.maxSize) { |
| 36 | this.queue.shift(); |
| 37 | } |
| 38 | // Add event to the end of the queue |
| 39 | this.queue.push(wrappedEvent || event); |
| 40 | this.update(); |
| 41 | // Return promise (if any) |
| 42 | return promise; |
| 43 | } |
| 44 | |
| 45 | private update() { |
| 46 | if (!this.isExecuting && this.queue.length > 0) { |