From f406a31ed2b90d9fc0559ea6b6b22f89c9bb0132 Mon Sep 17 00:00:00 2001 From: Jeff Williams Date: Sat, 12 Oct 2019 12:37:06 -0400 Subject: [PATCH] refactor(jsdoc-task-runner): improve promise handling --- packages/jsdoc-task-runner/lib/task-runner.js | 44 ++++++++++--------- packages/jsdoc-task-runner/lib/task.js | 4 ++ 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/packages/jsdoc-task-runner/lib/task-runner.js b/packages/jsdoc-task-runner/lib/task-runner.js index a18da04c1..77282d1ec 100644 --- a/packages/jsdoc-task-runner/lib/task-runner.js +++ b/packages/jsdoc-task-runner/lib/task-runner.js @@ -60,16 +60,19 @@ module.exports = class TaskRunner extends Emittery { this._unsubscribers.set(task.name, u); } - _addTaskSequenceToQueue(tasks) { + _bindTaskFunc(task) { + return _.bind(task.run, task, this.context); + } + + _createTaskSequence(tasks) { let firstTask; let promise; if (!tasks.length) { - return; + return null; } firstTask = this._nameToTask.get(tasks[0]); - // We don't want to run the first task yet, so we wrap it in another promise. promise = new Promise((resolve, reject) => { this._bindTaskFunc(firstTask)().then(resolve, reject); }); @@ -84,15 +87,7 @@ module.exports = class TaskRunner extends Emittery { } }, promise); - this._queue.add(() => promise); - } - - _addTaskToQueue(task) { - this._queue.add(this._bindTaskFunc(task)); - } - - _bindTaskFunc(task) { - return _.bind(task.run, task, this.context); + return () => promise; } _init(context) { @@ -271,6 +266,8 @@ module.exports = class TaskRunner extends Emittery { let endPromise; let { error, parallel, sequential } = this._orderTasks(); let runningPromise; + let taskFuncs = []; + let taskSequence; // First, fail based on the runner's state. runningPromise = this._rejectIfRunning(); @@ -286,21 +283,28 @@ module.exports = class TaskRunner extends Emittery { this._queue.pause(); for (const taskName of parallel) { - this._addTaskToQueue(this._nameToTask.get(taskName)); + taskFuncs.push(this._bindTaskFunc(this._nameToTask.get(taskName))); + } + + taskSequence = this._createTaskSequence(sequential); + if (taskSequence) { + taskFuncs.push(taskSequence); } - this._addTaskSequenceToQueue(sequential); - endPromise = this._queue.onIdle().then(() => { - let p; + endPromise = this._queue.addAll(taskFuncs).then(() => { + const err = this._error; + + this.end(); - if (this._error) { - p = Promise.reject(this._error); + if (err) { + return Promise.reject(this._error); } else { - p = Promise.resolve(); + return Promise.resolve(); } + }, e => { this.end(); - return p; + return Promise.reject(e); }); this.emit('start'); diff --git a/packages/jsdoc-task-runner/lib/task.js b/packages/jsdoc-task-runner/lib/task.js index e8ca3a741..ef5c4f7ed 100644 --- a/packages/jsdoc-task-runner/lib/task.js +++ b/packages/jsdoc-task-runner/lib/task.js @@ -35,6 +35,8 @@ module.exports = class Task extends Emittery { return this.func(context).then( () => { this.emit('end', this); + + return Promise.resolve(); }, error => { this.emit('error', { @@ -42,6 +44,8 @@ module.exports = class Task extends Emittery { error }); this.emit('end', this); + + return Promise.reject(error); } ); }