working on big scan/control refactoring
This commit is contained in:
+26
-100
@@ -1,27 +1,19 @@
|
||||
local private, public = {}, {}
|
||||
Aux.control = public
|
||||
|
||||
local event_listeners = {}
|
||||
local update_listeners = {}
|
||||
private.event_listeners = {}
|
||||
private.threads = {}
|
||||
|
||||
function Aux.control.on_event()
|
||||
for listener, _ in pairs(event_listeners) do
|
||||
function public.on_event()
|
||||
for listener, _ in pairs(private.event_listeners) do
|
||||
if event == listener.event and not listener.deleted then
|
||||
listener.action()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Aux.control.on_update()
|
||||
event_listeners = Aux.util.set_filter(event_listeners, function(l) return not l.deleted end)
|
||||
update_listeners = Aux.util.set_filter(update_listeners, function(l) return not l.deleted end)
|
||||
|
||||
for listener, _ in pairs(update_listeners) do
|
||||
if not listener.deleted then
|
||||
listener.action()
|
||||
end
|
||||
end
|
||||
function public.on_update()
|
||||
private.event_listeners = Aux.util.set_filter(private.event_listeners, function(l) return not l.deleted end)
|
||||
|
||||
for thread_id, k in pairs(private.threads) do
|
||||
private.threads[thread_id] = nil
|
||||
@@ -31,9 +23,7 @@ function Aux.control.on_update()
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
function Aux.control.event_listener(event, action)
|
||||
function public.event_listener(event, action)
|
||||
local self = {}
|
||||
|
||||
local listener = { event=event, action=action }
|
||||
@@ -43,14 +33,14 @@ function Aux.control.event_listener(event, action)
|
||||
end
|
||||
|
||||
function self:start()
|
||||
Aux.util.set_add(event_listeners, listener)
|
||||
Aux.util.set_add(private.event_listeners, listener)
|
||||
AuxControlFrame:RegisterEvent(event)
|
||||
return self
|
||||
end
|
||||
|
||||
function self:stop()
|
||||
listener.deleted = true
|
||||
if not Aux.util.any(Aux.util.set_to_array(event_listeners), function(l) return l.event == event end) then
|
||||
if not Aux.util.any(Aux.util.set_to_array(private.event_listeners), function(l) return l.event == event end) then
|
||||
AuxControlFrame:UnregisterEvent(event)
|
||||
end
|
||||
return self
|
||||
@@ -59,32 +49,8 @@ function Aux.control.event_listener(event, action)
|
||||
return self
|
||||
end
|
||||
|
||||
function Aux.control.update_listener(action)
|
||||
local self = {}
|
||||
|
||||
local listener = { action=action }
|
||||
|
||||
function self:set_action(action)
|
||||
listener.action = action
|
||||
end
|
||||
|
||||
function self:start()
|
||||
Aux.util.set_add(update_listeners, listener)
|
||||
return self
|
||||
end
|
||||
|
||||
function self:stop()
|
||||
listener.deleted = true
|
||||
return self
|
||||
end
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
|
||||
|
||||
function Aux.control.on_next_update(callback)
|
||||
local listener = Aux.control.update_listener()
|
||||
function public.on_next_event(event, callback)
|
||||
local listener = public.event_listener(event)
|
||||
|
||||
listener:set_action(function()
|
||||
listener:stop()
|
||||
@@ -94,68 +60,19 @@ function Aux.control.on_next_update(callback)
|
||||
listener:start()
|
||||
end
|
||||
|
||||
function Aux.control.on_next_event(event, callback)
|
||||
local listener = Aux.control.event_listener(event)
|
||||
|
||||
listener:set_action(function()
|
||||
listener:stop()
|
||||
return callback()
|
||||
end)
|
||||
|
||||
listener:start()
|
||||
function public.on_next_update(callback)
|
||||
return public.new_thread(callback)
|
||||
end
|
||||
|
||||
function Aux.control.as_soon_as(p, callback)
|
||||
local listener = Aux.control.update_listener()
|
||||
|
||||
listener:set_action(function()
|
||||
if p() then
|
||||
listener:stop()
|
||||
return callback()
|
||||
end
|
||||
function public.as_soon_as(p, callback)
|
||||
return public.new_thread(function()
|
||||
return public.wait_until(p, callback)
|
||||
end)
|
||||
|
||||
listener:start()
|
||||
end
|
||||
|
||||
|
||||
|
||||
function Aux.control.controller()
|
||||
local self = {}
|
||||
|
||||
local state
|
||||
|
||||
local listener = Aux.control.update_listener()
|
||||
listener:set_action(function()
|
||||
if state and state.p() then
|
||||
local k = state.k
|
||||
state = nil
|
||||
return k()
|
||||
end
|
||||
end)
|
||||
listener:start()
|
||||
|
||||
function self.wait(p, k)
|
||||
state = {
|
||||
k = k,
|
||||
p = p,
|
||||
}
|
||||
end
|
||||
|
||||
function self:reset()
|
||||
state = nil
|
||||
end
|
||||
|
||||
function self:cleanup()
|
||||
listener.stop()
|
||||
end
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
do
|
||||
local next_thread_id = 1
|
||||
function public.new(k)
|
||||
function public.new_thread(k)
|
||||
local thread_id = next_thread_id
|
||||
next_thread_id = next_thread_id + 1
|
||||
private.threads[thread_id] = k
|
||||
@@ -163,7 +80,7 @@ do
|
||||
end
|
||||
end
|
||||
|
||||
function public.kill(thread_id)
|
||||
function public.kill_thread(thread_id)
|
||||
private.threads[thread_id] = nil
|
||||
end
|
||||
|
||||
@@ -171,3 +88,12 @@ function public.wait(...)
|
||||
local k = tremove(arg, 1)
|
||||
private.threads[public.thread_id] = function() return k(unpack(arg)) end
|
||||
end
|
||||
|
||||
function public.wait_until(p, ...)
|
||||
local k = tremove(arg, 1)
|
||||
if p() then
|
||||
return k(unpack(arg))
|
||||
else
|
||||
return public.wait(public.wait_until, p, function() return k(unpack(arg)) end)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user