-
Notifications
You must be signed in to change notification settings - Fork 40
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
Added an autostart flag to defer loading #38
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,6 +88,10 @@ function Boot (server, opts, done) { | |
return instance | ||
} | ||
|
||
if (opts.autostart !== false) { | ||
opts.autostart = true | ||
} | ||
|
||
server = server || this | ||
|
||
this._server = server | ||
|
@@ -98,6 +102,7 @@ function Boot (server, opts, done) { | |
this.once('start', done) | ||
} | ||
|
||
this.started = false | ||
this.booted = false | ||
|
||
this._readyQ = fastq(this, callWithCbOrNextTick, 1) | ||
|
@@ -114,34 +119,34 @@ function Boot (server, opts, done) { | |
} | ||
this._thereIsCloseCb = false | ||
|
||
// we init, because we need to emit "start" if no use is called | ||
this._init() | ||
this._doStart = null | ||
const main = new Plugin(this, (s, opts, done) => { | ||
this._doStart = done | ||
if (opts.autostart) { | ||
this.start() | ||
} | ||
}, opts, noop) | ||
|
||
Plugin.loadPlugin.call(this, main, (err) => { | ||
debug('root plugin ready') | ||
if (err) { | ||
this._error = err | ||
if (this._readyQ.length() === 0) { | ||
throw err | ||
} | ||
} else { | ||
this._readyQ.resume() | ||
} | ||
}) | ||
} | ||
|
||
inherits(Boot, EE) | ||
|
||
Boot.prototype._init = function () { | ||
if (this.booted) { | ||
throw new Error('root plugin has already booted') | ||
} | ||
Boot.prototype.start = function () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be added to Also if this.started is already true do we want to run this._doStart again? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don’t think we should add it to wrap? Why would you want to do it? An avvio instance can be booted only once. Otherwise too many edge cases happens (this is discussed elsewhere). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nevermind I looked at the docs again it explicitly states which functions are exposed. It needs to be updated to list |
||
this.started = true | ||
|
||
if (this._current.length === 0) { | ||
const main = new Plugin(this, function root (s, opts, done) { | ||
// we need to wait any call to use() to happen | ||
process.nextTick(done) | ||
}, {}, noop) | ||
Plugin.loadPlugin.call(this, main, (err) => { | ||
debug('root plugin ready') | ||
if (err) { | ||
this._error = err | ||
if (this._readyQ.length() === 0) { | ||
throw err | ||
} | ||
} else { | ||
this._readyQ.resume() | ||
} | ||
}) | ||
} | ||
// we need to wait any call to use() to happen | ||
process.nextTick(this._doStart) | ||
} | ||
|
||
// allows to override the instance of a server, given a plugin | ||
|
@@ -176,8 +181,9 @@ Boot.prototype._addPlugin = function (plugin, opts, callback) { | |
opts = opts || {} | ||
callback = callback || null | ||
|
||
// we reinit, if use is called after emitting start once | ||
this._init() | ||
if (this.booted) { | ||
throw new Error('root plugin has already booted') | ||
} | ||
|
||
// we always add plugins to load at the current element | ||
const current = this._current[0] | ||
|
@@ -230,6 +236,7 @@ Boot.prototype.close = function (func) { | |
|
||
Boot.prototype.ready = function (func) { | ||
this._readyQ.push(func) | ||
this.start() | ||
return this | ||
} | ||
|
||
|
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.
Unless this._doStart is called with an error argument this is unreachable. I found this while working on a patch to increase test coverage.
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.
Not sure what happened, my code selection got moved up a couple lines. should have selected the lines within
if (err) {...}
.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.
This is not correct. Here we receive the errors of all the loaded plugins.
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 tried to get an error to reach this block, I was unsuccessful so I analyzed the code. I'm confident that it's unreachable but that's not important to this review.