Quantcast

Update AddonCore to latest version

James Whitehead II [11-24-10 - 19:07]
Update AddonCore to latest version
Filename
AddonCore.lua
diff --git a/AddonCore.lua b/AddonCore.lua
index 66ba437..d524340 100644
--- a/AddonCore.lua
+++ b/AddonCore.lua
@@ -39,7 +39,9 @@ if EMERGENCY_DEBUG then
     setmetatable(addon, {
         __index = function(t, k)
             local value = rawget(private, k)
-            print(addonName, "INDEX", k, value)
+            if type(value) == "function" then
+                print("CALL", addonName .. "." .. tostring(k))
+            end
             return value
         end,
         __newindex = function(t, k, v)
@@ -50,22 +52,6 @@ if EMERGENCY_DEBUG then
 end

 --[[-------------------------------------------------------------------------
---  Print/Printf support
--------------------------------------------------------------------------]]--
-
-local printHeader = "|cFF33FF99%s|r: "
-
-function addon:Printf(msg, ...)
-    msg = printHeader .. msg
-    local success, txt = pcall(string.format, msg, addonName, ...)
-    if success then
-        print(txt)
-    else
-        error(string.gsub(txt, "'%?'", string.format("'%s'", "Printf")), 3)
-    end
-end
-
---[[-------------------------------------------------------------------------
 --  Event registration and dispatch
 -------------------------------------------------------------------------]]--

@@ -106,7 +92,7 @@ function addon:RegisterMessage(name, handler)
 end

 function addon:UnregisterMessage(name)
-    assert(type(name) == "string", "Invalid argument to 'UnregisterMessage'")
+    assert(type(event) == "string", "Invalid argument to 'UnregisterMessage'")
     messageMap[name] = nil
 end

@@ -117,7 +103,7 @@ function addon:FireMessage(name, ...)
     if handler_t == "function" then
         handler(name, ...)
     elseif handler_t == "string" and addon[handler] then
-        addon[handler](addon, name, ...)
+        addon[handler](addon, event, ...)
     end
 end

@@ -141,6 +127,49 @@ addon:RegisterEvent("ADDON_LOADED", function(event, ...)
 end)

 --[[-------------------------------------------------------------------------
+--  Support for deferred execution (when in-combat)
+-------------------------------------------------------------------------]]--
+
+local deferframe = CreateFrame("Frame")
+deferframe.queue = {}
+
+local function runDeferred(thing)
+    local thing_t = type(thing)
+    if thing_t == "string" and addon[thing] then
+        addon[thing](addon)
+    elseif thing_t == "function" then
+        thing(addon)
+    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 addon: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] = select(i, ...)
+            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)
+
+--[[-------------------------------------------------------------------------
 --  Localization
 -------------------------------------------------------------------------]]--