diff --git a/README.md b/README.md index 08fc129..c135c81 100644 --- a/README.md +++ b/README.md @@ -3,15 +3,15 @@ [![Actual version published on NPM](https://badge.fury.io/js/evemit.png)](https://www.npmjs.org/package/evemit) [![npm module downloads per month](http://img.shields.io/npm/dm/evemit.svg)](https://www.npmjs.org/package/evemit) -Minimal and fast JavaScript event emitter for Node.js and front-end.
+Minimal and fast JavaScript / TypeScript event emitter for Node.js and front-end.
Only 1kb minified (577 bytes gzipped). - ## Getting started ### Install Via `NPM` + ```shell npm install evemit --save ``` @@ -22,8 +22,9 @@ If `Evemit` is used in CommonJS environment (Node.js, Browserify, Webpack, ...), it is exposed as module with `module.exports`. So in CommonJS, _evemit_ is not exposed in the global scope (even on client side) :) + ```js -var ev = require('evemit'); +const ev = require('evemit'); // true (it's the constructor) console.log(typeof ev === 'function'); @@ -33,6 +34,7 @@ console.log(typeof Evemit); ``` Basic usage (without CommonJS) + ```js // true console.log(typeof Evemit === 'function'); @@ -44,12 +46,12 @@ console.log(typeof window.Evemit === 'function'); ### Usage ```js -var Evemit = require('evemit'); +const Evemit = require('evemit'); // Or if you are not in an environment CommonJS (Node.js, Browserify, Webpack, ...) // uses directly `Evemit`, without `var Evemit = require('evemit')` -var obj = new Evemit(); +const obj = new Evemit(); obj.on('say-hello', function(hello) { console.log(hello); // Hello World! @@ -58,7 +60,6 @@ obj.on('say-hello', function(hello) { obj.emit('say-hello', 'Hello World!'); ``` - ## API See the complete [API doc](API.md). @@ -107,6 +108,7 @@ obj.emit('ping'); ``` With arguments passed to the listeners + ```js obj.emit('ping', 'arg1', 'arg2', {an: 'object'}); ``` @@ -130,24 +132,28 @@ a.off('say-hello', myCallback); ### Evemit.listeners({string} [event]) Get all listeners + ```js // Returns an array containing all listeners obj.listeners(); ``` Count all listeners + ```js // Returns a number. obj.listeners().length; ``` Get all listeners of a given event + ```js // Returns an array of listeners obj.listeners('say-hello'); ``` Count all listeners of a given event + ```js obj.listeners('say-hello').length; ``` @@ -155,6 +161,7 @@ obj.listeners('say-hello').length; The methods of the `Array` object can be used to manage the listeners. Example + ```js // reverse the order of the listeners execution obj.listeners('my-event').reverse(); @@ -163,25 +170,26 @@ obj.listeners('my-event').reverse(); obj.listeners('my-event').slice(1, 3); // Removes the first listener of the stack, and returns that listener -var firstListener = obj.listeners('my-event').shift(); +const firstListener = obj.listeners('my-event').shift(); // Removes the last listener of the stack, and returns that listener -var lastListener = obj.listeners('my-event').pop(); +const lastListener = obj.listeners('my-event').pop(); // ... ``` Use the property `obj.events` if you want to get an object like + ``` {event1: [array of listeners], event2: [array of listeners], ...} ``` - ## Unit tests `evemit` is unit tested with [Unit.js](http://unitjs.com). Run the tests + ```shell cd node_modules/evemit @@ -190,12 +198,10 @@ npm test To execute the tests on client side, download the `test` directory and go on _test/index.html_ file with your browser. - ## LICENSE [MIT](https://github.com/Nicolab/evemit/blob/master/LICENSE) (c) 2014, Nicolas Tallefourtane. - ## Author | [![Nicolas Tallefourtane - Nicolab.net](http://www.gravatar.com/avatar/d7dd0f4769f3aa48a3ecb308f0b457fc?s=64)](http://nicolab.net) | diff --git a/evemit.d.ts b/evemit.d.ts new file mode 100644 index 0000000..ff60302 --- /dev/null +++ b/evemit.d.ts @@ -0,0 +1,84 @@ +/** + * @name Evemit + * @description Minimal and fast JavaScript event emitter for Node.js and front-end. + * @author Nicolas Tallefourtane + * @link https://github.com/Nicolab/evemit + * @license MIT https://github.com/Nicolab/evemit/blob/master/LICENSE + */ + +declare module 'evemit' { + class Evemit { + /** + * Events object. + */ + events: Record; + + /** + * Evemit + * + * @constructor + * @api public + */ + constructor(); + + /** + * Register a new event listener for a given event. + * + * @param {string} event Event name. + * @param {function} fn Callback function (listener). + * @param {*} [context] Context for function execution. + * @return {Evemit} Current instance. + * @api public + */ + on(event: string, fn: Function, context?: any): this; + + /** + * Add an event listener that's only called once. + * + * @param {string} event Event name. + * @param {function} fn Callback function (listener). + * @param {*} [context] Context for function execution. + * @return {Evemit} Current instance. + * @api public + */ + once(event: string, fn: Function, context?: any): this; + + /** + * Emit an event to all registered event listeners. + * + * @param {string} event Event name. + * @param {*} [...arg] One or more arguments to pass to the listeners. + * @return {bool} Indication, `true` if at least one listener was executed, + * otherwise returns `false`. + * @api public + */ + emit(event: string, ...args: any[]): boolean; + + /** + * Remove event listeners. + * + * @param {string} event The event to remove. + * @param {function} fn The listener that we need to find. + * @return {Evemit} Current instance. + * @api public + */ + off(event: string, fn: Function): this; + + /** + * Get a list of assigned event listeners. + * + * @param {string} [event] The events that should be listed. + * If not provided, all listeners are returned. + * Use the property `Evemit.events` if you want to get an object like + * ``` + * {event1: [array of listeners], event2: [array of listeners], ...} + * ``` + * + * @return {array} + * @api public + */ + listeners(event?: string): Function[]; + } + + export = Evemit; +} \ No newline at end of file diff --git a/package.json b/package.json index d23c192..89d36d5 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,9 @@ { "name": "evemit", - "version": "1.0.2", + "version": "1.0.3", "description": "Minimal and fast JavaScript event emitter for Node.js and front-end (only 1kb minified!).", "main": "./evemit.js", + "types": "./evemit.d.ts", "keywords": [ "event", "emitter", @@ -37,4 +38,4 @@ "webpack": "^1.4.13", "unit.js": "^1.0.2" } -} +} \ No newline at end of file