vrld / hump

LÖVE Helper Utilities for Massive Progression
http://hump.readthedocs.org
1.09k stars 154 forks source link

Timer Bug Fix - Fixed indeterminate behavior of new timers. #94

Closed oniietzschan closed 6 years ago

oniietzschan commented 6 years ago

Fixed bug where behavior of new timers added during iteration through timer handles was indeterminate.

On master, consider that while iterating through self.functions, we use pairs.

  -- timer.lua on master

  for handle in pairs(self.functions) do
    -- <update handle...>
  end

So, if we have a timer that does something like this:

local timerOne = Timer.after(1, function()
  local timerTwo = Timer.after(1, function() print('does something else') end)
end

Now, if we run `Timer.update(1), there are two possibilities.

A. timerTwo is updated later.

  1. timerOne will finish.
  2. timerOne will execute and create timerTwo, it will be added to somewhere considered "before" timerOne in pairs(self.functions).
  3. timerTwo will not be updated in this call of Timer.update.

B. timerTwo is updated immediately.

  1. timerOne will finish.
  2. timerOne will execute and create timerTwo, it will be added to somewhere considered "after" timerOne in pairs(self.functions).
  3. timerTwo will be updated in this call of Timer.update, thus printing "does something else" immediately.

To fix this issue, before starting to iterate through self.functions, we gather together all of the timer handles which currently exist, so that new timers will not be updated until the next call of Timer.update().

vrld commented 6 years ago

Thanks again :)