refactoring

This commit is contained in:
Manuel Simon Hirsig
2016-08-27 12:47:29 +02:00
parent abcc782daa
commit b734d14bea
12 changed files with 86 additions and 121 deletions
+39 -47
View File
@@ -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
+7 -29
View File
@@ -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
+12 -12
View File
@@ -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
+2 -2
View File
@@ -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()
+1 -1
View File
@@ -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)
+1 -1
View File
@@ -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
+12 -16
View File
@@ -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]
+2 -2
View File
@@ -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
+1 -1
View File
@@ -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
+2 -3
View File
@@ -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()
+3 -3
View File
@@ -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
+4 -4
View File
@@ -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