Categories
Corona SDK Lua OOP

EventDispatcher docs

Documentation has been added for EventDispatcher in LDoc format, generated from the source file.

Here’s the basic usage:

local EvtD = require "EventDispatcher"

local dispatcher = EvtD()

-- listener as table
local listener = {
   eventName = function(event, ...)
      print(event.name, event.target, event.source)
   end
}

-- listener as function
local function listener(event, ...)
    print(event.name, event.target, event.source)
end

dispatcher:addEventListener( "eventName", listener ) -- or
dispatcher:on( "eventName", listener )

dispatcher:once( "eventName", listener )

dispatcher:hasEventListener( "eventName", listener )

dispatcher:dispatchEvent( { name="eventName" } ) -- or
dispatcher:dispatchEvent( "eventName" ) -- or
dispatcher:emit( { name="eventName" } ) -- or
dispatcher:emit( "eventName" )

dispatcher:removeEventListener( "eventName", listener )

dispatcher:removeAllListeners( "eventName" ) -- or
dispatcher:removeAllListeners()

dispatcher:printListeners()

All listeners receive the following fields in the parameter event table:

  • event.name (name of the event)
  • event.target (the listener itself)
  • event.source (the dispatcher)

Grab the latest code at Github.

Categories
Corona SDK Lua OOP

EventDispatcher update

Just a quick post that EventDispatcher has been updated with new features including new methods on(), emit(), once() that are similar to Node.js, a new method removeAllListeners(), and a debug function printListeners(). The on() and emit() methods are actually aliases to the addEventListener() and dispatchEvent() methods.

Chris Byerley (aka develephant) who developed Coronium.io and Coronium.gs is using EventDispatcher under the hood for the game server client code. Check out his projects if you’re a Lua / Corona developer.

Here’s the original post for EventDispatcher if you wish to read about it.

Grab the latest code at Github.