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

Timer: After caching timers "to_update", timer:cancel() doesn't apply until next timer:update() #103

Closed
oniietzschan opened this issue May 31, 2018 · 1 comment

Comments

@oniietzschan
Copy link
Contributor

Hey, you may recall this PR: #94

Turns out that it actually introduced a new issue which can be demonstrated with the following code:

local timer = require 'lib.hump.timer'()

local handles = {}
local function newTimer(index)
  local newHandle = timer:after(1, function()
    print('-> excuting timer #' .. index)
    -- Whichever timer executes first will cancel all of the other timers.
    for _, handle in ipairs(handles) do
      timer:cancel(handle)
    end
  end)
  table.insert(handles, newHandle)
end

for i = 1, 10 do
  newTimer(i)
end

print('Updating timers...')
timer:update(1)
print('Finished.')

Expected Result (Which one callback actually executes is contingent, of course.)

Updating timers...
-> excuting timer #9
Finished.

Actual Result (Again, the order here is contingent)

Updating timers...
-> excuting timer #6
-> excuting timer #5
-> excuting timer #2
-> excuting timer #8
-> excuting timer #10
-> excuting timer #1
-> excuting timer #4
-> excuting timer #7
-> excuting timer #3
-> excuting timer #9
Finished.

This issue can be fixed by adding a check like if self.functions[handle] == nil then <continue> end during the primary loop in Timer:update().

I didn't send you a PR because there are a few different styles of achieving a "continue" in Lua, and I wasn't sure which you'd prefer.

@vrld vrld closed this as completed in 0bf301f Jun 17, 2018
@vrld
Copy link
Owner

vrld commented Jun 17, 2018

Thanks!

This issue was closed.
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