-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Close queue #16
Merged
Merged
Close queue #16
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
78996a7
Updated travis
delvedor b97bd69
Moved plugin to its own file
delvedor 6b16e9a
Bumped dependencies
delvedor 88d3856
Added 'close' flow
delvedor 8300e79
Updated test
delvedor d5624d0
Updated README.md
delvedor 1fb1125
Close call order
delvedor c75944d
Expose close methods to custom instance
delvedor d40f36a
Updated test
delvedor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,13 @@ | ||
language: node_js | ||
|
||
node_js: | ||
- "4" | ||
- "8" | ||
- "7" | ||
- "6" | ||
- "5" | ||
- "4" | ||
|
||
notifications: | ||
email: | ||
on_success: never | ||
on_failure: always |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
'use strict' | ||
|
||
const fastq = require('fastq') | ||
|
||
function Plugin (parent, func, opts, callback) { | ||
this.func = func | ||
this.opts = opts | ||
this.callback = callback | ||
this.deferred = false | ||
this.onFinish = null | ||
this.parent = parent | ||
|
||
this.q = fastq(parent, loadPlugin, 1) | ||
this.q.pause() | ||
|
||
// always start the queue in the next tick | ||
// because we try to attach subsequent call to use() | ||
// to the right plugin. we need to defer them, | ||
// or they will end up at the top of _current | ||
process.nextTick(this.q.resume.bind(this.q)) | ||
} | ||
|
||
Plugin.prototype.exec = function (server, cb) { | ||
const func = this.func | ||
this.server = this.parent.override(server, func, this.opts) | ||
func(this.server, this.opts, cb) | ||
} | ||
|
||
Plugin.prototype.finish = function (err, cb) { | ||
const callback = this.callback | ||
// if 'use' has a callback | ||
if (callback) { | ||
callback(err) | ||
// if 'use' has a callback but does not have parameters | ||
cb(callback.length > 0 ? null : err) | ||
} else { | ||
cb(err) | ||
} | ||
} | ||
|
||
// loads a plugin | ||
function loadPlugin (toLoad, cb) { | ||
const last = this._current[0] | ||
// place the plugin at the top of _current | ||
this._current.unshift(toLoad) | ||
toLoad.exec((last && last.server) || this._server, (err) => { | ||
if (err || !(toLoad.q.length() > 0 || toLoad.q.running() > 0)) { | ||
// finish now, because there is nothing left to do | ||
this._current.shift() | ||
toLoad.finish(err, cb) | ||
} else { | ||
// finish when the queue of nested plugins to load is empty | ||
toLoad.q.drain = () => { | ||
this._current.shift() | ||
toLoad.finish(null, cb) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
module.exports = Plugin | ||
module.exports.loadPlugin = loadPlugin |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should call
unshift()
here. More or less, the latest you register a shutdown part, the sooner it will be executed. This is the inverse of the start phase.