Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,30 @@ Mitt: Tiny (~200b) functional event emitter / pubsub.

Returns **Mitt**

#### on
#### emit

Invoke all handlers for the given type.
If present, `"*"` handlers are invoked prior to type-matched handlers.

**Parameters**

- `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The event type to invoke
- `evt` **\[Any]** Any value (object is recommended and powerful), passed to each handler

Returns **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the `mitt` instance for chaining

### on

Register an event handler for the given type.

**Parameters**

- `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Type of event to listen for, or `"*"` for all events
- `handler` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** Function to call in response to the given event
- `handler` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** Function to call in response to given event

#### off
Returns **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the `mitt` instance for chaining

### off

Remove an event handler for the given type.

Expand All @@ -105,15 +119,7 @@ Remove an event handler for the given type.
- `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Type of event to unregister `handler` from, or `"*"`
- `handler` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** Handler function to remove

#### emit

Invoke all handlers for the given type.
If present, `"*"` handlers are invoked prior to type-matched handlers.

**Parameters**

- `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The event type to invoke
- `event` **\[Any]** An event object, passed to each handler
Returns **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the `mitt` instance for chaining

## Contribute
First off, thanks for taking the time to contribute!
Expand All @@ -139,3 +145,4 @@ Pull requests are the greatest contributions, so be sure they are focused in sco
## License

[MIT License](LICENSE.md) © [Jason Miller](https://jasonformat.com/)

20 changes: 20 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict'

var mitt = require('./dist/mitt')
var ee = mitt()

ee
.on('*', (type, arg) => console.log('wildcard:', type, arg))
.on('foo', (one, c) => console.log('foo1:', one)) // => 1
.on('foo', (one) => console.log('foo2:', one)) // => 1
.on('bar', (arg) => console.log('bar:', arg)) // => 444
.emit('foo', 1, 2, 3) // we not support multiple arguments
.emit('bar', 444)

console.log(ee.all)

var emitter = mitt()

console.log(emitter.all) // => {}

emitter.emit('foo', 777)
70 changes: 38 additions & 32 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,53 @@
/** Mitt: Tiny (~200b) functional event emitter / pubsub.
* @name mitt
* @returns {Mitt}
* @name mitt
* @returns {Mitt}
*/
export default function mitt(all) {
// Arrays of event handlers, keyed by type
all = all || {};
export default function mitt () {
let all = Object.create(null)
let ret = {
all,

// Get or create a named handler list
function list(type) {
let t = type.toLowerCase();
return all[t] || (all[t] = []);
}

return {

/** Register an event handler for the given type.
* @param {String} type Type of event to listen for, or `"*"` for all events
* @param {Function} handler Function to call in response to the given event
* @memberof mitt
/**
* Register an event handler for the given type.
*
* @param {String} type Type of event to listen for, or `"*"` for all events
* @param {Function} handler Function to call in response to given event
* @return {Object} the `mitt` instance for chaining
* @memberOf mitt
*/
on(type, handler) {
list(type).push(handler);
(all[type] || (all[type] = [])).push(handler);
return ret;
},

/** Remove an event handler for the given type.
* @param {String} type Type of event to unregister `handler` from, or `"*"`
* @param {Function} handler Handler function to remove
* @memberof mitt
/**
* Remove an event handler for the given type.
*
* @param {String} type Type of event to unregister `handler` from, or `"*"`
* @param {Function} handler Handler function to remove
* @return {Object} the `mitt` instance for chaining
* @memberOf mitt
*/
off(type, handler) {
let e = list(type),
i = e.indexOf(handler);
if (~i) e.splice(i, 1);
let e = all[type] || (all[type] = []);
e.splice(e.indexOf(handler) >>> 0, 1);
return ret;
},

/** Invoke all handlers for the given type.
* If present, `"*"` handlers are invoked prior to type-matched handlers.
* @param {String} type The event type to invoke
* @param {Any} [event] An event object, passed to each handler
* @memberof mitt
/**
* Invoke all handlers for the given type.
* If present, `"*"` handlers are invoked prior to type-matched handlers.
*
* @param {String} type The event type to invoke
* @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler
* @return {Object} the `mitt` instance for chaining
* @memberof mitt
*/
emit(type, event) {
list('*').concat(list(type)).forEach( f => { f(event); });
emit(type, evt) {
(all[type] || []).map((handler) => { handler(evt); });
(all['*'] || []).map((handler) => { handler(type, evt); });
return ret;
}
};
return ret;
}
Loading