Mercurial > wow > hotcorners
comparison Libs/CallbackHandler-1.0/CallbackHandler-1.0.lua @ 22:3596dadf9a90 v8.1.0.022
- ToC and Libraries update.
| author | Tercioo |
|---|---|
| date | Mon, 31 Dec 2018 11:30:31 -0200 |
| parents | 371e14cd2feb |
| children |
comparison
equal
deleted
inserted
replaced
| 21:f29a995e98e3 | 22:3596dadf9a90 |
|---|---|
| 1 --[[ $Id: CallbackHandler-1.0.lua 1131 2015-06-04 07:29:24Z nevcairiel $ ]] | 1 --[[ $Id: CallbackHandler-1.0.lua 22 2018-07-21 14:17:22Z nevcairiel $ ]] |
| 2 local MAJOR, MINOR = "CallbackHandler-1.0", 6 | 2 local MAJOR, MINOR = "CallbackHandler-1.0", 7 |
| 3 local CallbackHandler = LibStub:NewLibrary(MAJOR, MINOR) | 3 local CallbackHandler = LibStub:NewLibrary(MAJOR, MINOR) |
| 4 | 4 |
| 5 if not CallbackHandler then return end -- No upgrade needed | 5 if not CallbackHandler then return end -- No upgrade needed |
| 6 | 6 |
| 7 local meta = {__index = function(tbl, key) tbl[key] = {} return tbl[key] end} | 7 local meta = {__index = function(tbl, key) tbl[key] = {} return tbl[key] end} |
| 20 | 20 |
| 21 local function errorhandler(err) | 21 local function errorhandler(err) |
| 22 return geterrorhandler()(err) | 22 return geterrorhandler()(err) |
| 23 end | 23 end |
| 24 | 24 |
| 25 local function CreateDispatcher(argCount) | 25 local function Dispatch(handlers, ...) |
| 26 local code = [[ | 26 local index, method = next(handlers) |
| 27 local next, xpcall, eh = ... | 27 if not method then return end |
| 28 | 28 repeat |
| 29 local method, ARGS | 29 xpcall(method, errorhandler, ...) |
| 30 local function call() method(ARGS) end | 30 index, method = next(handlers, index) |
| 31 | 31 until not method |
| 32 local function dispatch(handlers, ...) | |
| 33 local index | |
| 34 index, method = next(handlers) | |
| 35 if not method then return end | |
| 36 local OLD_ARGS = ARGS | |
| 37 ARGS = ... | |
| 38 repeat | |
| 39 xpcall(call, eh) | |
| 40 index, method = next(handlers, index) | |
| 41 until not method | |
| 42 ARGS = OLD_ARGS | |
| 43 end | |
| 44 | |
| 45 return dispatch | |
| 46 ]] | |
| 47 | |
| 48 local ARGS, OLD_ARGS = {}, {} | |
| 49 for i = 1, argCount do ARGS[i], OLD_ARGS[i] = "arg"..i, "old_arg"..i end | |
| 50 code = code:gsub("OLD_ARGS", tconcat(OLD_ARGS, ", ")):gsub("ARGS", tconcat(ARGS, ", ")) | |
| 51 return assert(loadstring(code, "safecall Dispatcher["..argCount.."]"))(next, xpcall, errorhandler) | |
| 52 end | 32 end |
| 53 | |
| 54 local Dispatchers = setmetatable({}, {__index=function(self, argCount) | |
| 55 local dispatcher = CreateDispatcher(argCount) | |
| 56 rawset(self, argCount, dispatcher) | |
| 57 return dispatcher | |
| 58 end}) | |
| 59 | 33 |
| 60 -------------------------------------------------------------------------- | 34 -------------------------------------------------------------------------- |
| 61 -- CallbackHandler:New | 35 -- CallbackHandler:New |
| 62 -- | 36 -- |
| 63 -- target - target object to embed public APIs in | 37 -- target - target object to embed public APIs in |
| 85 function registry:Fire(eventname, ...) | 59 function registry:Fire(eventname, ...) |
| 86 if not rawget(events, eventname) or not next(events[eventname]) then return end | 60 if not rawget(events, eventname) or not next(events[eventname]) then return end |
| 87 local oldrecurse = registry.recurse | 61 local oldrecurse = registry.recurse |
| 88 registry.recurse = oldrecurse + 1 | 62 registry.recurse = oldrecurse + 1 |
| 89 | 63 |
| 90 Dispatchers[select('#', ...) + 1](events[eventname], eventname, ...) | 64 Dispatch(events[eventname], eventname, ...) |
| 91 | 65 |
| 92 registry.recurse = oldrecurse | 66 registry.recurse = oldrecurse |
| 93 | 67 |
| 94 if registry.insertQueue and oldrecurse==0 then | 68 if registry.insertQueue and oldrecurse==0 then |
| 95 -- Something in one of our callbacks wanted to register more callbacks; they got queued | 69 -- Something in one of our callbacks wanted to register more callbacks; they got queued |
