gereleth / hexapipes

Hexagonal pipes puzzles
https://hexapipes.vercel.app/hexagonal/5
MIT License
187 stars 19 forks source link

Pause timer when tab is hidden #37

Closed joshwilsonvu closed 2 years ago

joshwilsonvu commented 2 years ago

Resolves #35. Regarding the last comment, I believe visibilitychange covers every relevant case:

This event fires with a visibilityState of hidden when a user navigates to a new page, switches tabs, closes the tab, minimizes or closes the browser, or, on mobile, switches from the browser to a different app. Transitioning to hidden is the last event that's reliably observable by the page, so developers should treat it as the likely end of the user's session (for example, for sending analytics data).

I got pausing timers working in Safari and Chrome. I noticed a small thing, not a blocker—when you're working on a puzzle of one size and click a link to load a puzzle of a new size, the current puzzle won't be paused, I'm confused why. After that though, the back and forward buttons pause and unpause the puzzles correctly. But for every other case I tested, it worked like a charm!

Please let me know if it would be helpful to make any changes. Thanks!

vercel[bot] commented 2 years ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
hexapipes ✅ Ready (Inspect) Visit Preview Aug 6, 2022 at 6:29PM (UTC)
gereleth commented 2 years ago

Thanks for the PR =).

Navigating to another puzzle size is done client-side, so maybe it doesn't count as leaving the page for the purposes of this event. You could maybe dispatch a pause event from the Puzzle's onDestroy hook and handle it in the PuzzleWrapper.

I'll review and try this out after I'm done with revamping controls.

joshwilsonvu commented 2 years ago

Thanks for the idea, I'm a bit new to Svelte. Made the change and I think that took care of any logic issues, should be ready to go.

gereleth commented 2 years ago

Thanks again, the changes should be live now.

There was a small problem where puzzles started before this change would show "Paused" instead of a timer when you open them because their pausedAt was undefined. But I think I fixed it).

I mentioned you in the changelog (an obscure place, I know). If you have suggestions for how I should do contributor acknowledgements then please let me know)).

joshwilsonvu commented 2 years ago

Yep, I saw the fixes you made, looked good. I'm happy with the changelog acknowledgement, and thanks for taking the time to review and merge the PR!