Skip to content

Instantly share code, notes, and snippets.

@gizzmo
Created March 22, 2017 19:42
Show Gist options
  • Save gizzmo/eb90ea208ea409d8f3f033f250e5ee96 to your computer and use it in GitHub Desktop.
Save gizzmo/eb90ea208ea409d8f3f033f250e5ee96 to your computer and use it in GitHub Desktop.
Defer a function's execution if in combat lock down, until combat is over.
-- Get the addon namespace
local ADDON_NAME, private = ...
local deferframe = CreateFrame('Frame')
deferframe.queue = {}
local function runDeferred(thing)
local thing_t = type(thing)
if thing_t == 'string' and private[thing] then
private[thing](private)
elseif thing_t == 'function' then
thing(private)
end
end
-- This method will defer the execution of a method or function until the
-- player has exited combat. If they are already out of combat, it will
-- execute the function immediately.
function private:Defer(...)
for i = 1, select('#', ...) do
local thing = select(i, ...)
local thing_t = type(thing)
if thing_t == 'string' or thing_t == 'function' then
if InCombatLockdown() then
deferframe.queue[#deferframe.queue + 1] = thing
else
runDeferred(thing)
end
else
error('Invalid object passed to \'Defer\'')
end
end
end
deferframe:RegisterEvent('PLAYER_REGEN_ENABLED')
deferframe:SetScript('OnEvent', function(self, event, ...)
for idx, thing in ipairs(deferframe.queue) do
runDeferred(thing)
end
table.wipe(deferframe.queue)
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment