提供一个小工具 events.lua , NodeJS 的 EventEmitter 的 lua 实现,支持弱引用,无需管理回收问题

代码地址:https://github.com/yi/events.lua

说明如下:

# events.lua

An event emitter implementation in lua, api similar to (http://nodejs.org/api/events.html)

## 如何使用  Usage

``` coffee

events = require "events"

person =
  name: "Johnny"
  age: "very old"

events.EventEmitter(person)   -- make person an EventEmitter

handlers =
  hello: (who, howmany)->
    print " who:#{who.name}, #{howmany} times"
    assert.are.equal who, person

  helloOnce: (who, howmany)->
    print " who:#{who.name}, #{howmany} times"
    assert.are.equal who, person

  helloToRemove: (who, howmany)->
    print " who:#{who.name}, #{howmany} times"
    assert.are.equal who, person

  goodday: (who, howmany)->
    print " who:#{who.name}, #{howmany} times"
    assert.are.equal who, person

person\on "hello", handlers.hello
person\on "hello", handlers.helloToRemove
person\once "hello", handlers.helloOnce
person\on "goodday", handlers.goodday

person\emit "hello", person, 1      -- will fire: hello, helloToRemove, helloOnce
person\emit "hello", person, 2      -- will fire: hello, helloToRemove

person\off "hello", handlers.helloToRemove
person\emit "hello", person, 3      -- will fire: hello

person\emit "goodday", person, 1    -- fire goodday
person\removeAllListeners "hello"

person\emit "hello", person, 4      -- fire no-one
person\emit "goodday", person, 2    -- fire goodday

events.EventEmitter person, true    --should output:  table: 0x7fc142d70500 is already an EventEmitter


```

## 支持回调方法的弱引用,简化析构 Weak Reference Support

```coffee

person =
  name: "Walker"
  age: "long enough"

events.EventEmitter(person, true)   -- (WEAK KEY) make person an EventEmitter

handlers =
  hello: (who, howmany)->
    print " who:#{who.name}, #{howmany} times"
    assert.are.equal who, person

  helloOnce: (who, howmany)->
    print " who:#{who.name}, #{howmany} times"
    assert.are.equal who, person

  helloToRemove: (who, howmany)->
    print " who:#{who.name}, #{howmany} times"
    assert.are.equal who, person

person\on "hello", handlers.hello
person\on "hello", handlers.helloToRemove
person\once "hello", handlers.helloOnce
person\on "goodday", handlers.goodday

person\emit "hello", person, 1      -- will fire: hello, helloToRemove, helloOnce

handlers.hello = nil
collectgarbage "collect"            -- handlers.hello should be gced
person\emit "hello", person, 2      -- only fire: helloToRemove

```




## Test

Test specs are written in (http://www.moonscript.com/) on (https://github.com/Olivine-Labs/busted)

To run tests, do:

```sh
busted -v spec/
```






```

:3: :3:
我刚想说 谁呀 乱乱的~
结果一看 是斑竹大人呀~
:11::11:

看我的签名,这个论坛的输入不是一般地难用啊