From b734d14bea38e84a8c56906ba13165756b37f4ea Mon Sep 17 00:00:00 2001 From: Manuel Simon Hirsig Date: Sat, 27 Aug 2016 12:47:29 +0200 Subject: [PATCH] refactoring --- aux-addon.lua | 86 +++++++++++++++++++---------------------- control.lua | 36 ++++------------- core/history.lua | 24 ++++++------ core/scan.lua | 4 +- frame.lua | 2 +- gui/auction_listing.lua | 2 +- libs/module.lua | 28 ++++++-------- tabs/post/core.lua | 4 +- tabs/post/frame.lua | 2 +- tabs/search/core.lua | 5 +-- tabs/search/frame.lua | 6 +-- tabs/search/results.lua | 8 ++-- 12 files changed, 86 insertions(+), 121 deletions(-) diff --git a/aux-addon.lua b/aux-addon.lua index c31d6a1..2d273be 100644 --- a/aux-addon.lua +++ b/aux-addon.lua @@ -1,4 +1,4 @@ ---aux_account_settings = {} -- TODO +--aux_account_settings = {} -- TODO clean up the mess of savedvariables --aux_character_settings = {} module() @@ -21,31 +21,22 @@ do }) end -do - local bids_loaded - public.bids_loaded - { - get = function() return bids_loaded end, - set = function(value) bids_loaded = value end, - } -end -do - local current_owner_page - public.current_owner_page - { - get = function() return current_owner_page end, - set = function(value) current_owner_page = value end, - } -end +local bids_loaded +function public.bids_loaded.get() return bids_loaded end + +local current_owner_page +function public.current_owner_page.get() return current_owner_page end public.empty = setmetatable({}, {__metatable=false, __newindex=error}) do local pool, overflow_pool, transient = {}, setmetatable({}, {__mode='v'}), {} + CreateFrame'Frame':SetScript('OnUpdate', function() for t in transient do recycle(t) end wipe(transient) end) + function public.wipe(t) -- like with a cloth or something for k in t do t[k] = nil end t.reset = 1 @@ -53,6 +44,7 @@ do table.setn(t, 0) return setmetatable(t, nil) end + function public.recycle(t) wipe(t) if getn(pool) < 50 then @@ -60,8 +52,9 @@ do else overflow_pool[t] = true end - log(getn(table_pool), '-', getn(weak_pool)) +-- log(getn(table_pool), '-', getn(weak_pool)) end + function public.t.get() return tremove(pool) or tremove(overflow_pool, next(overflow_pool)) or {} end @@ -70,6 +63,7 @@ do transient[t] = true return t end + function public.operator_mt(f) local function apply(self, value) local raw = self.raw @@ -79,25 +73,22 @@ do return {__unm=function(self) self.raw = true end, __call=apply, __sub=apply} end do - local function make_transient(t) transient[t] = true return t end - local function make_persistent(t) transient[t] = nil return t end - do - local mt = operator_mt(make_transient) - public.temp - { - get = function() return setmetatable(t, mt) end, - set = function(t) return make_transient(t) end, - } - end - do - local mt = operator_mt(make_persistent) - public.perm - { - get = function() return setmetatable(t, mt) end, - set = function(t) return make_persistent(t) end, - } - end + local mt = operator_mt(function(t) transient[t] = true return t end) + public.temp + { + get = function() return setmetatable(t, mt) end, + set = function(t) return make_transient(t) end, + } end + do + local mt = operator_mt(function(t) transient[t] = nil return t end) + public.perm + { + get = function() return setmetatable(t, mt) end, + set = function(t) return make_persistent(t) end, + } + end + local function insert_keys(t,k1,k2,k3,k4,k5,k6,k7,k8,k9,k10,k11,k12,k13,k14,k15,k16,k17,k18,k19,k20,overflow) if overflow ~= nil then error 'Overflow.' end if k1 ~= nil then t[k1] = true end @@ -164,10 +155,11 @@ do return {__call=f, __unm=function(self) return setmetatable(self, nil) end} end local set_mt, list_mt, object_mt = collector_mt(insert_keys), collector_mt(insert_values), collector_mt(insert_pairs) - public.property() - set, list, object = {get=function() return setmetatable(t, set_mt) end}, {get=function() return setmetatable(t, list_mt) end}, {get=function() return setmetatable(t, object_mt) end} + public() + function set.list() return setmetatable(t, list_mt) end + function set.set() return setmetatable(t, set_mt) end + function set.object() return setmetatable(t, object_mt) end private() - -- TODO or 'auto' 'transient'? end local event_frame = CreateFrame 'Frame' @@ -219,12 +211,12 @@ do end do - local active_tab_index - function property.active_tab.get() return tab_info[active_tab_index] end - function on_tab_click(index) - call(active_tab_index and active_tab.CLOSE) - active_tab_index = index - call(active_tab_index and active_tab.OPEN) + local index + function active_tab.get() return tab_info[index] end + function on_tab_click(i) + call(index and active_tab.CLOSE) + index = i + call(index and active_tab.OPEN) end end @@ -314,7 +306,7 @@ end function AUCTION_HOUSE_SHOW() AuctionFrame:Hide() aux_frame:Show() - set_tab(1) + tab = 1 end function AUCTION_HOUSE_CLOSED() @@ -323,7 +315,7 @@ function AUCTION_HOUSE_CLOSED() post.stop() stack.stop() scan.abort() - set_tab() + tab = nil aux_frame:Hide() end diff --git a/control.lua b/control.lua index 6999447..5bbb6e1 100644 --- a/control.lua +++ b/control.lua @@ -2,32 +2,10 @@ aux 'core' event_frame = CreateFrame 'Frame' -do - local t = t - listeners - { - get = function() return t end, - set = function(v) t = v end, - } -end -do - local t = t - threads - { - get = function() return t end, - set = function(v) t = v end, - } -end +local listeners, threads = t, t -do - local thread_id - public.thread_id - { - get = function() return thread_id end, - set = function(value) thread_id = value end, - } - -end +local thread_id +function public.thread_id.get() return thread_id end function LOAD() event_frame:SetScript('OnUpdate', UPDATE) @@ -49,7 +27,7 @@ function UPDATE() end end - listeners = filter(listeners, function(l) return not l.killed end) + listeners = filter(listeners, function(l) return not l.killed end) -- TODO wipe tables instead threads = filter(threads, function(th) return not th.killed end) for id, thread in threads do @@ -68,17 +46,17 @@ end do local id = 0 - function property.id.get() id = id + 1; return id end + function id.get() id = id + 1; return id end end function public.kill_listener(listener_id) - for _, listener in {listeners[listener_id]} do + for listener in present(listeners[listener_id]) do listener.killed = true end end function public.kill_thread(thread_id) - for _, thread in {threads[thread_id]} do + for thread in present(threads[thread_id]) do thread.killed = true end end diff --git a/core/history.lua b/core/history.lua index fa658cc..257363f 100644 --- a/core/history.lua +++ b/core/history.lua @@ -5,30 +5,30 @@ local history_schema = {'record', '#', {next_push='number'}, {daily_min_buyout=' value_cache = t do - local data - function property.data.get() - if not data then + local cache + function data.get() + if not cache then local dataset = persistence.dataset - data = dataset.history or t - dataset.history = data + cache = dataset.history or t + dataset.history = cache end - return data + return cache end end do - local next_push = 0 - function property.next_push.get() - if time() > next_push then + local time = 0 + function next_push.get() + if time() > time then local date = date '*t' date.hour, date.min, date.sec = 24, 0, 0 - next_push = time(date) + time = time(date) end - return next_push + return time end end -function property.new_record.get() +function new_record.get() return -object('next_push', next_push, 'data_points', t) end diff --git a/core/scan.lua b/core/scan.lua index 308ded1..dd5cc73 100644 --- a/core/scan.lua +++ b/core/scan.lua @@ -37,13 +37,13 @@ do call(on_complete) end - function property.state.get() + function state.get() local _, state = next(filter(scan_states, function(state) return state.id == thread_id end)) return state end end -function property.query.get() return state.params.queries[state.query_index] end +function query.get() return state.params.queries[state.query_index] end function wait_for_callback(...) temp=arg local send_signal, signal_received = signal() diff --git a/frame.lua b/frame.lua index 899df51..cc6f821 100644 --- a/frame.lua +++ b/frame.lua @@ -28,7 +28,7 @@ end do tabs = gui.tabs(aux_frame, 'DOWN') tabs._on_select = on_tab_click - function public.set_tab(id) tabs:select(id) end + function public.tab.set(id) tabs:select(id) end end do local btn = gui.button(aux_frame, 16) diff --git a/gui/auction_listing.lua b/gui/auction_listing.lua index 6134a11..4692aa8 100644 --- a/gui/auction_listing.lua +++ b/gui/auction_listing.lua @@ -647,7 +647,7 @@ local methods = { elseif IsShiftKeyDown() and ChatFrameEditBox:IsVisible() then ChatFrameEditBox:Insert(this.row.data.record.link) elseif not modified and button == 'RightButton' then -- TODO not when alt (how?) - set_tab(1) + tab = 1 search_tab.set_filter(strlower(info.item(this.row.data.record.item_id).name)..'/exact') search_tab.execute(nil, false) else diff --git a/libs/module.lua b/libs/module.lua index e551de9..8d3ad31 100644 --- a/libs/module.lua +++ b/libs/module.lua @@ -36,26 +36,22 @@ do if ACCESS[key] and not state.declaration_access then state.declaration_access = ACCESS[key] elseif not state.declaration_name or declaration_error() then - state.declaration_name = key + state.declaration_name = typeof(key) == 'string' and key or declaration_error() end return self end function declarator_mt:__newindex(key, value) local state=_state[self] --- if module.access[key] then return end - local type - if state.declaration_name then - type = TYPE[key] or declaration_error() - else - type = typeof(value) == 'function' and FUNCTION or GETTER - end + local type = state.declaration_name + and (TYPE[key] or declaration_error()) + or typeof(value) == 'function' and FUNCTION or GETTER declare(state, state.declaration_access, key, {[type]=value}) state.declaration_access, state.declaration_name = nil, nil end function declarator_mt:__call(value) local state=_state[self] if state.declaration_name then - local success, f, get, set = pcall(extract, value) + local success, f, getter, setter = pcall(extract, value) if not success then declaration_error() end - declare(state, state.declaration_access, state.declaration_name, {[FUNCTION]=f, [GETTER]=get, [SETTER]=set}) + declare(state, state.declaration_access, state.declaration_name, {[FUNCTION]=f, [GETTER]=getter, [SETTER]=setter}) elseif state.declaration_access or declaration_error() then state.default_access = state.declaration_access end @@ -65,15 +61,15 @@ end local env_mt = {__metatable=false} function env_mt:__index(key) local state=_state[self] - local get = state[GETTER][key] - if get then return get() end + local getter = state[GETTER][key] + if getter then return getter() end local f = state[FUNCTION][key] if f then return f else return _G[key] end end function env_mt:__newindex(key, value) local state=_state[self] if state.access[key] then - local f = state[SETTER][key] - if f then f(value) end + local setter = state[SETTER][key] + if setter then setter(value) end else return state.declarator[key] end @@ -82,8 +78,8 @@ end local interface_mt = {__metatable=false} function interface_mt:__index(key) local state=_state[self] if state.access[key] == PUBLIC then - local get = state[GETTER][key] - if get then return get() else return state[FUNCTION][key] end + local getter = state[GETTER][key] + if getter then return getter() else return state[FUNCTION][key] end end end function interface_mt:__newindex(key, value) local state=_state[self] diff --git a/tabs/post/core.lua b/tabs/post/core.lua index 70f98d6..60faa1f 100644 --- a/tabs/post/core.lua +++ b/tabs/post/core.lua @@ -4,13 +4,13 @@ aux 'post_tab' local scan, scan_util, post, history, info, persistence, item_lis local DURATION_4, DURATION_8, DURATION_24 = 120, 480, 1440 local settings_schema = {'record', '#', {stack_size='number'}, {duration='number'}, {start_price='number'}, {buyout_price='number'}, {hidden='boolean'}} -function property.default_settings.get() +function default_settings.get() return -object('duration', DURATION_8 , 'stack_size', 1, 'start_price', 0, 'buyout_price', 0, 'hidden', false) end do local data - function property.data.get() + function data.get() if not data then local dataset = persistence.dataset data = dataset.post or t diff --git a/tabs/post/frame.lua b/tabs/post/frame.lua index d45f3df..e540162 100644 --- a/tabs/post/frame.lua +++ b/tabs/post/frame.lua @@ -46,7 +46,7 @@ function create_frames() if arg1 == 'LeftButton' then set_item(this.item_record) elseif arg1 == 'RightButton' then - set_tab(1) + tab = 1 search_tab.set_filter(strlower(info.item(this.item_record.item_id).name)..'/exact') search_tab.execute(nil, false) end diff --git a/tabs/search/core.lua b/tabs/search/core.lua index aee0146..4fe5c4b 100644 --- a/tabs/search/core.lua +++ b/tabs/search/core.lua @@ -51,7 +51,7 @@ RESULTS, SAVED, FILTER = 1, 2, 3 function LOAD() create_frames() - set_subtab(SAVED) + subtab = SAVED update_auto_buy_filter() new_search '' current_search.placeholder = true @@ -78,8 +78,7 @@ function USE_ITEM(item_info) execute(nil, false) end -function set_subtab(tab) - +function subtab.set(tab) search_results_button:UnlockHighlight() saved_searches_button:UnlockHighlight() new_filter_button:UnlockHighlight() diff --git a/tabs/search/frame.lua b/tabs/search/frame.lua index b79dbf6..60b1227 100644 --- a/tabs/search/frame.lua +++ b/tabs/search/frame.lua @@ -247,7 +247,7 @@ function create_frames() btn:SetWidth(243) btn:SetHeight(22) btn:SetText 'Search Results' - btn:SetScript('OnClick', function() set_subtab(RESULTS) end) + btn:SetScript('OnClick', function() subtab = RESULTS end) search_results_button = btn end do @@ -256,7 +256,7 @@ function create_frames() btn:SetWidth(243) btn:SetHeight(22) btn:SetText 'Saved Searches' - btn:SetScript('OnClick', function() set_subtab(SAVED) end) + btn:SetScript('OnClick', function() subtab = SAVED end) saved_searches_button = btn end do @@ -265,7 +265,7 @@ function create_frames() btn:SetWidth(243) btn:SetHeight(22) btn:SetText 'Filter Builder' - btn:SetScript('OnClick', function() set_subtab(FILTER) end) + btn:SetScript('OnClick', function() subtab = FILTER end) new_filter_button = btn end do diff --git a/tabs/search/results.lua b/tabs/search/results.lua index 6babec1..8e6b463 100644 --- a/tabs/search/results.lua +++ b/tabs/search/results.lua @@ -80,13 +80,13 @@ do function previous_search() search_box:ClearFocus() update_search(search_index - 1) - set_subtab(RESULTS) + subtab = RESULTS end function next_search() search_box:ClearFocus() update_search(search_index + 1) - set_subtab(RESULTS) + subtab = RESULTS end end @@ -281,7 +281,7 @@ function start_search(queries, continuation) search.status_bar:set_text 'Scan complete' if current_search == search and frame.results:IsVisible() and getn(search.records) == 0 then - set_subtab(SAVED) + subtab = SAVED end search.active = false @@ -362,7 +362,7 @@ function public.execute(resume, real_time) current_search.active = true update_start_stop() - set_subtab(RESULTS) + subtab = RESULTS if real_time then start_real_time_scan(queries[1], nil, continuation) else