diff --git a/docs/changes.md b/docs/changes.md index 9aebff6..ebaacb0 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -63,6 +63,9 @@ Table of contents # Update 16.1.0 - TBA Added --- +- `multi.hasType(typ)` returns true if a type has been registered +- `multi.isMultiObj(obj)` returns true if the object is a multi object +- `multi.forwardConnection(src, dest)` forwards events from one connection to another connection. Doesn't modify anything and both connections are triggered when src is Fired, but not when dest is fired. - `multi.isTimeout(res)` returns true if the response it gets is a timeout type or a string equal to `multi.TIMEOUT`'s value - `multi:newTimeout(seconds)` returns a connection that will trigger after a certain amount of time. See example below: ```lua @@ -85,6 +88,24 @@ multi:newThread(function() os.exit() end) +multi:mainloop() +``` +- `connection % function` can now modify the arguments of a connection. See above example modified below +```lua +local multi, thread = require("multi"):init() + +local data = multi:newAlarm(1).OnRing % function() return {Type="request"}, "data is tasty" end + +multi:newThread(function() + res, data = thread.hold(data + multi:newTimeout(3)) + if multi.isTimeout(res) then + print("We timed out!") + else + print("We got the data:", data) + end + os.exit() +end) + multi:mainloop() ``` diff --git a/init.lua b/init.lua index ed62e9a..6c6c432 100644 --- a/init.lua +++ b/init.lua @@ -76,12 +76,18 @@ end local types = {} function multi.registerType(typ, p) - if multi[typ:upper():gsub("_","")] then return typ end - multi[typ:upper():gsub("_","")] = typ + if multi["$"..typ:upper():gsub("_","")] then return typ end + multi["$"..typ:upper():gsub("_","")] = typ table.insert(types, {typ, p or typ}) return typ end +function multi.hasType(typ) + if multi["$"..typ:upper():gsub("_","")] then + return multi["$"..typ:upper():gsub("_","")] + end +end + function multi.getTypes() return types end @@ -185,6 +191,25 @@ function multi.randomString(n) return str end +function multi.isMulitObj(obj) + if type(obj)=="table" then + if obj.Type ~= nil then + return multi.hasType(obj.Type) ~= nil + end + end + return false +end + +function multi.forwardConnection(src, dest) + if multi.isMulitObj(src) and multi.isMulitObj(dest) then + src(function(...) + dest:Fire(...) + end) + else + multi.error("Cannot forward non-connection objects") + end +end + local optimization_stats = {} local ignoreconn = true local empty_func = function() end @@ -221,6 +246,7 @@ function multi:newConnection(protect,func,kill) for i = #conns, 1, -1 do obj.rawadd = true obj(conns[i]) + obj.rawadd = false end return obj end, @@ -230,6 +256,22 @@ function multi:newConnection(protect,func,kill) obj2(function(...) cn:Fire(obj1(...)) end) + elseif type(obj1) == "table" and type(obj2) == "function" then + local conns = obj1:Bind({}) + for i = 1,#conns do + obj1(function(...) + conns[i](obj2(...)) + end) + end + obj1.__connectionAdded = function(conn, func) + obj1:Unconnect(conn) + obj1.rawadd = true + obj1:Connect(function(...) + func(obj2(...)) + end) + obj1.rawadd = false + end + return obj1 else error("Invalid mod!", type(obj1), type(obj2),"Expected function, connection(table)") end @@ -277,6 +319,7 @@ function multi:newConnection(protect,func,kill) end end) end + return obj1 elseif type(obj1) == "table" and type(obj2) == "table" then -- else @@ -492,6 +535,10 @@ function multi:newConnection(protect,func,kill) return temp end + function c:Get() + return fast + end + function c:Remove() local temp = fast fast={}