putil-callable

A callable class implementation

Usage no npm install needed!

<script type="module">
  import putilCallable from 'https://cdn.skypack.dev/putil-callable';
</script>

README

putil-callable

NPM Version NPM Downloads Build Status Test Coverage DevDependencies

This package contains two very handy callable class implementations. Callable: Base extensible callable class CallableEventEmitter: Mixin of Callable and EventEmitter classes.

Installation

  • $ npm install putil-callable --save

Usage

Extend Callable in ES6

const {Callable} = require('putil-callable');

class MyCallable extends Callable {

  constructor() {
    super('handle');
  }

  handle(msg) {
    console.log('MyCallable:', msg);
  }
}
const obj1 = new MyCallable();
obj1('This is a callable class');

Extend Callable in ES5

const Callable = require('putil-callable').Callable;

function MyCallable() {  
  return Callable.call(this, 'handle');  
}
MyCallable.prototype = Object.create(Callable.prototype);
MyCallable.prototype.constructor = MyCallable;
MyCallable.prototype.handle = function(msg) {
    console.log('MyCallable:', msg);
};

const obj1 = new MyCallable();
obj1('This is a callable class');

Extend CallableEventEmitter in ES6

const {CallableEventEmitter} = require('putil-callable');

class MyCallable extends CallableEventEmitter {

  constructor() {
    super();
  }

  __call__(msg) {
    this.emit('msg', msg);
  }
}

const obj2 = new MyCallable();

obj2.on('msg', function(msg) {
  console.log('CallableEventEmitter:', msg);
});

Node Compatibility

  • node >= 1;

License

MIT