annotate Libs/CallbackHandler-1.0/CallbackHandler-1.0.lua @ 3:e75889a45130

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