Notes on code, technology, caffeine and everything in between.

Multitasking

Dec 27, 2022
tl;dr: I've had a hard time creating multiple instances.

Welcome back to part 3 of my stream deck plugin development journey. After I managed to use my canvas code and actually update the display, I thought I had done the most difficult part. But how wrong I was. After implementing input fields in the property inspector to change HEX colors of the plugin, I immediately found out, that the plugin is only run as a single instance by stream deck. Even if you have multiple buttons, it is always a single instance.

Changing a color for one button therefore means changing a color on every button with the clock displayed. And even worse, property inspector (PI) stores the settings per button context, so you could also have a mismatch between actual settings and displayed values.

It took me a whole day to figure out on how to resolve this. I was trying to mimic the nice code style of the analog clock plugin using local cache and other fancy stuff, but after some hours of frustration, I simply gave up on trying that approach. But at least I found out, that every button has its own context and also has somewhere its own properties stored.

Also, I got very frustrated, because I hadn’t found a way of storing a timer ID of setInterval() that I could access in some other function to destroy the clock when the button is removed by user to prevent too many timers running endlessly.

Fiddling around a bit more and in a very bad mood I decided to go the old school way of defining a global array of objects that stores all relevant properties of every single button and that I can access globally. That did the trick. Now I can define as many on air clock buttons I want and feed them with different colors or simply remove them without producing potential memory leaks or other bad side effects.

Don’t get me wrong. I was less frustrated with Elgato’s moderately good SDK documentation than with myself for not understanding the sample code in a way that I could use it for my application.

But now it works! I can now continue making the settings in property inspector a bit more user-friendly.

Multiple buttons with different colors

Multiple buttons don’t lead to multiple breakdowns anymore!