Go to most recent revision |
Details |
Compare with Previous |
View Log
Line No. |
Rev |
Author |
Line |
1 |
34 |
Thurallor-7095 |
-- New functions that supersede the ones in Utils.lua. |
2 |
|
Thurallor-7095 |
-- Needs to be in a separate file in case someone installs one of my plugins that contains an old copy of Utils.lua. |
3 |
|
Thurallor-7095 |
|
4 |
|
Thurallor-7095 |
-- New versions of RemoveCallback() and DoCallbacks() that tolerate removing an event handler from within an event handler: |
5 |
|
Thurallor-7095 |
function _G.RemoveCallback(object, event, callback) |
6 |
|
Thurallor-7095 |
if (callback == nil) then |
7 |
|
Thurallor-7095 |
return; |
8 |
|
Thurallor-7095 |
end |
9 |
|
Thurallor-7095 |
local handlers = object[event]; |
10 |
|
Thurallor-7095 |
if (handlers == callback) then |
11 |
|
Thurallor-7095 |
object[event] = nil; |
12 |
|
Thurallor-7095 |
elseif (type(handlers) == "table") then |
13 |
|
Thurallor-7095 |
local f, i; |
14 |
|
Thurallor-7095 |
repeat |
15 |
|
Thurallor-7095 |
i, f = next(handlers, i); |
16 |
|
Thurallor-7095 |
if (f == callback) then |
17 |
|
Thurallor-7095 |
handlers[i] = nil; |
18 |
|
Thurallor-7095 |
end |
19 |
|
Thurallor-7095 |
until (i == nil); |
20 |
|
Thurallor-7095 |
end |
21 |
|
Thurallor-7095 |
end |
22 |
|
Thurallor-7095 |
|
23 |
|
Thurallor-7095 |
function _G.DoCallbacks(object, event, args) |
24 |
|
Thurallor-7095 |
local handlers = object[event]; |
25 |
|
Thurallor-7095 |
if (type(handlers) == "function") then |
26 |
|
Thurallor-7095 |
handlers(object, args); |
27 |
|
Thurallor-7095 |
elseif (type(handlers) == "table") then |
28 |
|
Thurallor-7095 |
if (next(handlers) == nil) then |
29 |
|
Thurallor-7095 |
-- If all handlers have been removed, remove the table. |
30 |
|
Thurallor-7095 |
object[event] = nil; |
31 |
|
Thurallor-7095 |
else |
32 |
|
Thurallor-7095 |
local f, i; |
33 |
|
Thurallor-7095 |
repeat |
34 |
|
Thurallor-7095 |
i, f = next(handlers, i); |
35 |
|
Thurallor-7095 |
if (type(f) == "function") then |
36 |
|
Thurallor-7095 |
f(object, args); |
37 |
|
Thurallor-7095 |
end |
38 |
|
Thurallor-7095 |
until (i == nil); |
39 |
|
Thurallor-7095 |
end |
40 |
|
Thurallor-7095 |
end |
41 |
|
Thurallor-7095 |
end |
All times are GMT -5. The time now is 07:36 PM.