diff --git a/components/cache.lua b/components/cache.lua index 8c7dc29..05422c5 100644 --- a/components/cache.lua +++ b/components/cache.lua @@ -14,10 +14,10 @@ aux_characters = {} function public.LOAD() m.scan_wdb() - Aux.control.event_listener('MERCHANT_SHOW', m.on_merchant_show):start() - Aux.control.event_listener('MERCHANT_CLOSED', m.on_merchant_closed):start() - Aux.control.event_listener('MERCHANT_UPDATE', m.on_merchant_update):start() - Aux.control.event_listener('BAG_UPDATE', m.on_bag_update):start() + Aux.control.event_listener('MERCHANT_SHOW', m.on_merchant_show) + Aux.control.event_listener('MERCHANT_CLOSED', m.on_merchant_closed) + Aux.control.event_listener('MERCHANT_UPDATE', m.on_merchant_update) + Aux.control.event_listener('BAG_UPDATE', m.on_bag_update) CreateFrame('Frame', nil, MerchantFrame):SetScript('OnUpdate', m.merchant_on_update) @@ -29,7 +29,7 @@ function public.LOAD() aux_merchant_sell[Aux.cache.item_id(info.name)] = info.vendor_price / (Aux.info.max_item_charges(item_id) or info.count) end end - end):start() + end) end do diff --git a/components/post.lua b/components/post.lua index 353002f..fa078f8 100644 --- a/components/post.lua +++ b/components/post.lua @@ -40,15 +40,12 @@ function private.post_auction(slot, k) end) local posted - local listener = Aux.control.event_listener('CHAT_MSG_SYSTEM') - listener:set_action(function() + Aux.control.event_listener('CHAT_MSG_SYSTEM', function() if arg1 == ERR_AUCTION_STARTED then - listener:stop() c() + return Aux.control.kill end end) - listener:start() - else return m.stop() end diff --git a/components/scan.lua b/components/scan.lua index aa5cebb..8f043f5 100644 --- a/components/scan.lua +++ b/components/scan.lua @@ -89,27 +89,26 @@ end function private.wait_for_list_results(c) local updated, last_update - local listener = Aux.control.event_listener('AUCTION_ITEM_LIST_UPDATE', function() + Aux.control.event_listener('AUCTION_ITEM_LIST_UPDATE', function() + if -c then + return Aux.control.kill + end last_update = GetTime() updated = true end) - listener:start() + local type = m.th.params.type local ignore_owner = m.th.params.ignore_owner or aux_ignore_owner Aux.control.as_soon_as(function() -- short circuiting order important, owner_data_complete must be called iif an update has happened. - -- if no update has happened it must not be called for performance reasons, otherwise it must be called to request further missing data if there is any - local ok = updated and (ignore_owner or m.owner_data_complete()) or last_update and GetTime() - last_update > 5 + local ok = updated and (ignore_owner or m.owner_data_complete(type)) or last_update and GetTime() - last_update > 5 updated = false return ok - end, function() - listener:stop() - return c() - end) + end, c) end -function private.owner_data_complete() +function private.owner_data_complete(type) for i=1,PAGE_SIZE do - local auction_info = Aux.info.auction(i, m.th.params.type) + local auction_info = Aux.info.auction(i, type) if auction_info and not auction_info.owner then return false end diff --git a/control.lua b/control.lua index a24e940..9d5575b 100644 --- a/control.lua +++ b/control.lua @@ -4,6 +4,7 @@ private.event_frame = CreateFrame('Frame') private.event_listeners = {} private.threads = {} public.thread_id = nil +public.kill = {} function public.LOAD() m.event_frame:SetScript('OnUpdate', m.on_update) @@ -13,12 +14,18 @@ end function private.on_event() for _, listener in m.event_listeners do if event == listener.event and not listener.deleted then - listener.action() + listener.deleted = (listener.action() == m.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.deleted 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.deleted end) local threads = {} for thread_id, thread in m.threads do @@ -43,40 +50,15 @@ function private.on_update() end function public.event_listener(event, action) - local self = {} - - local listener = { event=event, action=action } - - function self:set_action(action) - listener.action = action - end - - function self:start() - tinsert(m.event_listeners, listener) - m.event_frame:RegisterEvent(event) - return self - end - - function self:stop() - listener.deleted = true - if not Aux.util.any(m.event_listeners, function(l) return l.event == event end) then - m.event_frame:UnregisterEvent(event) - end - return self - end - - return self + tinsert(m.event_listeners, { event=event, action=action }) + m.event_frame:RegisterEvent(event) end function public.on_next_event(event, callback) - local listener = m.event_listener(event) - - listener:set_action(function() - listener:stop() - return callback() + m.event_listener(event, function() + callback() + return m.kill end) - - listener:start() end function public.on_next_update(callback) diff --git a/core.lua b/core.lua index 335fd60..588a350 100644 --- a/core.lua +++ b/core.lua @@ -62,6 +62,7 @@ do rawget(self, '_f')()[key] = value end, __index = function(self, key) + if rawget(self, '_f')() == nil then error('', 2) end return rawget(self, '_f')()[key] end, __call = function(self) @@ -150,6 +151,8 @@ function public.on_load() this.money = arg1 end) + Aux.control.event_listener('CURSOR_UPDATE', m.CURSOR_UPDATE) + do local tab_group = m.gui.tab_group(AuxFrame, 'DOWN') tab_group:create_tab('Search') @@ -225,7 +228,6 @@ function m.on_addon_load.Blizzard_AuctionUI() m.hook('GetOwnerAuctionItems', m.GetOwnerAuctionItems) m.hook('PickupContainerItem', m.PickupContainerItem) - m.hook('PickupInventoryItem', m.PickupInventoryItem) m.hook('SetItemRef', m.SetItemRef) m.hook('UseContainerItem', m.UseContainerItem) m.hook('AuctionFrameAuctions_OnEvent', m.AuctionFrameAuctions_OnEvent) @@ -321,17 +323,13 @@ do if money >= amount then locked = true - local listener = m.control.event_listener('CHAT_MSG_SYSTEM') - listener:set_action(function() + m.control.event_listener('CHAT_MSG_SYSTEM', function() if arg1 == ERR_AUCTION_BID_PLACED then - listener:stop() - if on_success then - on_success() - end + Aux.call(on_success) locked = false + return Aux.control.kill end end) - listener:start() end end end @@ -352,17 +350,13 @@ do locked = true CancelAuction(index) - local listener = m.control.event_listener('CHAT_MSG_SYSTEM') - listener:set_action(function() + m.control.event_listener('CHAT_MSG_SYSTEM', function() if arg1 == ERR_AUCTION_REMOVED then - listener:stop() - if on_success then - on_success() - end + Aux.call(on_success) locked = false + return Aux.control.kill end end) - listener:start() end end @@ -426,15 +420,16 @@ end do -- TODO make it work for other ways to pick up things local last_picked_up + function private.CURSOR_UPDATE() + last_picked_up = nil + end function private.PickupContainerItem(...) local bag, slot = unpack(arg) - last_picked_up = { bag, slot } + Aux.control.on_next_update(function() + last_picked_up = { bag, slot } + end) return m.orig.PickupContainerItem(unpack(arg)) end - function private.PickupInventoryItem(...) - last_picked_up = nil - return m.orig.PickupInventoryItem(unpack(arg)) - end function public.cursor_item() if last_picked_up and CursorHasItem() then return m.info.container_item(unpack(last_picked_up))