Skip to content

Latest commit

 

History

History
60 lines (45 loc) · 1.28 KB

README.md

File metadata and controls

60 lines (45 loc) · 1.28 KB

Emitter

Emitter allows for easy event binding and triggering between objects with a happy syntax. The emitter does not rely on metatables but duck typing.

To create an emitter

local emitter = Emitter.new()

An object can be passed into the constructor for easy mixin

local gui_element = Gui_Element.new()
Emitter.new(gui_element) -- yay for mixins

You bind with the on method

local jukebox = Emitter.new()
local function dance()
  every_body_dance_now()
end
jukebox:on('play', dance)
jukebox:trigger('play')

You unregister with the off method

local jukebox = Emitter.new()
local event_registration = jukebox:on('play', dance)

-- emergent undesirable behavior like finding out you need to take your car to the shop
jukebox:off('play') -- or
jukebox:off('play', dance) -- or
jukqbox:off(event_registration)

You can have an event only fire once

local emitter = Emitter.new()
local emitter:once('click', load_scene)

You can also specify a parent on the emitter

local child = Emitter.new()
local parent = Emitter.new()
child.parent = parent

parent:on('carnage', function() take_advil() end)
child.trigger('carnage')

There are quite a few subtlties not specified in the readme. I would recommend reading the spec or the source.