-- A UE4SS loop may reject registration, invoke the callback synchronously, or fail to cancel. -- Only a callback whose returned handle belongs to the current registration may touch game state. local Timer={} function Timer.new(ms,register,cancel,clock,log) local self={} local slot,failedAt,delay local function release(handle) if handle and cancel then pcall(cancel,handle) end end function self:stop() local old=slot slot,failedAt,delay=nil,nil,nil if old then release(old.handle) end end function self:ensure(tick) if slot then return slot.handle~=nil end if type(register)~='function' then return false end local now=clock() or os.clock() if failedAt and now>=failedAt and now-failedAt