Skip to content

Commit

Permalink
Add Typescript types
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolab committed Dec 25, 2024
1 parent 1cf110d commit f6d7ec3
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 13 deletions.
28 changes: 17 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br>
Minimal and fast JavaScript / TypeScript event emitter for Node.js and front-end.<br>
Only 1kb minified (577 bytes gzipped).


## Getting started

### Install

Via `NPM`

```shell
npm install evemit --save
```
Expand All @@ -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');
Expand All @@ -33,6 +34,7 @@ console.log(typeof Evemit);
```

Basic usage (without CommonJS)

```js
// true
console.log(typeof Evemit === 'function');
Expand All @@ -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!
Expand All @@ -58,7 +60,6 @@ obj.on('say-hello', function(hello) {
obj.emit('say-hello', 'Hello World!');
```


## API

See the complete [API doc](API.md).
Expand Down Expand Up @@ -107,6 +108,7 @@ obj.emit('ping');
```

With arguments passed to the listeners

```js
obj.emit('ping', 'arg1', 'arg2', {an: 'object'});
```
Expand All @@ -130,31 +132,36 @@ 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;
```

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();
Expand All @@ -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

Expand All @@ -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) |
Expand Down
84 changes: 84 additions & 0 deletions evemit.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* @name Evemit
* @description Minimal and fast JavaScript event emitter for Node.js and front-end.
* @author Nicolas Tallefourtane <[email protected]>
* @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<string, Function[]>;

/**
* 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;
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -37,4 +38,4 @@
"webpack": "^1.4.13",
"unit.js": "^1.0.2"
}
}
}

0 comments on commit f6d7ec3

Please sign in to comment.