diff --git a/components/post.lua b/components/post.lua index 395151f..065c3c1 100644 --- a/components/post.lua +++ b/components/post.lua @@ -40,10 +40,10 @@ function private.post_auction(slot, k) end) local posted - Aux.control.event_listener('CHAT_MSG_SYSTEM', function() + Aux.control.event_listener('CHAT_MSG_SYSTEM', function(kill) if arg1 == ERR_AUCTION_STARTED then c() - Aux.control.kill() + kill() end end) else diff --git a/components/scan.lua b/components/scan.lua index 83cd206..fc28540 100644 --- a/components/scan.lua +++ b/components/scan.lua @@ -8,7 +8,7 @@ do local scan_states = {} function public.start(params) - for old_state in {scan_states[params.type]} do + for _, old_state in {scan_states[params.type]} do m.abort(old_state.id) end local thread_id = Aux.control.thread(Aux.f(m.wait_for_callback, params.on_scan_start, m.scan)) @@ -215,8 +215,8 @@ end function private.wait_for_list_results(c) local updated, last_update - Aux.control.event_listener('AUCTION_ITEM_LIST_UPDATE', function() - Aux.control.kill(-c) + Aux.control.event_listener('AUCTION_ITEM_LIST_UPDATE', function(kill) + kill(-c) last_update = GetTime() updated = true end) diff --git a/components/stack.lua b/components/stack.lua index 4d78bbc..2a06305 100644 --- a/components/stack.lua +++ b/components/stack.lua @@ -52,7 +52,6 @@ function private.find_charge_item_slot() end function private.move_item(from_slot, to_slot, amount, k) - if m.locked(from_slot) or m.locked(to_slot) then return Aux.control.wait(k) end @@ -80,7 +79,6 @@ function private.process() m.state.target_slot = m.find_charge_item_slot() return m.stop() end - if m.stack_size(m.state.target_slot) > m.state.target_size then local slot = m.find_item_slot(true) or m.find_empty_slot() if slot then @@ -102,24 +100,18 @@ function private.process() ) end end - return m.stop() end function public.stop() if m.state then Aux.control.kill_thread(m.state.thread_id) - local callback, slot = m.state.callback, m.state.target_slot if slot and not m.matching_item(slot) then slot = nil end - m.state = nil - - if callback then - callback(slot) - end + Aux.safe_call(callback, slot) end end @@ -127,7 +119,6 @@ function public.start(item_key, size, callback) m.stop() local thread_id = Aux.control.thread(m.process) - m.state = { thread_id = thread_id, item_key = item_key, diff --git a/control.lua b/control.lua index 7467e1a..4be36ca 100644 --- a/control.lua +++ b/control.lua @@ -1,7 +1,7 @@ local m, public, private = Aux.module'control' private.event_frame = CreateFrame('Frame') -private.event_listeners = {} +private.listeners = {} private.threads = {} public.thread_id = nil @@ -10,34 +10,22 @@ function public.LOAD() m.event_frame:SetScript('OnEvent', m.on_event) end -do - local active_listener - - function public.kill(...) - active_listener.killed = arg.n == 0 or arg[1] - end - - function private.on_event() - for thread_id, listener in m.event_listeners do - if event == listener.event and not listener.killed then - m.thread_id = thread_id - active_listener = listener - listener.cb() - active_listener = nil - m.thread_id = nil - end +function private.on_event() + for _, listener in m.listeners do + if event == listener.event and not listener.killed then + listener.cb(listener.kill) end end end function private.on_update() - for _, listener in m.event_listeners do - if not Aux.util.any(m.event_listeners, function(l) return not l.killed and l.event == listener.event end) then + for _, listener in m.listeners do + if not Aux.util.any(m.listeners, function(l) return not l.killed and l.event == listener.event end) then m.event_frame:UnregisterEvent(listener.event) end end - m.event_listeners = Aux.util.filter(m.event_listeners, function(l) return not l.killed end) + m.listeners = Aux.util.filter(m.listeners, function(l) return not l.killed end) m.threads = Aux.util.filter(m.threads, function(th) return not th.killed end) for thread_id, thread in m.threads do @@ -54,17 +42,29 @@ function private.on_update() end end +function public.kill_listener(listener_id) + for _, listener in {m.listeners[listener_id]} do + listener.killed = true + end +end + +function public.kill_thread(thread_id) + for _, thread in {m.threads[thread_id]} do + thread.killed = true + end +end + function public.event_listener(event, cb) - local thread_id = Aux.unique() - m.event_listeners[thread_id] = { event=event, cb=cb } + local listener_id = Aux.unique() + m.listeners[listener_id] = { event=event, cb=cb, kill=function(...) if arg.n == 0 or arg[1] then m.kill_listener(listener_id) end end } m.event_frame:RegisterEvent(event) - return thread_id + return listener_id end function public.on_next_event(event, callback) - m.event_listener(event, function() + m.event_listener(event, function(kill) callback() - m.kill() + kill() end) end @@ -78,12 +78,6 @@ function public.thread(k, ...) return thread_id end -function public.kill_thread(thread_id) - if m.threads[thread_id] then - m.threads[thread_id].killed = true - end -end - function public.await(k) local ret m.when(function() return ret end, function() return k(unpack(ret)) end) diff --git a/core.lua b/core.lua index eeb94ea..27d6da8 100644 --- a/core.lua +++ b/core.lua @@ -295,11 +295,11 @@ do if money >= amount then locked = true - m.control.event_listener('CHAT_MSG_SYSTEM', function() + m.control.event_listener('CHAT_MSG_SYSTEM', function(kill) if arg1 == ERR_AUCTION_BID_PLACED then Aux.safe_call(on_success) locked = false - Aux.control.kill() + kill() end end) end @@ -322,11 +322,11 @@ do locked = true CancelAuction(index) - m.control.event_listener('CHAT_MSG_SYSTEM', function() + m.control.event_listener('CHAT_MSG_SYSTEM', function(kill) if arg1 == ERR_AUCTION_REMOVED then Aux.safe_call(on_success) locked = false - Aux.control.kill() + kill() end end) end