module refactoring

This commit is contained in:
Manuel Simon Hirsig
2017-10-15 14:56:43 +02:00
committed by Simon Hirsig
parent 5e093306e6
commit 62ef2683f5
15 changed files with 69 additions and 97 deletions
+12 -12
View File
@@ -25,10 +25,10 @@ M.print = vararg-function(arg)
end
local bids_loaded
function M.get_bids_loaded() return bids_loaded end
function M.get.bids_loaded() return bids_loaded end
local current_owner_page
function M.get_current_owner_page() return current_owner_page end
function M.get.current_owner_page() return current_owner_page end
local event_frame = CreateFrame'Frame'
for event in pairs(temp-S('ADDON_LOADED', 'VARIABLES_LOADED', 'PLAYER_LOGIN', 'AUCTION_HOUSE_SHOW', 'AUCTION_HOUSE_CLOSED', 'AUCTION_BIDDER_LIST_UPDATE', 'AUCTION_OWNED_LIST_UPDATE')) do
@@ -37,10 +37,10 @@ end
do
local handlers, handlers2 = {}, {}
function M.set_LOAD(f)
function M.set.LOAD(f)
tinsert(handlers, f)
end
function M.set_LOAD2(f)
function M.set.LOAD2(f)
tinsert(handlers2, f)
end
event_frame:SetScript('OnEvent', function()
@@ -88,7 +88,7 @@ do
for scope in pairs(temp-S('character', 'faction', 'realm', 'account')) do
local scope = scope
M[scope .. '_data'] = function(key, init)
if not cache[scope] then error('Cache not ready', 2) end
if not cache[scope] then error('Cache for ' .. scope .. ' data not ready.', 2) end
cache[scope][key] = cache[scope][key] or {}
for k, v in pairs(init or empty) do
if cache[scope][key][k] == nil then
@@ -104,16 +104,16 @@ tab_info = {}
function M.TAB(name)
local tab = O('name', name)
local env = getfenv(2)
function env.set_OPEN(f) tab.OPEN = f end
function env.set_CLOSE(f) tab.CLOSE = f end
function env.set_USE_ITEM(f) tab.USE_ITEM = f end
function env.set_CLICK_LINK(f) tab.CLICK_LINK = f end
function env.set.OPEN(f) tab.OPEN = f end
function env.set.CLOSE(f) tab.CLOSE = f end
function env.set.USE_ITEM(f) tab.USE_ITEM = f end
function env.set.CLICK_LINK(f) tab.CLICK_LINK = f end
tinsert(tab_info, tab)
end
do
local index
function get_active_tab() return tab_info[index] end
function get.active_tab() return tab_info[index] end
function on_tab_click(i)
CloseDropDownMenus()
do (index and active_tab.CLOSE or nop)() end
@@ -159,7 +159,7 @@ end
do
local locked
function M.get_bid_in_progress() return locked end
function M.get.bid_in_progress() return locked end
function M.place_bid(type, index, amount, on_success)
if locked then return end
local money = GetMoney()
@@ -184,7 +184,7 @@ end
do
local locked
function M.get_cancel_in_progress() return locked end
function M.get.cancel_in_progress() return locked end
function M.cancel_auction(index, on_success)
if locked then return end
locked = true
+2 -2
View File
@@ -5,7 +5,7 @@ local event_frame = CreateFrame'Frame'
local listeners, threads = T, T
local thread_id
function M.get_thread_id() return thread_id end
function M.get.thread_id() return thread_id end
function LOAD()
event_frame:SetScript('OnUpdate', UPDATE)
@@ -50,7 +50,7 @@ end
do
local id = 0
function get_unique_id()
function get.unique_id()
id = id + 1
return id
end
+2 -2
View File
@@ -15,7 +15,7 @@ end
do
local next_push = 0
function get_next_push()
function get.next_push()
if time() > next_push then
local date = date('*t')
date.hour, date.min, date.sec = 24, 0, 0
@@ -25,7 +25,7 @@ do
end
end
function get_new_record()
function get.new_record()
return temp-O('next_push', next_push, 'data_points', T)
end
+2 -2
View File
@@ -49,7 +49,7 @@ do
do (on_complete or nop)() end
end
function get_state()
function get.state()
for _, state in pairs(scan_states) do
if state.id == thread_id then
return state
@@ -58,7 +58,7 @@ do
end
end
function get_query()
function get.query()
return state.params.queries[state.query_index]
end
+1 -1
View File
@@ -32,7 +32,7 @@ end
do
tabs = gui.tabs(AuxFrame, 'DOWN')
tabs._on_select = on_tab_click
function M.set_tab(id) tabs:select(id) end
function M.set.tab(id) tabs:select(id) end
end
do
local btn = gui.button(AuxFrame)
+1 -1
View File
@@ -88,7 +88,7 @@ M.font_size = immutable-{
do
local id = 1
function M.get_unique_name()
function M.get.unique_name()
id = id + 1
return 'AuxFrame' .. id
end
+1 -1
View File
@@ -56,7 +56,7 @@ do
M.static = setmetatable({}, {__metatable=false, __newindex=nop, __call=f, __sub=f})
end
M.get_T = acquire
M.get.T = acquire
do
local function ret(t)
+24 -52
View File
@@ -1,62 +1,34 @@
if module then return end
local strfind, type, setmetatable, setfenv, _G = strfind, type, setmetatable, setfenv, getfenv(0)
local error, nop, define, include, create_module, nop_default_mt, public_modifier_mt, proxy_mt
local _G, setfenv, setmetatable, rawget = getfenv(0), setfenv, setmetatable, rawget
local loaded, defined, interfaces, environments = {}, {}, {}, {}
function nop() end
function error(msg, ...) return _G.error(format(msg or '', unpack(arg)) .. '\n' .. debugstack(), 0) end
local function nop() end
function define(self, k, v, private)
if type(k) ~= 'string' or not strfind(k, '^[_%a][_%w]*') then error('Invalid identifier "%s".', k) end
local _, _, prefix, suffix = strfind(k, '^(.?.?.?.?)([_%a].*)')
local module = loaded[self]
local signature = (private and '-' or '+') .. k
module.defined[signature] = module.defined[signature] and error('Duplicate identifier "%s".', signature) or true
if private or not module.defined['-' .. k] then
module.fields[k] = v
if prefix == 'get_' then module.accessors[suffix] = v elseif prefix == 'set_' then module.mutators[suffix] = v end
end
if not private then
module.public_fields[k] = v
if prefix == 'get_' then module.public_accessors[suffix] = v elseif prefix == 'set_' then module.public_mutators[suffix] = v end
end
local function include(private, name)
local P = loaded[name] or error('No such module.', 2)
for k, v in P.public do private[k] = v end
end
function include(self, name)
local module = name and loaded[name] or error('No module "%s".', name)
for k, v in pairs(module.public_fields) do define(self, k, v, true) end
end
public_modifier_mt = {__metatable=false, __newindex=define}
nop_default_mt = {__index=function() return nop end}
function proxy_mt(fields, mutators)
return {__metatable=false, __index=fields, __newindex=function(_, k, v) return mutators[k](v) end}
end
function create_module(name)
if type(name) ~= 'string' then error('Invalid module name "%s".', name) end
local P, environment, interface, public_modifier, accessors, mutators, fields, public_accessors, public_mutators, public_fields
environment, interface, public_modifier = {}, {}, setmetatable({}, public_modifier_mt)
accessors = {M=function() return public_modifier end}
mutators = setmetatable({_=nop}, {__index=function(_, k) return function(v) define(name, k, v, true) end end})
fields = setmetatable(
{_M=environment, _G=_G, include=function(interface) include(name, interface) end, nop=nop},
{__index=function(_, k) local accessor = accessors[k]; if accessor then return accessor() else return _G[k] end end}
local function create_module(name)
local P, private, public, modifier
local modifier = setmetatable({}, {
__metatable=false,
__newindex=function(_, k, v)
public[k] = v
if rawget(private, k) == nil then
private[k] = v
end
end
})
local env = setmetatable(
{_G=_G, _M=private, M=modifier, include=function(name) include(private, name) end, nop=nop},
{__index=_G}
)
public_accessors = setmetatable({}, nop_default_mt)
public_mutators = setmetatable({}, nop_default_mt)
public_fields = setmetatable({}, {__index=function(_, k) return public_accessors[k]() end})
setmetatable(environment, proxy_mt(fields, mutators))
setmetatable(interface, proxy_mt(public_fields, public_mutators))
P = {
defined = {['-_M']=true, ['-_G']=true, ['-include']=true, ['-nop']=true, ['-M']=true, ['-set__']=true, ['-require']=true},
fields = fields, accessors = accessors, mutators = mutators,
public_fields = public_fields, public_accessors = public_accessors, public_mutators = public_mutators,
}
environments[name], interfaces[name] = environment, interface
loaded[name], loaded[public_modifier] = P, P
local public = {}
local interface = setmetatable({}, {__metatable=false, __index=public})
P = {private=private, public=public}
environments[name], interfaces[name] = env, interface
loaded[name] = P
end
function module(name) if not loaded[name] then create_module(name) end defined[name] = true; setfenv(2, environments[name]) end
+13 -13
View File
@@ -22,7 +22,7 @@ local settings_schema = {'tuple', '#', {duration='number'}, {start_price='number
local scan_id, inventory_records, bid_records, buyout_records = 0, {}, {}, {}
function get_default_settings()
function get.default_settings()
return O('duration', DURATION_8, 'start_price', 0, 'buyout_price', 0, 'hidden', false)
end
@@ -41,16 +41,16 @@ end
do
local bid_selections, buyout_selections = {}, {}
function get_bid_selection()
function get.bid_selection()
return bid_selections[selected_item.key]
end
function set_bid_selection(record)
function set.bid_selection(record)
bid_selections[selected_item.key] = record
end
function get_buyout_selection()
function get.buyout_selection()
return buyout_selections[selected_item.key]
end
function set_buyout_selection(record)
function set.buyout_selection(record)
buyout_selections[selected_item.key] = record
end
end
@@ -63,14 +63,14 @@ end
do
local item
function get_selected_item() return item end
function set_selected_item(v) item = v end
function get.selected_item() return item end
function set.selected_item(v) item = v end
end
do
local c = 0
function get_refresh() return c end
function set_refresh(v) c = v end
function get.refresh() return c end
function set.refresh(v) c = v end
end
function OPEN()
@@ -88,21 +88,21 @@ function USE_ITEM(item_info)
select_item(item_info.item_key)
end
function get_unit_start_price()
function get.unit_start_price()
return selected_item and read_settings().start_price or 0
end
function set_unit_start_price(amount)
function set.unit_start_price(amount)
local settings = read_settings()
settings.start_price = amount
write_settings(settings)
end
function get_unit_buyout_price()
function get.unit_buyout_price()
return selected_item and read_settings().buyout_price or 0
end
function set_unit_buyout_price(amount)
function set.unit_buyout_price(amount)
local settings = read_settings()
settings.buyout_price = amount
write_settings(settings)
+2 -2
View File
@@ -44,7 +44,7 @@ function USE_ITEM(item_info)
execute(nil, false)
end
function set_subtab(tab)
function set.subtab(tab)
CloseDropDownMenus()
search_results_button:UnlockHighlight()
saved_searches_button:UnlockHighlight()
@@ -65,7 +65,7 @@ function set_subtab(tab)
end
end
function M.set_filter(filter_string)
function M.set.filter(filter_string)
search_box:SetFocus()
search_box:SetText(filter_string)
end
+3 -3
View File
@@ -105,7 +105,7 @@ function update_form()
end
end
function get_filter_builder_query()
function get.filter_builder_query()
local filter_string
local function add(part)
@@ -150,7 +150,7 @@ function get_filter_builder_query()
return filter_string or ''
end
function set_form(filter)
function set.form(filter)
clear_form()
for _, component in ipairs(filter.components) do
if component[1] == 'blizzard' then
@@ -184,7 +184,7 @@ end
function import_filter_string()
local filter, error = filter_util.parse_filter_string(select(3, strfind(search_box:GetText(), '^([^;]*)')))
if filter or print(error) then
set_form(filter)
set.form(filter)
end
update_form()
end
+3 -3
View File
@@ -12,10 +12,10 @@ end
do
local id = 0
function get_search_scan_id()
function get.search_scan_id()
return id
end
function set_search_scan_id(v)
function set.search_scan_id(v)
id = v
end
end
@@ -36,7 +36,7 @@ do
local searches = {}
local search_index = 1
function get_current_search()
function get.current_search()
return searches[search_index]
end
+1 -1
View File
@@ -82,7 +82,7 @@ handlers = {
end
}
function get_auto_buy_validator()
function get.auto_buy_validator()
local validators = T
for _, search in pairs(favorite_searches) do
if search.auto_buy then
+1 -1
View File
@@ -40,7 +40,7 @@ end
M.huge = 1.8 * 10 ^ 308
function M.get_modified()
function M.get.modified()
return IsShiftKeyDown() or IsControlKeyDown() or IsAltKeyDown()
end
+1 -1
View File
@@ -418,7 +418,7 @@ function M.item_quality_index(item_quality)
end
end
function M.get_inventory()
function M.get.inventory()
local bag, slot = 0, 0
return function()
if not GetBagName(bag) or slot >= GetContainerNumSlots(bag) then