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

vMix.connected() returns false in connect listener callback #16

Open
hrueger opened this issue May 29, 2022 · 2 comments
Open

vMix.connected() returns false in connect listener callback #16

hrueger opened this issue May 29, 2022 · 2 comments

Comments

@hrueger
Copy link

hrueger commented May 29, 2022

Hi @jensstigaard,
Thanks for this library. It makes interfacing with vMix very easy.
I did find a small bug though:

this.vMix = new ConnectionTCP(this.settings?.ip || "localhost", { autoReconnect: true });
this.vMix.on("connect", () => { 
    console.log(this.vMix.connected())
}

prints false. The reason I found this is that I do some initialization stuff once vMix is connected. Everything I send to vMix goes through a custom send function which does some logging, then checks whether it is actually connected to vMix and if yes, sends the data.
However, I need to skip this check when I'm inside the callback because the connected() method returns false there.

Best regards

@jensstigaard
Copy link
Owner

jensstigaard commented May 30, 2022

I think that I have found the cause of the problem.. but I do not have the expertise to solve it perfectly. It all comes down to the order of the execution of the listener callbacks. It seems like the custom registered on('connect',...) listener is executed before the internal one, where the variable holding the state is updated.. I however have a quickfix to this issue, wrapping the custom registered listeners with a setTimeout(() => ..., 0) to let the event queue finish up its tasks before firing off the custom listeners.
Code example:

const connection = new ConnectionTCP('localhost', { debug: true })
// Add on connect listener
connection.on('connect', () => {
  console.log('Received connected event clean', connection.connected()) // false
  setTimeout(() => {
    console.log('Received connected event wrapped', connection.connected()) // true
  }, 0)
})

I am not entirely sure whether it is a good idea to implement the quickfix... so I will reflect a liittle on it.

@hrueger
Copy link
Author

hrueger commented Jun 9, 2022

Maybe we could also use setImmediate instead of setTimeout as it des not check timers and therefore should be a bit faster?

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

2 participants