I've encountered an error that occurs on line 150:
TypeError: Cannot read property 'options' of undefined
This seems to happen intermittently, and I have yet to identify reliable repro steps. It seems to happen when performing multiple badge
calls in succession. It also seems more likely to happen when the updates occur when the page is invisible.
My theory as to why this is occurring, and also why it's hard to repro, is that it's a race condition to access the first element of the queue
array.
If _queue.shift();
completes and the resulting queue is empty before line 150 executes, that would throw the error above.
Here is what I think is happening, although I'm unsure.
Let's say I have an empty queue, and _lastBadge
is false
as set in init. Now I call badge(1)
, which pushes a set of options, which we will call 'A' into the queue and triggers icon.start
. This kicks of an animation that lasts, let's say 100ms, using setTimeout
to advance the animation at set intervals.
Now before A is complete, say 10ms in, we kick off another animation by calling badge(2)
, which we will call 'B'. This is scheduled to complete 110ms into our experiment.
Now at 100ms, A completes, and the finished
callback is called. This function sets _lastBadge
to A and shifts A out of the queue, leaving our queue containing 1 element, B.
The last thing our callback does is call icon.start
again, which now triggers the animation.runcall on line 149, using animation A, which is our current value of
_lastBadge`. When this is complete, at 200ms, we'll call animation.run again, with the first item in the queue.
Now fastforward 10ms and our initial B animation completes at 110ms. This also calls finished, which sets _lastBadge
to B, shifts B off the queue (we now have an empty queue) and calls icon.start, which does nothing since _queue.length
is 0.
Now fastforward to 200ms in, and the animation that was initiated by the finished callback completes. Instead of calling the finished callback, it calls animation.run with _queue[0].options as the first argument. But wait, _queue is empty!
Here is a diagram that illustrates what I'm thinking:
bug