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

Should it be possible to call close more than once? #57

Open
StarpTech opened this issue Mar 31, 2018 · 15 comments
Open

Should it be possible to call close more than once? #57

StarpTech opened this issue Mar 31, 2018 · 15 comments

Comments

@StarpTech
Copy link
Member

Discussed first in #56

@mcollina
Copy link
Member

I'm 👎 . It should error on the second time (in the callback, reject in the promise). I probably added that part in a haste. That whole logic could be simplified.

@StarpTech StarpTech changed the title Should it be possible to call close more than twice? Should it be possible to call close more than once? Mar 31, 2018
@StarpTech
Copy link
Member Author

I'm 👎 too

We should remove the queue mechanism in close and provide a single simple callback.

@StarpTech
Copy link
Member Author

StarpTech commented Mar 31, 2018

@mcollina in the current state a second close handler can't receive an err correct?

'use strict'

const avvio = require('.')()

avvio
  .ready(function () {
    console.log('application booted!')
    avvio.onClose((context, done) => {
      done(new Error('test'))
    })
    avvio.close((err, cb) => {
      console.log(err, '1111') // error, '1111'
      cb(err)
    })
    avvio.close((err) => {
      console.log(err, '2222') // null '2222'
    })
  })

We could release it as bugfix when we adjust the code as @delvedor #56 (comment) suggested because the err must be handled by the user since the start. WDYT?

@StarpTech
Copy link
Member Author

As the next step, we could remove the queue completely and this would be a major change.

@StarpTech
Copy link
Member Author

Do you working on this or can I create a PR?

@mcollina
Copy link
Member

mcollina commented Apr 5, 2018 via email

@StarpTech
Copy link
Member Author

My plan would be:

Round 1

  • Delvedor suggestion
  • It's minor change because the error of close(err) could never be true but is present in the parameters.

Round 2

  • Remove call of ready() inside close()
  • Don't append close handler to closeQ queue support only a single callback per app.
  • Major change

@delvedor
Copy link
Member

delvedor commented Apr 8, 2018

Remove call of ready() inside close()

Why remove the ready call?
If you have already started the app nothing will change, if the app is not yet started why call close in the first place?

Don't append close handler to closeQ queue support only a single callback per app.

Maybe I'm missing something here. What do you mean?


In my opinion call close more than once must return an error and should not be allowed.

@StarpTech
Copy link
Member Author

StarpTech commented Apr 8, 2018

Why remove the ready call?

For me, it's wrong that close handle more than it should. If you want to bootstrap the app call start or enable autostart period.

Maybe I'm missing something here. What do you mean?

It should only allow setting a single callback like

avvio.close(() => {}) // OK
avvio.close(() => {}) // error: close was already called

so, in that case, we don't need a queue.

@delvedor
Copy link
Member

delvedor commented Apr 8, 2018

For me, it's wrong that close handle more than it should. If you want to bootstrap the app call start or enable autostart period.

I strongly suggest you to moderate your tone.
While I can agree that close should just call the close queue, I think that is safer call ready, because call close will run all the onClose functions, and it can happen that one of them needs that a plugin has been started before to use them.

so, in that case, we don't need a queue.

There is not difference between the close queue and the onClose queue, we put the close callback at the end of the onClose queue just to notify the user when the closing process has finished.

@StarpTech
Copy link
Member Author

StarpTech commented Apr 8, 2018

I strongly suggest you to moderate your tone.

Don't be so pedantic! This is a technical discussion and I just make my point clear.

While I can agree that close should just call the close queue, I think that is safer call ready, because call close will run all the onClose functions, and it can happen that one of them needs that a plugin has been started before to use them.

Then you will mix up again two different things starting and closing. As a user, I expect that close will close my application and not start the plugin queue. When the plugin queue wasn't started then there is nothing to care about and when yes it should be fine that close() can only be called after ready().

@delvedor
Copy link
Member

delvedor commented Apr 8, 2018

What's your problem?

Again.

Then you will mix up again two different things starting and closing. As a user, I expect that close will close my application and not start the plugin queue. When the plugin queue wasn't started then there is nothing to care about.

It is not a matter of mixing up, in theory a user should not call close before calling ready, it is useless and we all agree on this.
The thing is that if a user calls close in a bad moment (during the application boot for example) we cannot run the close queue while the others plugins are still loading. @mcollina made this very clear in #59 (comment).
That ready is just to avoid this case, if you call close in the wrong moment, it will start at the right time.

@StarpTech
Copy link
Member Author

StarpTech commented Apr 8, 2018

It is not a matter of mixing up, in theory a user should not call close before calling ready, it is useless and we all agree on this.

  • Agree and this can be avoided by a check in close()

The thing is that if a user calls close in a bad moment (during the application boot for example) we cannot run the close queue while the others plugins are still loading. @mcollina made this very clear in #59

  • Yes, I understand this case but this can be avoided when we agree that close must be called after ready any other case is unpredictable and this change sounds reasonable. We could throw an error when the user call close before the queue drains.

@StarpTech
Copy link
Member Author

StarpTech commented Apr 8, 2018

  • Throw error when a user try to call close before ready (or before plugin queue drains)
  • Document that close can only be called after ready
  • Don't call ready() inside close
  • Throw error when close is called twice
  • Don't append close handler to onClose queue and provide only a single callback handler

WDYT?

@mcollina
Copy link
Member

mcollina commented Apr 9, 2018

This should work:

  1. if we are booting, then wait for the boot to finish (call .ready()).
  2. if we are not loading at all, do not call ready(), but rather make future ready()  calls error if we are already closing.
  3. error (via callback or reject) if close is called twice.

We need to call .ready() while we are booting to support always calling .close() in tests.

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

No branches or pull requests

3 participants