Move promise to separate class file

Sasky [06-21-14 - 13:46]
Move promise to separate class file
Setup debug MISSED_CALLS for easier debug
Filename
LAddMin.lua
LAddMin.txt
Promise.lua
diff --git a/LAddMin.lua b/LAddMin.lua
index 709d016..1ceaf20 100644
--- a/LAddMin.lua
+++ b/LAddMin.lua
@@ -45,55 +45,6 @@ local invalidAddons = LAddMinCompatability
 -- local lastID = 1000

 -------------------------------------------------------------------------------
--- Delayed calls object, author Sasky (http://www.esoui.com/forums/member.php?u=2586)
--------------------------------------------------------------------------------
-local Promise = {}
-setmetatable(Promise, {
-   __index = function(self, key)
-      self:__preSave(key)
-      return (self.__save)
-   end,
-   __call = function (cls, ...)
-      return cls.__new(...)
-   end
-})
-function Promise:__new()
-   local o = setmetatable({}, {
-      __index = Promise,
-      __call = function (cls, ...)
-         return cls:__execute(...)
-      end
-   })
-   o.callList = {}
-   o.nextKey = nil
-
-   return o
-end
-
-function Promise:__preSave(key)
-   self.nextKey = key
-end
-
-function Promise:__save(...)
-   if self.nextKey == nil then return end
-
-   table.insert(self.callList, {
-      func = self.nextKey,
-      narg = select('#', ...),
-      varargs = {...}
-   })
-   self.nextKey = nil
-end
-
-MISSED_CALLS = {}
-function Promise:__execute(obj)
-   for k,v in ipairs(self.callList) do
-      table.insert(MISSED_CALLS, k .. ":" .. v.func .. "(" .. table.concat(v.varargs,",") .. ")")
-      obj[v.func](v.func, unpack(v.varargs))
-   end
-end
-
--------------------------------------------------------------------------------
 -- Library replacement --------------------------------------------------------
 -------------------------------------------------------------------------------
 --catch calls from addons which creates menu before saved variables are available (Rainbow Reticle)
@@ -380,7 +331,7 @@ end
 local function DelayedCalls(panel)
     local panelID = panelIDs[panel:GetName()]
     if panelID then
-        for i,controlTable in ipairs(settingsTable[panelID - 1000].controls) do
+        for _,controlTable in ipairs(settingsTable[panelID - 1000].controls) do
             local missedCalls = _G[controlTable.controlName]
             _G[controlTable.controlName] = _G[controlTable.reference]

@@ -396,7 +347,10 @@ local function DelayedCalls(panel)
             end

             _G[controlTable.reference] = nil
-            missedCalls = nil
+
+            --Catch any other saved references addons might be using
+            --TODO: Verify this in a mockup
+            setmetatable(missedCalls, { __index = _G[controlTable.controlName] })
         end
     end
 end
diff --git a/LAddMin.txt b/LAddMin.txt
index fee0a9b..355287a 100644
--- a/LAddMin.txt
+++ b/LAddMin.txt
@@ -22,4 +22,5 @@ Libs\LibAddonMenu-2.0\controls\texture.lua

 LAddMinCompatability.lua
 LAddMinConfig.lua
+Promise.lua
 LAddMin.lua
diff --git a/Promise.lua b/Promise.lua
new file mode 100644
index 0000000..b992937
--- /dev/null
+++ b/Promise.lua
@@ -0,0 +1,55 @@
+--Debug inspection. aa to show at beginning of Zgoo
+--Uncomment to ignore
+MISSED_CALLS = {}
+
+Promise = {}
+setmetatable(Promise, {
+    __index = function(self, key)
+        self:__preSave(key)
+        return (self.__save)
+    end,
+    __call = function (cls, ...)
+        return cls.__new(...)
+    end
+})
+function Promise:__new()
+    local o = setmetatable({}, {
+        __index = Promise,
+        __call = function (cls, ...)
+            return cls:__execute(...)
+        end
+    })
+    o.callList = {}
+    o.nextKey = nil
+
+    return o
+end
+
+function Promise:__getMissedCalls()
+    return missedCalls
+end
+
+function Promise:__preSave(key)
+    self.nextKey = key
+end
+
+function Promise:__save(...)
+    if self.nextKey == nil then return end
+
+    table.insert(self.callList, {
+        func = self.nextKey,
+        narg = select('#', ...),
+        varargs = {...}
+    })
+    self.nextKey = nil
+end
+
+function Promise:__execute(obj)
+    for k,v in ipairs(self.callList) do
+        --Add info for debug inspection
+        if MISSED_CALLS then
+            table.insert(aaMISSED_CALLS, k .. ":" .. v.func .. "(" .. table.concat(v.varargs,",") .. ")")
+        end
+        obj[v.func](v.func, unpack(v.varargs))
+    end
+end
\ No newline at end of file