diff --git a/docs/changes.md b/docs/changes.md index ba4d36d..a959786 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -367,6 +367,25 @@ Added return cn end ``` +- 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 + __div = function(obj1, obj2) -- / + local cn = self:newConnection() + local ref + if type(obj1) == "function" and type(obj2) == "table" then + obj2(function(...) + local args = {obj1(...)} + if args[1] then + cn:Fire(multi.unpack(args)) + end + end) + else + multi.error("Invalid divide! ", type(obj1), type(obj2)," Expected function/connection(table)") + end + return cn + end + ``` - Connection objects can now be concatenated with functions, not each other. For example: ```lua multi, thread = require("multi"):init{print=true,findopt=true} diff --git a/init.lua b/init.lua index 44914db..703bc4b 100644 --- a/init.lua +++ b/init.lua @@ -225,6 +225,21 @@ function multi:newConnection(protect,func,kill) end return cn end, + __div = function(obj1, obj2) -- / + local cn = self:newConnection() + local ref + if type(obj1) == "function" and type(obj2) == "table" then + obj2(function(...) + local args = {obj1(...)} + if args[1] then + cn:Fire(multi.unpack(args)) + end + end) + else + multi.error("Invalid divide!", type(obj1), type(obj2),"Expected function/connection(table)") + end + return cn + end, __concat = function(obj1, obj2) -- .. local cn = self:newConnection() local ref