noleak-emitter

0.3.0 • Public • Published

noleak-emitter

Rank

Version Build status

Wrapper for Simple Event Emitter ( ex1: "util('events').EventEmitter", ex2: "browser-emitter" ). On "error" or "end" event, you can unbind the listeners automatically.

Install

Install with npm:

npm install noleak-emitter

API - Set functions by args

    // On node.js, "GLOBAL.Emitter" or "require('events').EventEmitter"
    // will be inherited.
    var NoleakEmitter = require('noleak-emitter');
    var emitter = new NoleakEmitter();
    emitter.on('hoge', function(){ console.log(arguments) });
    emitter.emit('hoge', 'a', 'b', 'c'); // => 'a', 'b', 'c'
    emitter.emit('end');
    
    setImmediate(function(){
      emitter.emit('hoge', 'd'); // => (no output)
    });

also use on browser

<script type="text/javascript" src="Emitter.js"></script>
<script type="text/javascript" src="NoleakEmitter.js"></script>
<script type="text/javascript">
 
    // On browser, some simple event emitter must be exported
    // on global-scope(window).
    var emitter = new NoleakEmitter();
    emitter.on('hoge', function(){ console.log(arguments) });
    emitter.emit('hoge', 'a', 'b', 'c'); // => ['a', 'b', 'c']
    emitter.emit('end');
    
    setTimeout(function(){
      emitter.emit('hoge', 'd'); // => (no output)
    }, 4);
 
</script> 

if you want to inherit Emitter to another class, use prototype chain.

    // for Factory
    var SubClass = function(){
      NoleakEmitter.call(this);
    }
    for(var i in NoleakEmitter.prototype)
      SubClass.prototype[i] = NoleakEmitter.prototype[i];
 
    // for Singleton (not recommended)
    var SubClass = function(){
      this.__proto__.__proto__ = new NoleakEmitter();
    }

Dependencies (0)

    Dev Dependencies (4)

    Package Sidebar

    Install

    npm i noleak-emitter

    Weekly Downloads

    1

    Version

    0.3.0

    License

    none

    Unpacked Size

    8.63 kB

    Total Files

    8

    Last publish

    Collaborators

    • ystskm