-
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
Start loading the nested plugins after the current function completes #29
Conversation
I would also like to pull in #28, as this change would simplify it greatly. |
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.
Stunning work!
I think that with this change the api is even better than before :)
boot.js
Outdated
@@ -165,8 +167,12 @@ Boot.prototype._addPlugin = function (plugin, opts, callback) { | |||
|
|||
const obj = new Plugin(this, plugin, opts, callback) | |||
|
|||
if (current.loaded) { | |||
throw new Error(`Impossible to load (${obj.name}) plugin because the parent (${current.name}) was already loaded`) |
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.
"
instead of (
?
test/callbacks.js
Outdated
s.use(third, done) | ||
t.throws(() => { | ||
s.use(third) | ||
}, 'Impossible to load (third) because the parent (first) already loaded') |
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.
The error is:
throw new Error('Impossible to load (${obj.name}) plugin because the parent (${current.name}) was already loaded')
Why here there is not the was
?
I also like the change and it simplifies the api to use |
👍 |
README.md
Outdated
2. the next `ready` or `after` callback | ||
|
||
In order to handle errors in the loading plugins, you must use the | ||
`.reayd()` method, like so: |
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.
ready
@@ -136,9 +131,9 @@ app.on('start', () => { | |||
------------------------------------------------------- | |||
<a name="use"></a> | |||
|
|||
### app.use(func, [opts], [cb]) | |||
### app.use(func, [opts]) |
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.
Why did you removed [cb]
?
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.
because it's very bad to use it anyway, but we still support it for backward compat.
plugin.js
Outdated
debug('exec', this.name) | ||
|
||
var promise = func(this.server, this.opts, done) | ||
if (promise && typeof promise.then === 'function') { |
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.
We could add a debug log here, something like debug('resolving promise', this.name)
.
Same few lines below: debug('promise resolved', this.name)
.
Everything looks good!! A test that will fail, and we should be able to detect it, is: test('promise long resolve', (t) => {
t.plan(1)
const app = boot()
var insidePromise = false
new Promise((resolve, reject) => {
setTimeout(() => {
resolve()
}, 500)
})
.then(() => {
app.use((s, opts, done) => {
insidePromise = true
done()
})
})
app.ready(function () {
t.ok(insidePromise)
})
}) If we can't detect it we should document that this behaviour is not supported. |
@delvedor that test will fail right now. A change that we can do is in https://github.com/mcollina/avvio/blob/master/boot.js#L110, basically not reinitializing avvio if the root plugin has finished loading. One more question.. should we add |
@delvedor I implemented detection for that condition, you might want to remove. |
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.
LGTM
I think we should autostart. |
This resolves fastify/fastify#329, but it will be a major version for avvio.
It is impossible for us to retain the context of which plugin is loaded in the current scenario:
As in this case,
third
will be loaded as a child ofsecond
, not as a child offirst
. This is typically not a problem, but it messes with the chaining of prefixes ({prefix: '/path'
}) in fastify.This change makes the callbacks to
use
(register
in fastify) completely irrelevant within the plugins, and it makes the following code not work anymore:Instead this must be replaced by:
The callback to
use
is still supported, but probably is only needed as the top-level, and I think we should stop recommending users to use it (in the plugins readme and the docs).Another PR will be needed in fastify after this is released.
cc @delvedor @StarpTech
(I have also added debug and a lot of debug points, because I am tired of adding those in via console.log every time there is an hard issue here).
The docs would need to updated.