Updated connections

This commit is contained in:
Ryan Ward 2023-01-15 13:28:45 -05:00
parent 3776fdff9d
commit a7a902acd6
2 changed files with 65 additions and 154 deletions

View File

@ -138,9 +138,18 @@ Added
Changed Changed
--- ---
- Connections internals changed, not too much changed on the surface.
- newConnection(protect, func, kill)
- `protect` disables fastmode, but protects the connection
- `func` uses `..` and appends func to the connection so it calls it after all connections run. There is some internal overhead added when using this, but it isn't much.
- `kill` removes the connection when fired
**Note:** When using protect/kill connections are triggered in reverse order
Removed Removed
--- ---
- conn:SetHelper(func) -- With the removal of old Connect this function is nolonger needed
- connection events can nolonger can be chained with connect. Connect only takes a function that you want to connect
Fixed Fixed
--- ---

210
init.lua
View File

@ -159,16 +159,16 @@ local CRef = {
]] ]]
local optimization_stats = {} local optimization_stats = {}
local ignoreconn = true local ignoreconn = true
function multi:newConnection(protect,func,kill) function multi:newConnection(protect, func, kill)
local c={} local c={}
local call_funcs = {} local call_funcs = {}
local lock = false local lock = false
c.__connectionAdded = function() end c.__connectionAdded = function() end
c.rawadd = false c.rawadd = false
c.callback = func
c.Parent = self c.Parent = self
setmetatable(c,{__call=function(self, ...) setmetatable(c,{
__call=function(self, ...) -- ()
local t = ... local t = ...
if type(t)=="table" then if type(t)=="table" then
for i,v in pairs(t) do for i,v in pairs(t) do
@ -186,7 +186,7 @@ function multi:newConnection(protect,func,kill)
return self:Connect(...) return self:Connect(...)
end end
end, end,
__mod = function(obj1, obj2) __mod = function(obj1, obj2) -- %
local cn = multi:newConnection() local cn = multi:newConnection()
if type(obj1) == "function" and type(obj2) == "table" then if type(obj1) == "function" and type(obj2) == "table" then
obj2(function(...) obj2(function(...)
@ -197,7 +197,7 @@ function multi:newConnection(protect,func,kill)
end end
return cn return cn
end, end,
__concat = function(obj1, obj2) __concat = function(obj1, obj2) -- ..
local cn = multi:newConnection() local cn = multi:newConnection()
local ref local ref
if type(obj1) == "function" and type(obj2) == "table" then if type(obj1) == "function" and type(obj2) == "table" then
@ -294,41 +294,16 @@ function multi:newConnection(protect,func,kill)
end end
function c:Lock() function c:Lock()
lock = true lock = self.Fire
self.Fire = function() end
return self return self
end end
function c:Unlock() function c:Unlock()
lock = false self.Fire = lock
return self return self
end end
if protect then
function c:Fire(...)
if lock then return end
for i=#call_funcs,1,-1 do
if not call_funcs[i] then return end
local suc, err = pcall(call_funcs[i],...)
if not suc then
print(err)
end
if kill then
table.remove(call_funcs,i)
end
end
end
else
function c:Fire(...)
if lock then return end
for i=#call_funcs,1,-1 do
call_funcs[i](...)
if kill then
table.remove(call_funcs,i)
end
end
end
end
function c:getConnections() function c:getConnections()
return call_funcs return call_funcs
end end
@ -345,75 +320,18 @@ function multi:newConnection(protect,func,kill)
end end
end end
function c:fastMode() function c:Fire(...)
if find_optimization then return self end for i=1,#call_funcs do
function self:Fire(...) call_funcs[i](...)
if lock then return end
for i=1,#call_funcs do
call_funcs[i](...)
end
end end
function self:Connect(func)
table.insert(call_funcs, func)
local temp = {fast = true}
setmetatable(temp,{
__call=function(s,...)
return self:Connect(...)
end,
__index = function(t,k)
if rawget(t,"root_link") then
return t["root_link"][k]
end
return nil
end,
__newindex = function(t,k,v)
if rawget(t,"root_link") then
t["root_link"][k] = v
end
rawset(t,k,v)
end,
})
temp.ref = func
if self.rawadd then
self.rawadd = false
else
self.__connectionAdded(temp, func)
end
return temp
end
return self
end end
function c:Bind(t) -- Not needed anymore, since it's so light, I'll leave it in forever
local temp = call_funcs function c:fastMode() return self end
call_funcs=t
return temp
end
function c:Remove()
local temp = call_funcs
call_funcs={}
return temp
end
local function conn_helper(self,func,name,num)
self.ID=self.ID+1
if num then
table.insert(call_funcs,num,func)
else
table.insert(call_funcs,1,func)
end
local temp = {
func=func,
Type="connector_link",
Parent=self,
connect = function(s,...)
return self:Connect(...)
end
}
function c:Connect(func)
table.insert(call_funcs, func)
local temp = {fast = true}
setmetatable(temp,{ setmetatable(temp,{
__call=function(s,...) __call=function(s,...)
return self:Connect(...) return self:Connect(...)
@ -431,68 +349,25 @@ function multi:newConnection(protect,func,kill)
rawset(t,k,v) rawset(t,k,v)
end, end,
}) })
temp.ref = func
function temp:Fire(...) if self.rawadd then
return call_funcs(...) self.rawadd = false
else
self.__connectionAdded(temp, func)
end end
function temp:Destroy()
for i=#call_funcs,1,-1 do
if call_funcs[i]~=nil then
if call_funcs[i]==self.func then
table.remove(call_funcs,i)
self.remove=function() end
multi.setType(temp,multi.DestroyedObj)
end
end
end
end
if name then
connections[name]=temp
end
if self.callback then
self.callback(temp)
end
return temp return temp
end end
function c:Connect(...) -- func, name, num function c:Bind(t)
local tab = {...} local temp = call_funcs
local funcs = {} call_funcs=t
for i = 1, #tab do return temp
if type(tab[i])=="function" then
funcs[#funcs + 1] = tab[i]
end
end
if #funcs>1 then
local ret = {}
for i = 1, #funcs do
local temp = conn_helper(self, funcs[i])
table.insert(ret, temp)
if self.rawadd then
self.rawadd = false
else
self.__connectionAdded(temp, funcs[i])
end
end
return ret
else
local conn = conn_helper(self, tab[1], tab[2], tab[3])
if self.rawadd then
self.rawadd = false
else
self.__connectionAdded(conn, tab[1])
end
return conn
end
end end
function c:SetHelper(func) function c:Remove()
conn_helper = func local temp = call_funcs
return self call_funcs={}
return temp
end end
if find_optimization then if find_optimization then
@ -504,9 +379,36 @@ function multi:newConnection(protect,func,kill)
c.HasConnections = c.hasConnections c.HasConnections = c.hasConnections
c.GetConnection = c.getConnection c.GetConnection = c.getConnection
if protect then -- Do some tests and override the fastmode if you want to do something differently
function c:Fire(...)
for i=#call_funcs,1,-1 do
if not call_funcs[i] then return end
local suc, err = pcall(call_funcs[i],...)
if not suc then
print(err)
end
if kill then
table.remove(call_funcs,i)
end
end
end
elseif kill then
function c:Fire(...)
for i=#call_funcs,1,-1 do
call_funcs[i](...)
table.remove(call_funcs,i)
end
end
end
if func then
c = c .. func
end
if not(ignoreconn) then if not(ignoreconn) then
multi:create(c) multi:create(c)
end end
return c return c
end end