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

initialize method runs only when browser tab is active? #731

Closed
michelson opened this issue Oct 20, 2023 · 2 comments
Closed

initialize method runs only when browser tab is active? #731

michelson opened this issue Oct 20, 2023 · 2 comments

Comments

@michelson
Copy link

Hi there, I'm developing a chat application with hotwire and stimulus, and I have a controller that is initialized for every received message, the messages are appended via turbo_streams. The problem is that if the user is not viewing the browser tab the received messages are not connecting with the stimulus controller; at least that is my guess. My other guess is that it could be that the turbo_streams are not updating the DOM if the browser tab is not active and there is some kind of queuing until the browser is active..

Basically, what the stimulus controller does is make a sound whenever the message is received.

Proof, please turn the audio on.
https://github.com/hotwired/stimulus/assets/11976/9ec6c7e2-2e7b-4f70-b79a-6bce550070a3

The controller is something like this:

export default class extends Controller {
  initialize() {
    const dataset = this.element.dataset
      if(dataset.viewerType !== dataset.author ){
        this.playSound();
    }
  }
}

I would appreciate any hints. Thanks.

@lb-
Copy link
Contributor

lb- commented Oct 20, 2023

I don't think this would have anything to do with Stimulus or Turbo, the browser can choose to ignore JS in non-active tabs.

Maybe this is due to Turbo or some other code relying on setInterval/setTimeout to do updates. In which case these will be actively throttled when the tab is inactive.

https://isamatov.com/prevent-timers-stopping-javascript/

iOS Safari will be even more aggressive and completely block some JS if the tab isn't active.

You may need to explore web workers or some other browser API like push events.

@brunoprietog
Copy link
Collaborator

I guess we can close this, since it was finally a Turbo problem and is now fixed

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

No branches or pull requests

3 participants