Publish your own packages to the world's most popular software ecosystem with npm Pro.Get started »

noleak-emitter

0.2.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();
    }

Install

npm i noleak-emitter

DownloadsWeekly Downloads

3

Version

0.2.0

License

none

Last publish

Collaborators

  • avatar