new mod feature, helper functions for types and connections

This commit is contained in:
Ryan Ward 2024-11-11 00:51:28 -05:00
parent bbada260d7
commit dccd2791cc
2 changed files with 70 additions and 2 deletions

View File

@ -63,6 +63,9 @@ Table of contents
# Update 16.1.0 - TBA # Update 16.1.0 - TBA
Added 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.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: - `multi:newTimeout(seconds)` returns a connection that will trigger after a certain amount of time. See example below:
```lua ```lua
@ -85,6 +88,24 @@ multi:newThread(function()
os.exit() os.exit()
end) 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() multi:mainloop()
``` ```

View File

@ -76,12 +76,18 @@ end
local types = {} local types = {}
function multi.registerType(typ, p) function multi.registerType(typ, p)
if multi[typ:upper():gsub("_","")] then return typ end if multi["$"..typ:upper():gsub("_","")] then return typ end
multi[typ:upper():gsub("_","")] = typ multi["$"..typ:upper():gsub("_","")] = typ
table.insert(types, {typ, p or typ}) table.insert(types, {typ, p or typ})
return typ return typ
end end
function multi.hasType(typ)
if multi["$"..typ:upper():gsub("_","")] then
return multi["$"..typ:upper():gsub("_","")]
end
end
function multi.getTypes() function multi.getTypes()
return types return types
end end
@ -185,6 +191,25 @@ function multi.randomString(n)
return str return str
end 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 optimization_stats = {}
local ignoreconn = true local ignoreconn = true
local empty_func = function() end local empty_func = function() end
@ -221,6 +246,7 @@ function multi:newConnection(protect,func,kill)
for i = #conns, 1, -1 do for i = #conns, 1, -1 do
obj.rawadd = true obj.rawadd = true
obj(conns[i]) obj(conns[i])
obj.rawadd = false
end end
return obj return obj
end, end,
@ -230,6 +256,22 @@ function multi:newConnection(protect,func,kill)
obj2(function(...) obj2(function(...)
cn:Fire(obj1(...)) cn:Fire(obj1(...))
end) 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 else
error("Invalid mod!", type(obj1), type(obj2),"Expected function, connection(table)") error("Invalid mod!", type(obj1), type(obj2),"Expected function, connection(table)")
end end
@ -277,6 +319,7 @@ function multi:newConnection(protect,func,kill)
end end
end) end)
end end
return obj1
elseif type(obj1) == "table" and type(obj2) == "table" then elseif type(obj1) == "table" and type(obj2) == "table" then
-- --
else else
@ -492,6 +535,10 @@ function multi:newConnection(protect,func,kill)
return temp return temp
end end
function c:Get()
return fast
end
function c:Remove() function c:Remove()
local temp = fast local temp = fast
fast={} fast={}