Skip to content

proposal: add a way to hook into mainFinished #525

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

Closed
broady opened this issue Sep 22, 2016 · 3 comments
Closed

proposal: add a way to hook into mainFinished #525

broady opened this issue Sep 22, 2016 · 3 comments
Labels

Comments

@broady
Copy link

broady commented Sep 22, 2016

It might sound strange, but I'd like some way to hook into mainFinished - kind of as a finalizer.

The Node.js runtime can block program termination with object refs. See net.Socket.ref/net.Socket.unref for example. This is useful for shutting down keep-alive connections.

I'd like to call "unref" on mainFinished to close down any sockets when all of the Go code has finished executing. I can explain in more detail if it still doesn't make sense.

runtime.AddProgramFinalizer(func())
runtime.OnMainFinished(func())

Essentially:

$mainFinished = true;
for (var i = 0; i < $mainFinishers.length; i++) {
  $mainFinishers[i]();
}
@dmitshur
Copy link
Member

Can this be done with plain Go code? For example:

package main

func main() {
    err := run()
    if err != nil {
        log.Println(err)
    }

    // Run "finalizer" code here.
}

If there's a reason that can't be done, then my next question is...

Can we use an existing API (which GopherJS doesn't yet implement) and achieve this by implementing that API? Instead of creating new/custom API that doesn't exist in real Go standard library. What about runtime.SetFinalizer?

@flimzy
Copy link
Member

flimzy commented Dec 23, 2016

Related to, or possible duplicate of #383.

This is also the main missing feature preventing running standard library tests in the browser (requested again here: #558 (comment) )

And to answer your question, @shurcooL, I don't think it can be done with plain go code, because, if I understand the request (and certainly for #383), it needs to be visible to the external JS environment.

@nevkontakte
Copy link
Member

This proposal seems very similar to #383, so I'm closing this issue in favor of the earlier one to avoid splitting the discussion. I've copied the text of the first comment into #383 for posterity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants