Round and pubsub lib.

This commit is contained in:
jussi
2024-03-17 10:38:30 +02:00
parent ca238975dc
commit ae1d0b65f1
18 changed files with 123 additions and 28 deletions

View File

@@ -0,0 +1,34 @@
local PubSub = {}
PubSub.__index = PubSub
function PubSub:new()
local object = setmetatable( {}, self )
object.signals = {}
return object
end
function PubSub:add( name )
self.signals[ name ] = {}
end
function PubSub:subscribe( name, func )
table.insert( self.signals[ name ], func )
end
function PubSub:unSubscribe( name, uFunc )
for i, func in ipairs( self.signals[ name ] ) do
if func == uFunc then
table.remove( self.signals[ name ], i )
end
end
end
function PubSub:publish( name, ... )
for _, func in ipairs( self.signals[ name ] ) do
func( ... )
end
end
return PubSub