refactoring

This commit is contained in:
Manuel Simon Hirsig
2016-09-04 14:03:37 +02:00
parent a4d497b47b
commit cfa7ed5d14
6 changed files with 43 additions and 46 deletions
+4 -4
View File
@@ -122,9 +122,9 @@ do
local index
function private.active_tab.get() return tab_info[index] end
function on_tab_click(i)
call(index and active_tab.CLOSE)
(index and active_tab.CLOSE or nop)()
index = i
call(index and active_tab.OPEN)
;(index and active_tab.OPEN or nop)()
end
end
@@ -172,7 +172,7 @@ do
locked = true
event_listener('CHAT_MSG_SYSTEM', function(kill)
if arg1 == ERR_AUCTION_BID_PLACED then
call(on_success)
(on_success or nop)()
locked = false
kill()
end
@@ -190,7 +190,7 @@ do
CancelAuction(index)
event_listener('CHAT_MSG_SYSTEM', function(kill)
if arg1 == ERR_AUCTION_REMOVED then
call(on_success)
(on_success or nop)()
locked = false
kill()
end
+4 -4
View File
@@ -27,14 +27,14 @@ do
end
end
for _, state in aborted do
call(state.params.on_abort)
(state.params.on_abort or nop)()
end
end
function complete()
local on_complete = state.params.on_complete
scan_states[state.params.type] = nil
call(on_complete)
(on_complete or nop)()
end
function private.state.get()
@@ -101,7 +101,7 @@ end
function submit_query()
when(function() return state.params.type ~= 'list' or CanSendAuctionQuery() end, function()
call(state.params.on_submit_query)
(state.params.on_submit_query or nop)()
state.last_query_time = GetTime()
if state.params.type == 'bidder' then
GetBidderAuctionItems(state.page)
@@ -151,7 +151,7 @@ function scan_page(i)
history.process_auction(auction_info)
if call(state.params.auto_buy_validator, auction_info) then
if (state.params.auto_buy_validator or nop)(auction_info) then
local send_signal, signal_received = signal()
when(signal_received, recurse)
place_bid(auction_info.query_type, auction_info.index, auction_info.buyout_price, L(send_signal, true))
+2 -4
View File
@@ -107,11 +107,9 @@ function public.stop()
if state then
kill_thread(state.thread_id)
local callback, slot = state.callback, state.target_slot
if slot and not matching_item(slot) then
slot = nil
end
slot = slot and matching_item(slot) and slot or nil
state = nil
call(callback, slot)
(callback or nop)(slot)
end
end
+12 -12
View File
@@ -267,7 +267,7 @@ do
function mt.__index:select(id)
self._selected = id
self:update()
call(self._on_select, id)
;(self._on_select or nop)(id)
end
function mt.__index:update()
for _, tab in self._tabs do
@@ -304,13 +304,13 @@ function public.editbox(parent)
editbox:SetHeight(24)
editbox:SetTextColor(0, 0, 0, 0)
set_content_style(editbox)
local function colorize() this.display:SetText(call(this.colorizer, this:GetText()) or this:GetText()) end
local function format() this.display:SetText(call(this.formatter or this.colorizer, this:GetText()) or this:GetText()) end
local function colorize() this.display:SetText((this.colorizer or nop)(this:GetText()) or this:GetText()) end
local function format() this.display:SetText((this.formatter or this.colorizer or nop)(this:GetText()) or this:GetText()) end
editbox:SetScript('OnEscapePressed', function()
this:ClearFocus()
call(this.escape)
;(this.escape or nop)()
end)
editbox:SetScript('OnEnterPressed', function() call(this.enter) end)
editbox:SetScript('OnEnterPressed', function() (this.enter or nop)() end)
editbox:SetScript('OnEditFocusGained', function()
colorize()
this.focused = true
@@ -318,7 +318,7 @@ function public.editbox(parent)
this:SetScript('OnUpdate', function()
this.cursor:SetAlpha(mod(floor((GetTime()-this.cursor.last_change) * 2 + 1), 2))
end)
call(this.focus_gain)
;(this.focus_gain or nop)()
end)
editbox:SetScript('OnEditFocusLost', function()
format()
@@ -326,17 +326,17 @@ function public.editbox(parent)
this.cursor:SetAlpha(0)
this:HighlightText(0, 0)
this:SetScript('OnUpdate', nil)
call(this.focus_loss)
;(this.focus_loss or nop)()
end)
editbox:SetScript('OnTextChanged', function()
if this.focused then colorize() else format() end
call(this.change)
(this.change or nop)()
end)
editbox:SetScript('OnCursorChanged', function()
this.cursor.last_change = GetTime()
this.cursor:SetPoint(this:GetJustifyH(), this, this:GetJustifyH(), call(arg1 > 0 and max or min, 0, arg1 - 1), 1.5)
this.cursor:SetPoint(this:GetJustifyH(), this, this:GetJustifyH(), (arg1 > 0 and max or min)(0, arg1 - 1), 1.5)
end)
editbox:SetScript('OnChar', function() call(this.char) end)
editbox:SetScript('OnChar', function() (this.char or nop)() end)
do
local last_click
editbox:SetScript('OnMouseDown', function()
@@ -398,7 +398,7 @@ function public.status_bar(parent)
status_bar:SetFrameLevel(level + 2)
status_bar:SetScript('OnUpdate', function()
if this:GetValue() < 100 then
this:SetAlpha(1 - ((math.sin(GetTime()*PI)+1)/2)/2)
this:SetAlpha(1 - (sin(GetTime() * 180) + 1) / 4)
else
this:SetAlpha(1)
end
@@ -416,7 +416,7 @@ function public.status_bar(parent)
status_bar:SetFrameLevel(level + 3)
status_bar:SetScript('OnUpdate', function()
if this:GetValue() < 100 then
this:SetAlpha(1 - ((math.sin(GetTime()*PI)+1)/2)/2)
this:SetAlpha(1 - (sin(GetTime() * 180) + 1) / 4)
else
this:SetAlpha(1)
end
+8 -8
View File
@@ -3,10 +3,10 @@ green_t = module
local next, setn, type, setmetatable = next, table.setn, type, setmetatable
local wipe, release, acquire, acquire_temp
local pool, pool_size, overflow_pool, tmp = {}, 0, setmetatable({}, {__mode='k'}), {}
local pool, pool_size, overflow_pool, auto_release = {}, 0, setmetatable({}, {__mode='k'}), {}
CreateFrame('Frame'):SetScript('OnUpdate', function()
for t in tmp do release(t) end; wipe(tmp)
for t in auto_release do release(t) end; wipe(auto_release)
end)
function wipe(t)
@@ -18,7 +18,7 @@ end
public.wipe = wipe
function release(t)
wipe(t); tmp[t] = nil
wipe(t); auto_release[t] = nil
if pool_size < 50 then
pool_size = pool_size + 1
pool[pool_size] = t
@@ -39,7 +39,7 @@ function acquire()
end
public.t.get = acquire
function acquire_temp() local t = acquire(); tmp[t] = true; return t end
function acquire_temp() local t = acquire(); auto_release[t] = true; return t end
public.tt.get = acquire_temp
do local mt = {__newindex=nop}
@@ -47,14 +47,14 @@ do local mt = {__newindex=nop}
end
do local f, mt
f = function(_, v) if type(v) == 'table' then tmp[v] = true end return v end
f = function(_, v) if type(v) == 'table' then auto_release[v] = true end return v end
mt = {__call=f, __sub=f}
public.temp {get=function() return setmetatable(acquire_temp(), mt) end, set=function(t) tmp[t] = true end}
public.temp {get=function() return setmetatable(acquire_temp(), mt) end, set=function(t) auto_release[t] = true end}
end
do local f, mt
f = function(_, v) if type(v) == 'table' then tmp[v] = nil end return v end
f = function(_, v) if type(v) == 'table' then auto_release[v] = nil end return v end
mt = {__call=f, __sub=f}
public.perm {get=function() return setmetatable(acquire_temp(), mt) end, set=function(t) tmp[t] = nil end}
public.perm {get=function() return setmetatable(acquire_temp(), mt) end, set=function(t) auto_release[t] = nil end}
end
--T :kek(5) :foo(1) :bar{} :moo 'trump' TODO possible alternative constructor syntax, but high overhead, more elegant because not limited, sometimes better looking.
+13 -14
View File
@@ -2,6 +2,15 @@ aux 'core'
public.join = _G.table.concat
do local mt = {__index=function(self, key) setmetatable(self, nil); return getfenv(2)[key] end}
function public.__(t) return setmetatable(t, mt) end
end
function public.range(arg1, arg2)
local i, n = arg2 and arg1 or 1, arg2 or arg1
if i <= n then return first, range(i + 1, n) end
end
function public.replicate(count, value)
if count > 0 then return value, replicate(count - 1, value) end
end
@@ -36,32 +45,22 @@ do
end
return f(unpack(params))
end
local a1b2 = {a=1, b=2}
function public.L(body, ...)
if type(body) == 'function' then
local arg1 = arg
return function(...) return helper(body, arg1, arg) end
else
error(debugstack(1, 10))
body = gsub(body, '_([ab])', function(char) return '_'..a1b2[char] end)
local lambda = loadstring 'return function(_1,_2,_3,_4,_5,_6,_7,_8,_9)'..f..' end'
-- body = gsub(body, '_([ab])', function(char) return '_'.. end)
-- local lambda = loadstring('return function(_1,_2,_3,_4,_5,_6,_7,_8,_9)'..body..' end')
local lambda = loadstring('return '..body) or loadstring(body)
setfenv(lambda, getfenv(2))
return lambda
end
end
end
function public.call(f, ...) temp=arg
if f then
return f(unpack(arg))
end
end
function public.index(t, ...) temp=arg
for i = 1, arg.n do
t = t and t[arg[i]]
end
return t
for i = 1, arg.n do t = t and t[arg[i]] end return t
end
public.huge = 1.8*10^308