From 54c082cff93c9e60617dc1c6c6ade23580f3cc99 Mon Sep 17 00:00:00 2001 From: Manuel Simon Hirsig Date: Mon, 22 Aug 2016 15:58:43 +0200 Subject: [PATCH] big refactoring --- components/cache.lua | 1 + components/disenchant.lua | 15 +++--- core.lua | 2 +- init.lua | 105 +++++++++++++++++++++++++++++++++----- util/core.lua | 14 +---- 5 files changed, 104 insertions(+), 33 deletions(-) diff --git a/components/cache.lua b/components/cache.lua index d9eaa8b..219e99d 100644 --- a/components/cache.lua +++ b/components/cache.lua @@ -14,6 +14,7 @@ _g.aux_merchant_sell = {} _g.aux_characters = {} function LOAD() + import 'persistence' 'info' scan_wdb() event_listener('MERCHANT_SHOW', on_merchant_show) diff --git a/components/disenchant.lua b/components/disenchant.lua index 464cb57..222f721 100644 --- a/components/disenchant.lua +++ b/components/disenchant.lua @@ -59,15 +59,14 @@ function LOAD() 'INVTYPE_HOLDABLE' ) - weapon = set( - 'INVTYPE_2HWEAPON' , - 'INVTYPE_WEAPONMAINHAND', - 'INVTYPE_WEAPON', - 'INVTYPE_WEAPONOFFHAND', - 'INVTYPE_SHIELD', - 'INVTYPE_RANGED', + weapon = set + 'INVTYPE_2HWEAPON' + 'INVTYPE_WEAPONMAINHAND' + 'INVTYPE_WEAPON' + 'INVTYPE_WEAPONOFFHAND' + 'INVTYPE_SHIELD' + 'INVTYPE_RANGED' 'INVTYPE_RANGEDRIGHT' - ) end function public.value(slot, quality, level) diff --git a/core.lua b/core.lua index f043020..7906b15 100644 --- a/core.lua +++ b/core.lua @@ -65,7 +65,7 @@ function public.tab(index, name) local module_env = getfenv(2) local tab = {name=name, env=module_env} function module_env.public.getter.ACTIVE() return tab == active_tab end - for _, handler in temp-{'OPEN', 'CLOSE', 'CLICK_LINK', 'USE_ITEM'} do module_env.mutable[handler] = nil end + for _, handler in tmp-{'OPEN', 'CLOSE', 'CLICK_LINK', 'USE_ITEM'} do module_env.mutable[handler] = nil end tabs[index] = tab end do diff --git a/init.lua b/init.lua index 6ce85bd..38d7fae 100644 --- a/init.lua +++ b/init.lua @@ -6,23 +6,104 @@ module 'core' public.version = '5.0.0' do - local table_pool, auto_recycle = {}, {} - function public.wipe(t) -- like with a cloth or something - for k in t do t[k] = nil end; table.setn(t, 0) - end - function public.recycle(t) - auto_recycle[t] = nil; wipe(t); tinsert(table_pool, t) - end - public.temp = setmetatable({}, {__sub = function(_, t) auto_recycle[t] = true return t end}) - function public.getter.t() return tremove(table_pool) or {} end + local table_pool, temporary = {}, {} + CreateFrame 'Frame' :SetScript('OnUpdate', function() - for t in auto_recycle do recycle(t); log(getn(table_pool)) end - wipe(auto_recycle) + for t in temporary do + recycle(t) + log(getn(table_pool)) + end + wipe(temporary) end) + + function public.wipe(t) -- like with a cloth or something + for k in t do + t[k] = nil + end + table.setn(t, 0) + end + + function public.recycle(t) + auto_recycle[t] = nil + wipe(t) + tinsert(table_pool, t) + end + + function public.getter.t() return + tremove(table_pool) or {} + end + + function public.getter.tt() + local t = tremove(table_pool) or {} + temporary[t] = true + return t + end + + public.tmp = setmetatable({}, {__sub = function(_, t) temporary[t] = true return t end}) + + + do + local mt = { + __call=function(self, arg1, arg2) tinsert(self, arg2 or arg1); return self; end, + __index=function(self, key) tinsert(self, key); return self; end, + } + function public.getter.from() + return setmetatable(tt, mt) + end + end + + do + local mt = { + __call=function(self, arg) return self[1](arg) end, + __unm=function(self, arg) return self[1](arg) end, + } + function public.modifier(f) + local self = t; t[1] = f + return setmetatable(self, mt) + end + end + + do + local mt = {__call = function(self, key) return self[key] end} + public.getter.set = modifier(function(table) + local self = t + for _, v in table do self[v] = true end + recycle(table) + return setmetatable(self, mt) + end) + end + + + + function public.array(table) + local array = t + for k, v in table do tinsert(array, v) end + return array + end + + do + local mt = { + __call=function(self, arg1, arg2) self[arg2 or arg1] = true; return self; end, + __index=function(self, key) self[key] = true; return self; end, + } + function public.getter.metaset() + return setmetatable(t, mt) + end + end + + do + local mt = { + __call=function(self, arg1, arg2) self[arg2 or arg1] = true; return self; end, + __index=function(self, key) tinsert(self, key); return self; end, + } + function public.getter.metamap() + return setmetatable(t, mt) + end + end end local event_frame = CreateFrame 'Frame' -for _, event in temp-{'ADDON_LOADED', 'VARIABLES_LOADED', 'PLAYER_LOGIN', 'AUCTION_HOUSE_SHOW', 'AUCTION_HOUSE_CLOSED', 'AUCTION_BIDDER_LIST_UPDATE', 'AUCTION_OWNED_LIST_UPDATE'} do +for _, event in tmp-{'ADDON_LOADED', 'VARIABLES_LOADED', 'PLAYER_LOGIN', 'AUCTION_HOUSE_SHOW', 'AUCTION_HOUSE_CLOSED', 'AUCTION_BIDDER_LIST_UPDATE', 'AUCTION_OWNED_LIST_UPDATE'} do event_frame:RegisterEvent(event) end diff --git a/util/core.lua b/util/core.lua index 5702432..34fbfc2 100644 --- a/util/core.lua +++ b/util/core.lua @@ -33,9 +33,9 @@ end do local state local function f() - local tmp = state + local temp = state state = nil - return tmp + return temp end function public.present(v) state = v; return f @@ -201,16 +201,6 @@ function public.map(xs, f) return ys end -do - local mt = {__call = function(self, key) return self[key] end} - function public.set(t) - local self = {} - for i=1,getn(t) do self[t[i]] = true end - recycle(t) - return setmetatable(self, mt) - end -end - function public.trim(str) return gsub(str, '^%s*(.-)%s*$', '%1') end