Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wscherphof/lua-eventemitter
Node.js inspired EventEmitter for Lua
https://github.com/wscherphof/lua-eventemitter
Last synced: about 1 month ago
JSON representation
Node.js inspired EventEmitter for Lua
- Host: GitHub
- URL: https://github.com/wscherphof/lua-eventemitter
- Owner: wscherphof
- Created: 2013-05-25T05:14:33.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2013-05-27T03:28:44.000Z (over 11 years ago)
- Last Synced: 2023-03-24T12:32:43.737Z (over 1 year ago)
- Language: Lua
- Size: 133 KB
- Stars: 4
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
#LuaRock "eventemitter"
Node.js inspired EventEmitter for Lua
##Install
EventEmitter is a listed [LuaRock](http://luarocks.org/repositories/rocks/). Install using [LuaRocks](http://www.luarocks.org/): `luarocks install eventemitter`###Dependencies
- [Lua 5.2](http://www.lua.org/download.html)
- [lunitx](https://github.com/dcurrie/lunit) for running the tests (if missing, installed along automatically by LuaRocks)##Usage
Start off with
```lua
require("luarocks.loader")
local EventEmitter = require("EventEmitter")
```
Create an EventEmitter object
```lua
local obj = EventEmitter:new()
```
Or spray EventEmitter on your existing object
```lua
local obj = {}
EventEmitter:new(obj)
```
Or on your newly created object
```lua
local obj = EventEmitter:new(ThatOtherType:new())
```
Let your object emit a named event on certain conditions
```lua
function obj:action ()
self:emit("fire")
end
```
Listen for named events
```lua
obj:on("fire", function ()
print("obj on fire!")
end)
```
You can pass data with the event
```lua
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`