mudgangster

Tiny, scriptable MUD client
Log | Files | Refs | README

event.lua (666B)


      1 local Events = { }
      2 
      3 function mud.listen( name, callback, disabled )
      4 	enforce( name, "name", "string" )
      5 	enforce( callback, "callback", "function" )
      6 
      7 	local event = {
      8 		callback = callback,
      9 
     10 		enabled = not disabled,
     11 
     12 		enable = function( self )
     13 			self.enabled = true
     14 		end,
     15 		disable = function( self )
     16 			self.enabled = false
     17 		end,
     18 	}
     19 
     20 	if not Events[ name ] then
     21 		Events[ name ] = { }
     22 	end
     23 
     24 	table.insert( Events[ name ], event )
     25 
     26 	return event
     27 end
     28 
     29 function mud.event( name, ... )
     30 	enforce( name, "name", "string" )
     31 
     32 	if Events[ name ] then
     33 		for _, event in ipairs( Events[ name ] ) do
     34 			if event.enabled then
     35 				event.callback( ... )
     36 			end
     37 		end
     38 	end
     39 end