Working with media API is presenting some unique challenges (pun intended). Looking for thoughts on how to solve, especially the enforcement of unique id’s on htmlmediaelements.
The background: HtmlMediaElements are objects, and we need the instance to play, pause, etc, in JavaScript. I’m not sure if other specialty areas handle this differently, but standard practice is to put a unique id on each element, then us Document,getElementById.
Since we have to send the id out a port to play, pause, load, etc, it’s important to enforce unique ids, and is correctness, or else nothing will happen, or will happen to the wrong media element.
My current solution is to use an opaque MediaState type, which requires a string to create (which is then used as the Id). This enforced using the correct id and makes the api a little nicer, as my play/pause ports take one of these opaque types and get the id out.
However, I haven’t yet figured out a way to enforce uniqueness of ids, except to recommend using a dictionary to hold multiple states if using multiple media elements (with the key for each being the id).
I’m also more worried after an incident yesterday where a colleague copied and pasted some html I had given him previously to put two players on the same page. He ended up with two players with two identical ids. And then he spent really a lot of time trying to figure out what was going on before reaching out to me.
My concern is that the opaque type approach could encourage that kind of behavior…people put one in their model and reuse it.
So any thoughts on both enforcing the creation of unique ids and discouraging their reuse?
PS I’ve thus far been shy about using a Task.random approach, as it feels to me as if it may make the API too complicated. At some point, it feels to me, simply nagging people repeatedly in documentation is better than making them jump through hoops to absolutely enforce it.