diff --git a/docs/changes.md b/docs/changes.md index a959786..d638d78 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -367,6 +367,46 @@ Added return cn 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 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 diff --git a/init.lua b/init.lua index 703bc4b..709ae0c 100644 --- a/init.lua +++ b/init.lua @@ -196,7 +196,8 @@ function multi:newConnection(protect,func,kill) c.rawadd = false c.Parent = self - setmetatable(c,{__call=function(self,...) + setmetatable(c,{ + __call=function(self,...) local t = ... if type(t)=="table" then for i,v in pairs(t) do @@ -214,6 +215,14 @@ function multi:newConnection(protect,func,kill) return self:Connect(...) 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) -- % local cn = self:newConnection() if type(obj1) == "function" and type(obj2) == "table" then @@ -267,6 +276,8 @@ function multi:newConnection(protect,func,kill) end end) end + elseif type(obj1) == "table" and type(obj2) == "table" then + -- else error("Invalid concat!", type(obj1), type(obj2),"Expected function/connection(table), connection(table)/function") end @@ -391,6 +402,7 @@ function multi:newConnection(protect,func,kill) function c:Unconnect(conn) for i = 1, #fast do if fast[conn.ref] == fast[i] then + table.remove(self) return table.remove(fast, i), i end end @@ -466,6 +478,7 @@ function multi:newConnection(protect,func,kill) if self.rawadd then self.rawadd = false else + table.insert(self,true) self.__connectionAdded(temp, func) end return temp diff --git a/tests/test.lua b/tests/test.lua index 6a0479e..a9ec6e8 100644 --- a/tests/test.lua +++ b/tests/test.lua @@ -90,15 +90,28 @@ multi, thread = require("multi"):init{print=true,warn=true,error=true,debugging= -- a:Destroy() -- end) -local func = thread:newFunction(function() - thread.sleep(4) - print("Hello!") -end) +-- local func = thread:newFunction(function() +-- thread.sleep(4) +-- print("Hello!") +-- 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()