Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
tunnckoCore committed Jan 17, 2017
1 parent 42fb45e commit a6190e9
Show file tree
Hide file tree
Showing 4 changed files with 4,302 additions and 185 deletions.
45 changes: 25 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,18 @@
<a href="https://www.npmjs.org/package/mitt"><img src="https://img.shields.io/npm/v/mitt.svg?style=flat" alt="npm"></a> <a href="https://travis-ci.org/developit/mitt"><img src="https://travis-ci.org/developit/mitt.svg?branch=master" alt="travis"></a>
</p>


## Why Mitt?

- **Microscopic:** weighs less than 200 bytes gzipped
- **Useful:** a wildcard `"*"` event type listens to all events
- **Familiar:** same names & ideas as [Node's EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter)
- **Functional:** methods don't rely on `this`
- **Great Name:** somehow [mitt](https://npm.im/mitt) wasn't taken
- **Microscopic:** weighs less than 200 bytes gzipped
- **Useful:** a wildcard `"*"` event type listens to all events
- **Familiar:** same names & ideas as [Node's EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter)
- **Functional:** methods don't rely on `this`
- **Great Name:** somehow [mitt](https://npm.im/mitt) wasn't taken

> Mitt was made for the browser, but works in any JavaScript runtime. It has no dependencies and only uses basic language features. It probably works in Internet Explorer 5.

* * *


## Usage

After installing via `npm install --save mitt`:
Expand Down Expand Up @@ -55,16 +52,32 @@ 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
- `arg1` **\[Any]** A value (first argument), passed to each handler
- `arg2` **\[Any]** A value (second argument), passed to each handler
- `arg3` **\[Any]** A value (third argument), 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

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

#### off
### off

Remove an event handler for the given type.

Expand All @@ -73,12 +86,4 @@ 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
90 changes: 51 additions & 39 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,59 @@
/** 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 ret = {
all: Object.create(null),

// Get or create a named handler list
function list(type) {
let t = type.toLowerCase();
return all[t] || (all[t] = []);
}
/**
* 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).add(handler);
return ret;
},

return {
/**
* 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) {
list(type).delete(handler);
return ret;
},

/** 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
*/
on(type, handler) {
list(type).push(handler);
},
/**
* 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} [arg1] A value (first argument), passed to each handler
* @param {Any} [arg2] A value (second argument), passed to each handler
* @param {Any} [arg3] A value (third argument), passed to each handler
* @return {Object} the `mitt` instance for chaining
* @memberof mitt
*/
emit(type, arg1, arg2, arg3) {
list(type).forEach((handler) => handler(arg1, arg2, arg3));
list('*').forEach((handler) => handler(type, arg1, arg2, arg3));
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
*/
off(type, handler) {
let e = list(type),
i = e.indexOf(handler);
if (~i) e.splice(i, 1);
},
// Get or create a named handler list
let list = (type) => {
return ret.all[type = type.toLowerCase()] || (ret.all[type] = new Set());
};

/** 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
*/
emit(type, event) {
list('*').concat(list(type)).forEach( f => { f(event); });
}
};
return ret;
}
Loading

0 comments on commit a6190e9

Please sign in to comment.