Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Send custom events with EventDispatcher
Started by qwidave Aug 19 2014 10:37 PM

* * * * * 1 votes
6 replies to this topic
event dispatcher eventdispatcher listener oop custom event objects
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

qwidave

[GLOBAL: userInfoPane.html]
qwidave
  • Pro
  • PipPipPip
  • 36 posts
  • Advanced Member

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


[TOPIC: post.html]
#2

develephant

[GLOBAL: userInfoPane.html]
develephant
  • Pro
  • PipPipPipPipPipPip
  • 425 posts
  • Jedi

As a former Flash developer, this is awesome!

 

Thanks.



[TOPIC: post.html]
#3

mroberti

[GLOBAL: userInfoPane.html]
mroberti
  • Enterprise
  • PipPipPipPipPipPip
  • 271 posts
  • Jedi

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

 

-Mario



[TOPIC: post.html]
#4

qwidave

[GLOBAL: userInfoPane.html]
qwidave
  • Pro
  • PipPipPip
  • 36 posts
  • Advanced Member

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

[TOPIC: post.html]
#5

develephant

[GLOBAL: userInfoPane.html]
develephant
  • Pro
  • PipPipPipPipPipPip
  • 425 posts
  • Jedi

Way cool!  Thanks Dave!  :D



[TOPIC: post.html]
#6

jonjonsson

[GLOBAL: userInfoPane.html]
jonjonsson
  • Pro
  • PipPipPipPipPipPip
  • 951 posts
  • Jedi

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



[TOPIC: post.html]
#7

qwidave

[GLOBAL: userInfoPane.html]
qwidave
  • Pro
  • PipPipPip
  • 36 posts
  • Advanced Member

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

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

Cheers,
Dave


[topic_controls]
[/topic_controls]



Also tagged with one or more of these keywords: event, dispatcher, eventdispatcher, listener, oop, custom event, objects