2021-02-26 01:28:54 -05:00
|
|
|
// mautrix-line - A very hacky Matrix-LINE bridge based on running LINE's Chrome extension in Puppeteer
|
|
|
|
// Copyright (C) 2020-2021 Tulir Asokan, Andrew Ferrazzutti
|
2020-08-17 15:03:10 -04:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2020-08-18 09:47:06 -04:00
|
|
|
export default class TaskQueue {
|
|
|
|
constructor(id) {
|
|
|
|
this.id = id
|
2020-08-17 15:03:10 -04:00
|
|
|
this._tasks = []
|
|
|
|
this.running = false
|
|
|
|
this._wakeup = null
|
|
|
|
}
|
|
|
|
|
2020-08-18 09:47:06 -04:00
|
|
|
log(...text) {
|
|
|
|
console.log(`[TaskQueue/${this.id}]`, ...text)
|
|
|
|
}
|
|
|
|
|
|
|
|
error(...text) {
|
|
|
|
console.error(`[TaskQueue/${this.id}]`, ...text)
|
|
|
|
}
|
|
|
|
|
2020-08-17 15:03:10 -04:00
|
|
|
async _run() {
|
2020-08-18 09:47:06 -04:00
|
|
|
this.log("Started processing tasks")
|
2020-08-17 15:03:10 -04:00
|
|
|
while (this.running) {
|
|
|
|
if (this._tasks.length === 0) {
|
2020-08-18 09:47:06 -04:00
|
|
|
this.log("Sleeping until a new task is received")
|
2020-08-17 15:03:10 -04:00
|
|
|
await new Promise(resolve => this._wakeup = () => {
|
|
|
|
resolve()
|
|
|
|
this._wakeup = null
|
|
|
|
})
|
|
|
|
if (!this.running) {
|
|
|
|
break
|
|
|
|
}
|
2020-08-18 09:47:06 -04:00
|
|
|
this.log("Continuing processing tasks")
|
2020-08-17 15:03:10 -04:00
|
|
|
}
|
|
|
|
const { task, resolve, reject } = this._tasks.shift()
|
|
|
|
await task().then(resolve, reject)
|
|
|
|
}
|
2020-08-18 09:47:06 -04:00
|
|
|
this.log("Stopped processing tasks")
|
2020-08-17 15:03:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @callback Task
|
|
|
|
* @return {Promise<any>}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Push a task to the queue.
|
|
|
|
*
|
|
|
|
* @param {Task} task - The task to run
|
|
|
|
* @return {Promise<any>} - A promise that resolves to the return value of the task
|
|
|
|
*/
|
|
|
|
push(task) {
|
|
|
|
if (!this.running) {
|
|
|
|
throw Error("task queue is not running")
|
|
|
|
}
|
|
|
|
if (this._wakeup !== null) {
|
|
|
|
this._wakeup()
|
|
|
|
}
|
|
|
|
return new Promise((resolve, reject) => this._tasks.push({ task, resolve, reject }))
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start handling tasks
|
|
|
|
*/
|
|
|
|
start() {
|
|
|
|
if (this.running) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this.running = true
|
2020-08-18 09:47:06 -04:00
|
|
|
this._run().catch(err => this.error("Fatal error processing tasks:", err))
|
2020-08-17 15:03:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop handling tasks.
|
|
|
|
*/
|
|
|
|
stop() {
|
|
|
|
if (!this.running) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this.running = false
|
|
|
|
if (this._wakeup !== null) {
|
|
|
|
this._wakeup()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|