Implement EventEmitter on Lua
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
examples
rockspecs
src/lua
test
.travis.yml
LICENSE
README.md

README.md

lua-EventEmitter

Build Status Coverage Status License

Implementation of EventEmitter for Lua.

Extend 30log class

local EE    = require "EventEmitter"
local class = require "30log"

-- Create wrapper for EventEmitter
local EventEmitter = EE.extend(class('EventEmitter')) do

function EventEmitter:init()
  -- object by default should have `_EventEmitter` property
  self._EventEmitter = EE.EventEmitter.new{self = self}
end

end

local Window = EventEmitter:extend('Window')

local window = Window:new()

window:on('resize', function(self, event, w, h)
  print(self, ("Resized %dx%d"):format(w, h))
end)

window:emit('resize', 125, 250)

Wrap some table with emitter

local EventEmitter = require "EventEmitter"

local server = {}

EventEmitter.extend_object(server)

server:on('accept', function() ... end)

server:emit('accept', host, port)

Incompatabilities with EventEmitter2

  1. Lua version has no defined order of call listners
  2. JS Allow register same function for same event multiple times.
server.on('A', test);
server.on('A', test);
server.emit('A')       // `test` called twice
  1. If event has wildcard followed by some value then mult-level wildcard does not works
server.on('foo.**', test);
server.emit('foo.*')       // called
server.emit('foo.*.bar')   // not called
  1. In JS Multi-level key in middle of mask does not allo zero match with wildcard event.
server.on('foo.**.bar', test);
server.emit('foo.*')   // not called
server.emit('foo.bar') // called