Document new features to conns, todo fix newTask
This commit is contained in:
parent
e7ff234cef
commit
9ad2c45b8f
@ -367,6 +367,46 @@ Added
|
|||||||
return cn
|
return cn
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
- The len operator `#` will return the number of connections in the object!
|
||||||
|
```
|
||||||
|
local conn = multi:newConnection()
|
||||||
|
conn(function() print("Test 1") end)
|
||||||
|
conn(function() print("Test 2") end)
|
||||||
|
conn(function() print("Test 3") end)
|
||||||
|
conn(function() print("Test 4") end)
|
||||||
|
print(#conn)
|
||||||
|
```
|
||||||
|
Output:
|
||||||
|
```
|
||||||
|
4
|
||||||
|
```
|
||||||
|
- Connection objects can be negated -conn returns self so conn = -conn, reverses the order of connection events
|
||||||
|
```lua
|
||||||
|
local conn = multi:newConnection()
|
||||||
|
conn(function() print("Test 1") end)
|
||||||
|
conn(function() print("Test 2") end)
|
||||||
|
conn(function() print("Test 3") end)
|
||||||
|
conn(function() print("Test 4") end)
|
||||||
|
|
||||||
|
print("Fire 1")
|
||||||
|
conn:Fire()
|
||||||
|
conn = -conn
|
||||||
|
print("Fire 2")
|
||||||
|
conn:Fire()
|
||||||
|
```
|
||||||
|
Output:
|
||||||
|
```
|
||||||
|
Fire 1
|
||||||
|
Test 1
|
||||||
|
Test 2
|
||||||
|
Test 3
|
||||||
|
Test 4
|
||||||
|
Fire 2
|
||||||
|
Test 4
|
||||||
|
Test 3
|
||||||
|
Test 2
|
||||||
|
Test 1
|
||||||
|
```
|
||||||
- Connection objects can be divided, function / connection
|
- Connection objects can be divided, function / connection
|
||||||
This is a mix between the behavior between mod and concat, where the original connection can forward it's events to the new one as well as do a check like concat can. View it's implementation below:
|
This is a mix between the behavior between mod and concat, where the original connection can forward it's events to the new one as well as do a check like concat can. View it's implementation below:
|
||||||
```lua
|
```lua
|
||||||
|
|||||||
15
init.lua
15
init.lua
@ -196,7 +196,8 @@ function multi:newConnection(protect,func,kill)
|
|||||||
c.rawadd = false
|
c.rawadd = false
|
||||||
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
|
||||||
@ -214,6 +215,14 @@ function multi:newConnection(protect,func,kill)
|
|||||||
return self:Connect(...)
|
return self:Connect(...)
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
|
__unm = function(obj) -- -obj Reverses the order of connected events
|
||||||
|
local conns = obj:Bind({})
|
||||||
|
for i = #conns, 1, -1 do
|
||||||
|
obj.rawadd = true
|
||||||
|
obj(conns[i])
|
||||||
|
end
|
||||||
|
return obj
|
||||||
|
end,
|
||||||
__mod = function(obj1, obj2) -- %
|
__mod = function(obj1, obj2) -- %
|
||||||
local cn = self:newConnection()
|
local cn = self:newConnection()
|
||||||
if type(obj1) == "function" and type(obj2) == "table" then
|
if type(obj1) == "function" and type(obj2) == "table" then
|
||||||
@ -267,6 +276,8 @@ function multi:newConnection(protect,func,kill)
|
|||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
elseif type(obj1) == "table" and type(obj2) == "table" then
|
||||||
|
--
|
||||||
else
|
else
|
||||||
error("Invalid concat!", type(obj1), type(obj2),"Expected function/connection(table), connection(table)/function")
|
error("Invalid concat!", type(obj1), type(obj2),"Expected function/connection(table), connection(table)/function")
|
||||||
end
|
end
|
||||||
@ -391,6 +402,7 @@ function multi:newConnection(protect,func,kill)
|
|||||||
function c:Unconnect(conn)
|
function c:Unconnect(conn)
|
||||||
for i = 1, #fast do
|
for i = 1, #fast do
|
||||||
if fast[conn.ref] == fast[i] then
|
if fast[conn.ref] == fast[i] then
|
||||||
|
table.remove(self)
|
||||||
return table.remove(fast, i), i
|
return table.remove(fast, i), i
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -466,6 +478,7 @@ function multi:newConnection(protect,func,kill)
|
|||||||
if self.rawadd then
|
if self.rawadd then
|
||||||
self.rawadd = false
|
self.rawadd = false
|
||||||
else
|
else
|
||||||
|
table.insert(self,true)
|
||||||
self.__connectionAdded(temp, func)
|
self.__connectionAdded(temp, func)
|
||||||
end
|
end
|
||||||
return temp
|
return temp
|
||||||
|
|||||||
@ -90,15 +90,28 @@ multi, thread = require("multi"):init{print=true,warn=true,error=true,debugging=
|
|||||||
-- a:Destroy()
|
-- a:Destroy()
|
||||||
-- end)
|
-- end)
|
||||||
|
|
||||||
local func = thread:newFunction(function()
|
-- local func = thread:newFunction(function()
|
||||||
thread.sleep(4)
|
-- thread.sleep(4)
|
||||||
print("Hello!")
|
-- print("Hello!")
|
||||||
end)
|
-- end)
|
||||||
|
|
||||||
multi:newTLoop(func, 1)
|
-- multi:newTLoop(func, 1)
|
||||||
|
|
||||||
multi:mainloop()
|
-- multi:mainloop()
|
||||||
|
|
||||||
|
local conn = multi:newConnection()
|
||||||
|
conn(function() print("Test 1") end)
|
||||||
|
conn(function() print("Test 2") end)
|
||||||
|
conn(function() print("Test 3") end)
|
||||||
|
conn(function() print("Test 4") end)
|
||||||
|
|
||||||
|
print("Fire 1")
|
||||||
|
conn:Fire()
|
||||||
|
conn = -conn
|
||||||
|
print("Fire 2")
|
||||||
|
conn:Fire()
|
||||||
|
|
||||||
|
print(#conn)
|
||||||
|
|
||||||
-- local conn1, conn2, conn3 = multi:newConnection(nil,nil,true), multi:newConnection(), multi:newConnection()
|
-- local conn1, conn2, conn3 = multi:newConnection(nil,nil,true), multi:newConnection(), multi:newConnection()
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user