Skip to content

moteus/lua-EventEmitter

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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