annotate lib/CallbackHandler-1.0/CallbackHandler-1.0.lua @ 28:21bcaf8215ff

- converted to Ace3 - rearranged file layout - configGUI menus not working right now
author Flick <flickerstreak@gmail.com>
date Mon, 17 Mar 2008 18:24:53 +0000
parents
children
rev   line source
flickerstreak@28 1 --[[ $Id: CallbackHandler-1.0.lua 60697 2008-02-09 16:51:20Z nevcairiel $ ]]
flickerstreak@28 2 local MAJOR, MINOR = "CallbackHandler-1.0", 3
flickerstreak@28 3 local CallbackHandler = LibStub:NewLibrary(MAJOR, MINOR)
flickerstreak@28 4
flickerstreak@28 5 if not CallbackHandler then return end -- No upgrade needed
flickerstreak@28 6
flickerstreak@28 7 local meta = {__index = function(tbl, key) tbl[key] = {} return tbl[key] end}
flickerstreak@28 8
flickerstreak@28 9 local type = type
flickerstreak@28 10 local pcall = pcall
flickerstreak@28 11 local pairs = pairs
flickerstreak@28 12 local assert = assert
flickerstreak@28 13 local concat = table.concat
flickerstreak@28 14 local loadstring = loadstring
flickerstreak@28 15 local next = next
flickerstreak@28 16 local select = select
flickerstreak@28 17 local type = type
flickerstreak@28 18 local xpcall = xpcall
flickerstreak@28 19
flickerstreak@28 20 local function errorhandler(err)
flickerstreak@28 21 return geterrorhandler()(err)
flickerstreak@28 22 end
flickerstreak@28 23
flickerstreak@28 24 local function CreateDispatcher(argCount)
flickerstreak@28 25 local code = [[
flickerstreak@28 26 local next, xpcall, eh = ...
flickerstreak@28 27
flickerstreak@28 28 local method, ARGS
flickerstreak@28 29 local function call() method(ARGS) end
flickerstreak@28 30
flickerstreak@28 31 local function dispatch(handlers, ...)
flickerstreak@28 32 local index
flickerstreak@28 33 index, method = next(handlers)
flickerstreak@28 34 if not method then return end
flickerstreak@28 35 local OLD_ARGS = ARGS
flickerstreak@28 36 ARGS = ...
flickerstreak@28 37 repeat
flickerstreak@28 38 xpcall(call, eh)
flickerstreak@28 39 index, method = next(handlers, index)
flickerstreak@28 40 until not method
flickerstreak@28 41 ARGS = OLD_ARGS
flickerstreak@28 42 end
flickerstreak@28 43
flickerstreak@28 44 return dispatch
flickerstreak@28 45 ]]
flickerstreak@28 46
flickerstreak@28 47 local ARGS, OLD_ARGS = {}, {}
flickerstreak@28 48 for i = 1, argCount do ARGS[i], OLD_ARGS[i] = "arg"..i, "old_arg"..i end
flickerstreak@28 49 code = code:gsub("OLD_ARGS", concat(OLD_ARGS, ", ")):gsub("ARGS", concat(ARGS, ", "))
flickerstreak@28 50 return assert(loadstring(code, "safecall Dispatcher["..argCount.."]"))(next, xpcall, errorhandler)
flickerstreak@28 51 end
flickerstreak@28 52
flickerstreak@28 53 local Dispatchers = setmetatable({}, {__index=function(self, argCount)
flickerstreak@28 54 local dispatcher = CreateDispatcher(argCount)
flickerstreak@28 55 rawset(self, argCount, dispatcher)
flickerstreak@28 56 return dispatcher
flickerstreak@28 57 end})
flickerstreak@28 58
flickerstreak@28 59 --------------------------------------------------------------------------
flickerstreak@28 60 -- CallbackHandler:New
flickerstreak@28 61 --
flickerstreak@28 62 -- target - target object to embed public APIs in
flickerstreak@28 63 -- RegisterName - name of the callback registration API, default "RegisterCallback"
flickerstreak@28 64 -- UnregisterName - name of the callback unregistration API, default "UnregisterCallback"
flickerstreak@28 65 -- UnregisterAllName - name of the API to unregister all callbacks, default "UnregisterAllCallbacks". false == don't publish this API.
flickerstreak@28 66
flickerstreak@28 67 function CallbackHandler:New(target, RegisterName, UnregisterName, UnregisterAllName, OnUsed, OnUnused)
flickerstreak@28 68 -- TODO: Remove this after beta has gone out
flickerstreak@28 69 assert(not OnUsed and not OnUnused, "ACE-80: OnUsed/OnUnused are deprecated. Callbacks are now done to registry.OnUsed and registry.OnUnused")
flickerstreak@28 70
flickerstreak@28 71 RegisterName = RegisterName or "RegisterCallback"
flickerstreak@28 72 UnregisterName = UnregisterName or "UnregisterCallback"
flickerstreak@28 73 if UnregisterAllName==nil then -- false is used to indicate "don't want this method"
flickerstreak@28 74 UnregisterAllName = "UnregisterAllCallbacks"
flickerstreak@28 75 end
flickerstreak@28 76
flickerstreak@28 77 -- we declare all objects and exported APIs inside this closure to quickly gain access
flickerstreak@28 78 -- to e.g. function names, the "target" parameter, etc
flickerstreak@28 79
flickerstreak@28 80
flickerstreak@28 81 -- Create the registry object
flickerstreak@28 82 local events = setmetatable({}, meta)
flickerstreak@28 83 local registry = { recurse=0, events=events }
flickerstreak@28 84
flickerstreak@28 85 -- registry:Fire() - fires the given event/message into the registry
flickerstreak@28 86 function registry:Fire(eventname, ...)
flickerstreak@28 87 if not rawget(events, eventname) or not next(events[eventname]) then return end
flickerstreak@28 88 local oldrecurse = registry.recurse
flickerstreak@28 89 registry.recurse = oldrecurse + 1
flickerstreak@28 90
flickerstreak@28 91 Dispatchers[select('#', ...) + 1](events[eventname], eventname, ...)
flickerstreak@28 92
flickerstreak@28 93 registry.recurse = oldrecurse
flickerstreak@28 94
flickerstreak@28 95 if registry.insertQueue and oldrecurse==0 then
flickerstreak@28 96 -- Something in one of our callbacks wanted to register more callbacks; they got queued
flickerstreak@28 97 for eventname,callbacks in pairs(registry.insertQueue) do
flickerstreak@28 98 local first = not rawget(events, eventname) or not next(events[eventname]) -- test for empty before. not test for one member after. that one member may have been overwritten.
flickerstreak@28 99 for self,func in pairs(callbacks) do
flickerstreak@28 100 events[eventname][self] = func
flickerstreak@28 101 -- fire OnUsed callback?
flickerstreak@28 102 if first and registry.OnUsed then
flickerstreak@28 103 registry.OnUsed(registry, target, eventname)
flickerstreak@28 104 first = nil
flickerstreak@28 105 end
flickerstreak@28 106 end
flickerstreak@28 107 end
flickerstreak@28 108 registry.insertQueue = nil
flickerstreak@28 109 end
flickerstreak@28 110 end
flickerstreak@28 111
flickerstreak@28 112 -- Registration of a callback, handles:
flickerstreak@28 113 -- self["method"], leads to self["method"](self, ...)
flickerstreak@28 114 -- self with function ref, leads to functionref(...)
flickerstreak@28 115 -- "addonId" (instead of self) with function ref, leads to functionref(...)
flickerstreak@28 116 -- all with an optional arg, which, if present, gets passed as first argument (after self if present)
flickerstreak@28 117 target[RegisterName] = function(self, eventname, method, ... --[[actually just a single arg]])
flickerstreak@28 118 if type(eventname) ~= "string" then
flickerstreak@28 119 error("Usage: "..RegisterName.."(eventname, method[, arg]): 'eventname' - string expected.", 2)
flickerstreak@28 120 end
flickerstreak@28 121
flickerstreak@28 122 method = method or eventname
flickerstreak@28 123
flickerstreak@28 124 local first = not rawget(events, eventname) or not next(events[eventname]) -- test for empty before. not test for one member after. that one member may have been overwritten.
flickerstreak@28 125
flickerstreak@28 126 if type(method) ~= "string" and type(method) ~= "function" then
flickerstreak@28 127 error("Usage: "..RegisterName.."(\"eventname\", \"methodname\"): 'methodname' - string or function expected.", 2)
flickerstreak@28 128 end
flickerstreak@28 129
flickerstreak@28 130 local regfunc
flickerstreak@28 131
flickerstreak@28 132 if type(method) == "string" then
flickerstreak@28 133 -- self["method"] calling style
flickerstreak@28 134 if type(self) ~= "table" then
flickerstreak@28 135 error("Usage: "..RegisterName.."(\"eventname\", \"methodname\"): self was not a table?", 2)
flickerstreak@28 136 elseif self==target then
flickerstreak@28 137 error("Usage: "..RegisterName.."(\"eventname\", \"methodname\"): do not use Library:"..RegisterName.."(), use your own 'self'", 2)
flickerstreak@28 138 elseif type(self[method]) ~= "function" then
flickerstreak@28 139 error("Usage: "..RegisterName.."(\"eventname\", \"methodname\"): 'methodname' - method '"..tostring(method).."' not found on self.", 2)
flickerstreak@28 140 end
flickerstreak@28 141
flickerstreak@28 142 if select("#",...)>=1 then -- this is not the same as testing for arg==nil!
flickerstreak@28 143 local arg=select(1,...)
flickerstreak@28 144 regfunc = function(...) self[method](self,arg,...) end
flickerstreak@28 145 else
flickerstreak@28 146 regfunc = function(...) self[method](self,...) end
flickerstreak@28 147 end
flickerstreak@28 148 else
flickerstreak@28 149 -- function ref with self=object or self="addonId"
flickerstreak@28 150 if type(self)~="table" and type(self)~="string" then
flickerstreak@28 151 error("Usage: "..RegisterName.."(self or \"addonId\", eventname, method): 'self or addonId': table or string expected.", 2)
flickerstreak@28 152 end
flickerstreak@28 153
flickerstreak@28 154 if select("#",...)>=1 then -- this is not the same as testing for arg==nil!
flickerstreak@28 155 local arg=select(1,...)
flickerstreak@28 156 regfunc = function(...) method(arg,...) end
flickerstreak@28 157 else
flickerstreak@28 158 regfunc = method
flickerstreak@28 159 end
flickerstreak@28 160 end
flickerstreak@28 161
flickerstreak@28 162
flickerstreak@28 163 if events[eventname][self] or registry.recurse<1 then
flickerstreak@28 164 -- if registry.recurse<1 then
flickerstreak@28 165 -- we're overwriting an existing entry, or not currently recursing. just set it.
flickerstreak@28 166 events[eventname][self] = regfunc
flickerstreak@28 167 -- fire OnUsed callback?
flickerstreak@28 168 if registry.OnUsed and first then
flickerstreak@28 169 registry.OnUsed(registry, target, eventname)
flickerstreak@28 170 end
flickerstreak@28 171 else
flickerstreak@28 172 -- we're currently processing a callback in this registry, so delay the registration of this new entry!
flickerstreak@28 173 -- yes, we're a bit wasteful on garbage, but this is a fringe case, so we're picking low implementation overhead over garbage efficiency
flickerstreak@28 174 registry.insertQueue = registry.insertQueue or setmetatable({},meta)
flickerstreak@28 175 registry.insertQueue[eventname][self] = regfunc
flickerstreak@28 176 end
flickerstreak@28 177 end
flickerstreak@28 178
flickerstreak@28 179 -- Unregister a callback
flickerstreak@28 180 target[UnregisterName] = function(self, eventname)
flickerstreak@28 181 if not self or self==target then
flickerstreak@28 182 error("Usage: "..UnregisterName.."(eventname): bad 'self'", 2)
flickerstreak@28 183 end
flickerstreak@28 184 if type(eventname) ~= "string" then
flickerstreak@28 185 error("Usage: "..UnregisterName.."(eventname): 'eventname' - string expected.", 2)
flickerstreak@28 186 end
flickerstreak@28 187 if rawget(events, eventname) and events[eventname][self] then
flickerstreak@28 188 events[eventname][self] = nil
flickerstreak@28 189 -- Fire OnUnused callback?
flickerstreak@28 190 if registry.OnUnused and not next(events[eventname]) then
flickerstreak@28 191 registry.OnUnused(registry, target, eventname)
flickerstreak@28 192 end
flickerstreak@28 193 end
flickerstreak@28 194 if registry.insertQueue and rawget(registry.insertQueue, eventname) and registry.insertQueue[eventname][self] then
flickerstreak@28 195 registry.insertQueue[eventname][self] = nil
flickerstreak@28 196 end
flickerstreak@28 197 end
flickerstreak@28 198
flickerstreak@28 199 -- OPTIONAL: Unregister all callbacks for given selfs/addonIds
flickerstreak@28 200 if UnregisterAllName then
flickerstreak@28 201 target[UnregisterAllName] = function(...)
flickerstreak@28 202 if select("#",...)<1 then
flickerstreak@28 203 error("Usage: "..UnregisterAllName.."([whatFor]): missing 'self' or \"addonId\" to unregister events for.", 2)
flickerstreak@28 204 end
flickerstreak@28 205 if select("#",...)==1 and ...==target then
flickerstreak@28 206 error("Usage: "..UnregisterAllName.."([whatFor]): supply a meaningful 'self' or \"addonId\"", 2)
flickerstreak@28 207 end
flickerstreak@28 208
flickerstreak@28 209
flickerstreak@28 210 for i=1,select("#",...) do
flickerstreak@28 211 local self = select(i,...)
flickerstreak@28 212 if registry.insertQueue then
flickerstreak@28 213 for eventname, callbacks in pairs(registry.insertQueue) do
flickerstreak@28 214 if callbacks[self] then
flickerstreak@28 215 callbacks[self] = nil
flickerstreak@28 216 end
flickerstreak@28 217 end
flickerstreak@28 218 end
flickerstreak@28 219 for eventname, callbacks in pairs(events) do
flickerstreak@28 220 if callbacks[self] then
flickerstreak@28 221 callbacks[self] = nil
flickerstreak@28 222 -- Fire OnUnused callback?
flickerstreak@28 223 if registry.OnUnused and not next(callbacks) then
flickerstreak@28 224 registry.OnUnused(registry, target, eventname)
flickerstreak@28 225 end
flickerstreak@28 226 end
flickerstreak@28 227 end
flickerstreak@28 228 end
flickerstreak@28 229 end
flickerstreak@28 230 end
flickerstreak@28 231
flickerstreak@28 232 return registry
flickerstreak@28 233 end
flickerstreak@28 234
flickerstreak@28 235
flickerstreak@28 236 -- CallbackHandler purposefully does NOT do explicit embedding. Nor does it
flickerstreak@28 237 -- try to upgrade old implicit embeds since the system is selfcontained and
flickerstreak@28 238 -- relies on closures to work.
flickerstreak@28 239