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

Recognize window kill and/or browser back button #210

Open
pratapsu opened this issue Jul 10, 2018 · 5 comments
Open

Recognize window kill and/or browser back button #210

pratapsu opened this issue Jul 10, 2018 · 5 comments
Labels

Comments

@pratapsu
Copy link

I have written my web app entirely using vecty & gopherjs. It connects via websockets/rpc to a backend server. So far, the experience has been quite satisfying.

I have the need to reset a few data structures in the backend server when any of the following events happen:

  • The current window is killed by the user explicitly
  • The user moves focus from the current window to older window on the stack via the "Back" button in the browser.
  • The user quits the browser

I'd like to do this while staying pure and using vecty/gopherjs only, preferably. I noted there are APIs for "beforeunload" events, but it is not immediately obvious to me how I'd go about catching that event. If this is not possible with vecty alone, I'm also open to a jQuery based solution.

ow would I do this? Can you help?

Thanks,

Pratap

@ghost
Copy link

ghost commented Jul 15, 2018 via email

@pratapsu
Copy link
Author

I arrived at a similar workaround too ... I poll my cached state every 5 seconds to update the true state stored on the server. As you also point out, this is clearly inefficient. Perhaps some one on this forum will shed some light.

@NateWilliams2
Copy link

I'm facing the same situation now, does anyone have updated information on using beforeunload or some other event listener to do this? Thanks.

@pdf
Copy link
Contributor

pdf commented Jul 15, 2020

Totally untested, but something like:

import "github.com/gopherjs/vecty/event"

...
event.BeforeUnload(func(evt *vecty.Event) {
    evt.PreventDefault()
    // do your handling here
}
...

Seems like it should do what you want, though I've never used it, and I don't know what particular semantics browsers may apply to this event handler.

@NateWilliams2
Copy link

NateWilliams2 commented Jul 15, 2020

Thanks for the idea, I still was unable to get my handling there to trigger. I came up with something that just uses syscall/js manually instead:

func BeforeUnload(close func()) {
	var unloadFunc js.Func
	unloadFunc = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
		close()
		unloadFunc.Release() // release the function if the button will not be clicked again
		return nil
	})
	js.Global().Call("addEventListener", "beforeunload", unloadFunc)
}

There's probably just something about event.BeforeUnload I'm not getting right.. But for now this is working for me

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