forked from shirsig/aux-addon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.lua
85 lines (73 loc) · 2.12 KB
/
control.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
select(2, ...) 'aux'
local event_frame = CreateFrame'Frame'
local listeners = {}
function event.AUX_LOADED()
event_frame:SetScript('OnUpdate', function()
for _, listener in pairs(listeners) do
local event, needed = listener.event, false
for _, listener in pairs(listeners) do
needed = needed or listener.event == event and not listener.killed
end
if not needed then
event_frame:UnregisterEvent(event)
end
end
end)
event_frame:SetScript('OnEvent', function(_, event, ...)
for id, listener in pairs(listeners) do
if listener.killed then
listeners[id] = nil
elseif event == listener.event then
listener.cb(...)
end
end
end)
end
function M.kill_listener(listener_id)
local listener = listeners[listener_id]
if listener then
listener.killed = true
end
end
do
local id = 0
function M.event_listener(event, cb)
local listener_id = id
id = id + 1
listeners[listener_id] = {
event = event,
cb = cb,
}
event_frame:RegisterEvent(event)
return listener_id
end
end
do
local threads, kill_signals = {}, {}
CreateFrame'Frame':SetScript('OnUpdate', function()
for thread_id, thread in pairs(threads) do
local status = coroutine.status(thread)
if status == 'dead' or kill_signals[thread_id] then
kill_signals[thread_id] = nil
threads[thread_id] = nil
elseif status == 'suspended' then
assert(coroutine.resume(thread))
end
end
end)
function M.coro_thread(f)
local thread = coroutine.create(f)
local thread_id = tostring(thread)
threads[thread_id] = thread
assert(coroutine.resume(thread))
end
function M.coro_wait()
coroutine.yield()
end
function M.coro_kill(thread_id)
kill_signals[thread_id] = true
end
function M.coro_id()
return tostring(coroutine.running())
end
end