Send custom events with EventDispatcher

In order to broadcast messages to event listeners, the event dispatcher is limited to either the global Runtime object or display objects. This limitation places messaging in the two scopes instead of between objects that are supposed to be talking to each other. This just doesn’t feel right to me; so I’m releasing yet another EventDispatcher, perhaps other developers may find it useful too.

Those who came from the good old Flash 5 days may remember FLEM (FLash Event Model). It was the first listener event model for Flash and ActionScript, and was created by Branden Hall and further developed and maintained by me. I’ve adapted the basic event model mechanism found in ActionScript 2/3 to Lua. This EventDispatcher has a similar API as the listener model in Corona SDK, with some extra features thrown in (such as optional extra parameters when dispatching events, and returning status).

EventDispatcher provides custom event broadcaster/listener mechanism to regular Lua objects, it works as regular Lua 5.1 / 5.2 code, in Corona SDK, and likely other Lua-based frameworks.

Basic usage:

local EvtD = require "EventDispatcher" local listener = {     eventName = function(event)         print(event.name)     end } local broadcaster = EvtD() broadcaster:addEventListener( "eventName", listener ) broadcaster:hasEventListener( "eventName", listener ) broadcaster:dispatchEvent( { name="eventName" } ) broadcaster:removeEventListener( "eventName", listener )

More info here: http://swfoo.com/?p=632

Enjoy!

Dave Yang - Quantumwave Interactive Inc.
http://qwmobile.com
http://swfoo.com

As a former Flash developer, this is awesome!

Thanks.

Dave this is awesome!! Who needs FSMs when you got event-driven stuff?! I can’t wait to really play around with this!!

-Mario

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.

http://swfoo.com/?p=718

Cheers,
Dave

Way cool!  Thanks Dave!  :smiley:

Thanks, very cool. I have this saved for my next project.

As a former Flash developer, this is awesome!

Thanks.

Dave this is awesome!! Who needs FSMs when you got event-driven stuff?! I can’t wait to really play around with this!!

-Mario

Documentation for EventDispatcher has been added (finally!): http://bit.ly/1vYakaY

Latest code can be found at: https://github.com/daveyang/EventDispatcher

Cheers,
Dave

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.

http://swfoo.com/?p=718

Cheers,
Dave

Way cool!  Thanks Dave!  :smiley:

Thanks, very cool. I have this saved for my next project.

Documentation for EventDispatcher has been added (finally!): http://bit.ly/1vYakaY

Latest code can be found at: https://github.com/daveyang/EventDispatcher

Cheers,
Dave