/lua-eventemitter

Node.js inspired EventEmitter for Lua

Primary LanguageLua

#LuaRock "eventemitter"

Node.js inspired EventEmitter for Lua

##Install EventEmitter is a listed LuaRock. Install using LuaRocks: luarocks install eventemitter

###Dependencies

  • Lua 5.2
  • lunitx for running the tests (if missing, installed along automatically by LuaRocks)

##Usage Start off with

require("luarocks.loader")
local EventEmitter = require("EventEmitter")

Create an EventEmitter object

local obj = EventEmitter:new()

Or spray EventEmitter on your existing object

local obj = {}
EventEmitter:new(obj)

Or on your newly created object

local obj = EventEmitter:new(ThatOtherType:new())

Let your object emit a named event on certain conditions

function obj:action ()
  self:emit("fire")
end

Listen for named events

obj:on("fire", function ()
  print("obj on fire!")
end)

You can pass data with the event

function obj:connect (address)
  local connection = self.connector:createConnection(address)
  if not connection then
    self:emit("error", "Could not connect to address " .. address)
    return
  end
  -- do connection things
end
obj:on("error", function (message)
  print("Error", message)
end)

##Tests See ./tst/init.lua

##License LGPL; see ./doc/LICENSE