Skip to content
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

Add support for ready to return a promise. #37

Merged
merged 2 commits into from
Dec 20, 2017
Merged

Add support for ready to return a promise. #37

merged 2 commits into from
Dec 20, 2017

Conversation

coreyfarrell
Copy link
Contributor

No description provided.

README.md Outdated
@@ -234,9 +234,11 @@ The callback changes basing on the parameters your are giving:
3. If two parameters are given to the callback, the first will be the `error` object, the second will be the `done` callback.
4. If three parameters are given to the callback, the first will be the `error` object, the second will be the top level `context` unless you have specified both server and override, in that case the `context` will be what the override returns, and the third the `done` callback.

If no callback is provided `ready` will return a Promise that is resolved or rejected once plugins and `after` calls are completed. No data is provided to the `.then` callback, if an error occurs it is provided to the `.catch` callback.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should provide the context to resolve.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated my working copy code to do this. I'll update the docs and push the update once it's decided what should be returned from app.ready(callbackForm).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has not been updated

@allevo
Copy link
Member

allevo commented Dec 6, 2017

Sorry for the intrusion

I'm 👍 for moving ready for a better async support

I'm 👎 for return different kind of type depending on a parameter presence

@mcollina
Copy link
Member

mcollina commented Dec 6, 2017

@allevo you propose to return null if we are not generating a Promise? That'll be a semver-major change.

@allevo
Copy link
Member

allevo commented Dec 6, 2017

That'll be a semver-major change.

Yeah. And it could be a problem. But returning different type is very a bad thing. null could be a solution.

For example the mongodb library return a Promise or undefined (no return statement). See here for example

@mcollina
Copy link
Member

mcollina commented Dec 6, 2017

If we do a semver-major, we might want to do some more work here to better support async/await overall.

Copy link
Member

@delvedor delvedor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok with this change, but we should add a check to verify that a user does not register other plugins after the ready execution.

@coreyfarrell
Copy link
Contributor Author

Should I make ready return null or undefined when not returning a Promise?

@delvedor So I make sure I understand correctly you want me to add a check to Boot.prototype.use:

if (this.booted) {
  throw new Error('plugin cannot be loaded after ready')
}

Should that be done in a separate commit so it appears in git log --oneline?

@mcollina
Copy link
Member

Should I make ready return null or undefined when not returning a Promise?

Yes, I'd prefer undefined.

@coreyfarrell the check that @delvedor wants is already part of https://github.com/mcollina/avvio/pull/38/files#diff-fe60a8aed4b3103661f9d88361346b0dR185. They will be released together anyway. Go ahead and just do the change on ready().

@coreyfarrell
Copy link
Contributor Author

Returning undefined helped me catch the fact that I missed the wrap() function while writing this patch. I've fixed that for ready but it also shows that tests/async-await.js fails to test the wrap feature using:

const app = {}
boot(app)

Right now I'm manually switching the tests in so I can find/fix the issues with wrap async. I haven't looked into what it would take to have async-await.js run each test twice (once with and once without wrap).

@coreyfarrell
Copy link
Contributor Author

I'm now realizing github probably didn't send any notifications when I updated the PR yesterday. I've addressed feedback and fixed issues found by testing with wrapped objects.

README.md Outdated
@@ -234,9 +234,11 @@ The callback changes basing on the parameters your are giving:
3. If two parameters are given to the callback, the first will be the `error` object, the second will be the `done` callback.
4. If three parameters are given to the callback, the first will be the `error` object, the second will be the top level `context` unless you have specified both server and override, in that case the `context` will be what the override returns, and the third the `done` callback.

If no callback is provided `ready` will return a Promise that is resolved or rejected once plugins and `after` calls are completed. No data is provided to the `.then` callback, if an error occurs it is provided to the `.catch` callback.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has not been updated

boot.js Outdated
@@ -229,8 +228,23 @@ Boot.prototype.close = function (func) {
}

Boot.prototype.ready = function (func) {
this._readyQ.push(func)
return this
if (func) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would check if this is a function and throw if it exists but it is not.

@@ -16,10 +16,8 @@ test('chainable standalone', (t) => {
done()
}).ready(function () {
t.pass('we are ready')
}).use(function (ctx, opts, done) {
t.pass('3rd plugin')
done()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you restore this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ready no longer returns this so the test now verifies that it is not chainable.

If you are concerned about testing that use after ready does the right thing I believe other tests do this, ready adds at the end of the queue from after-and-ready.test.js for example calls ready before calling use.

Please let me know if I'm misunderstanding your finding.

@@ -38,10 +36,8 @@ test('chainable automatically binded', (t) => {
done()
}).ready(function () {
t.pass('we are ready')
}).use(function (ctx, opts, done) {
t.pass('3rd plugin')
done()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you restore this?

@mcollina mcollina merged commit a1668fc into fastify:master Dec 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants