Files
aux-addon-ClassicAPI/control.lua
T
Manuel Simon Hirsig 9221dcc0ef refactoring
2016-09-04 20:00:26 +02:00

91 lines
2.0 KiB
Lua

aux 'core'
local event_frame = CreateFrame('Frame')
local listeners, threads = t, t
local thread_id
function public.thread_id.get() return thread_id end
function LOAD()
event_frame:SetScript('OnUpdate', UPDATE)
event_frame:SetScript('OnEvent', EVENT)
end
function EVENT()
for id, listener in listeners do
if listener.killed then
listeners[id] = nil
elseif event == listener.event then
listener.cb(listener.kill)
end
end
end
do
function UPDATE()
for _, listener in listeners do
local event, needed = listener.event, false
for _, listener in listeners do needed = needed or listener.event == event and not listener.killed end
if not needed then event_frame:UnregisterEvent(event) end
end
for id, thread in threads do
if thread.killed or not thread.k then
threads[id] = nil
else
local k = thread.k
thread.k = nil
thread_id = id
k()
thread_id = nil
end
end
end
end
do local id = 0
function private.id.get() id = id + 1; return id end
end
function public.kill_listener(listener_id)
for listener in present(listeners[listener_id]) do listener.killed = true end
end
function public.kill_thread(thread_id)
for thread in present(threads[thread_id]) do thread.killed = true end
end
function public.event_listener(event, cb)
local listener_id = id
listeners[listener_id] = {event=event, cb=cb, kill=function(...) temp=arg if arg.n == 0 or arg[1] then kill_listener(listener_id) end end}
event_frame:RegisterEvent(event)
return listener_id
end
function public.on_next_event(event, callback)
event_listener(event, function(kill) callback(); kill() end)
end
function public.thread(k, ...) temp=arg
local thread_id = id
threads[thread_id] = T('k', L(k, unpack(arg)))
return thread_id
end
function public.wait(k, ...) temp=arg
if type(k) == 'number' then
when(function() k = k - 1 return k <= 1 end, unpack(arg))
else
threads[thread_id].k = L(k, unpack(arg))
end
end
function public.when(p, k, ...) temp=arg
if p() then
return k(unpack(arg))
else
return wait(when, p, k, unpack(arg))
end
end