mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2024-12-20 19:06:06 +01:00
35 lines
785 B
JavaScript
35 lines
785 B
JavaScript
'use strict';
|
|
|
|
const EventEmitter = require('events');
|
|
|
|
class Task extends EventEmitter{
|
|
constructor(execution){
|
|
super();
|
|
if(typeof execution !== 'function') {
|
|
throw 'execution must be a function';
|
|
}
|
|
this._execution = execution;
|
|
}
|
|
|
|
execute(now) {
|
|
let exec;
|
|
try {
|
|
exec = this._execution(now);
|
|
} catch (error) {
|
|
return this.emit('task-failed', error);
|
|
}
|
|
|
|
if (exec instanceof Promise) {
|
|
return exec
|
|
.then(() => this.emit('task-finished'))
|
|
.catch((error) => this.emit('task-failed', error));
|
|
} else {
|
|
this.emit('task-finished');
|
|
return exec;
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = Task;
|
|
|