mirror of
https://github.com/dzeiocom/libs.git
synced 2025-04-22 10:52:11 +00:00
Added Queue System
This commit is contained in:
parent
a0d210acb6
commit
dd8515cc9b
1
packages/queue/.gitignore
vendored
Normal file
1
packages/queue/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
dist
|
3
packages/queue/.npmignore
Normal file
3
packages/queue/.npmignore
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
Queue.ts
|
||||||
|
.gitignore
|
||||||
|
tsconfig.json
|
49
packages/queue/Queue.ts
Normal file
49
packages/queue/Queue.ts
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
import Logger from "@dzeio/logger"
|
||||||
|
|
||||||
|
export default class Queue {
|
||||||
|
private queue = 0
|
||||||
|
private isPaused = false
|
||||||
|
|
||||||
|
public constructor(
|
||||||
|
private maxQueueLength = 5,
|
||||||
|
private timeToWait = 500
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public pause() {
|
||||||
|
this.isPaused = true
|
||||||
|
}
|
||||||
|
|
||||||
|
public start() {
|
||||||
|
this.isPaused = false
|
||||||
|
}
|
||||||
|
|
||||||
|
public updateCurrentQueueLength(len: number) {
|
||||||
|
this.queue = len
|
||||||
|
}
|
||||||
|
|
||||||
|
public async add<T = any>(promise: Promise<T>) {
|
||||||
|
while (this.queue >= this.maxQueueLength || this.isPaused) {
|
||||||
|
await new Promise((res) => setTimeout(res, this.timeToWait))
|
||||||
|
}
|
||||||
|
this.updateCurrentQueueLength(this.queue+1)
|
||||||
|
promise
|
||||||
|
.then(() => {
|
||||||
|
this.updateCurrentQueueLength(this.queue-1)
|
||||||
|
}).catch(() => {
|
||||||
|
this.updateCurrentQueueLength(this.queue-1)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
public async waitEnd() {
|
||||||
|
let currentQueue = this.queue
|
||||||
|
while (this.queue !== 0) {
|
||||||
|
await new Promise((res) => setTimeout(() => {
|
||||||
|
if (currentQueue !== this.queue) {
|
||||||
|
Logger.log('PromiseQueue', this.queue, 'remaining in queue')
|
||||||
|
currentQueue = this.queue
|
||||||
|
}
|
||||||
|
res()
|
||||||
|
}, this.timeToWait))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
24
packages/queue/package.json
Normal file
24
packages/queue/package.json
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"name": "@dzeio/queue",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "My Personnal Promise Queue System",
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/dzeiocom/libs.git",
|
||||||
|
"directory": "packages/Queue"
|
||||||
|
},
|
||||||
|
"author": "Aviortheking",
|
||||||
|
"license": "MIT",
|
||||||
|
"main": "./dist/Queue.js",
|
||||||
|
"types": "./dist/Queue.d.ts",
|
||||||
|
"devDependencies": {
|
||||||
|
"typescript": "^3.9.5"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"prepublishOnly": "yarn build",
|
||||||
|
"build": "tsc"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@dzeio/logger": "^1.1.1"
|
||||||
|
}
|
||||||
|
}
|
12
packages/queue/tsconfig.json
Normal file
12
packages/queue/tsconfig.json
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
"extends": "../../tsconfig.base.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"outDir": "dist"
|
||||||
|
},
|
||||||
|
"exclude": [
|
||||||
|
"node_modules"
|
||||||
|
],
|
||||||
|
"files": [
|
||||||
|
"Queue.ts"
|
||||||
|
]
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user