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
41 changes: 26 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ emitter.on('*', (type, e) => console.log(type, e) )
// fire an event
emitter.emit('foo', { a: 'b' })

// clearing all events
emitter.all.clear()

// working with handler references:
function onFoo() {}
emitter.on('foo', onFoo) // listen
Expand Down Expand Up @@ -99,11 +102,13 @@ const emitter: mitt.Emitter = mitt();
#### Table of Contents

- [mitt](#mitt)
- [on](#on)
- [emit](#emit)
- [Properties](#properties)
- [emit](#emit-1)
- [Parameters](#parameters)
- [off](#off)
- [on](#on)
- [Parameters](#parameters-1)
- [emit](#emit)
- [off](#off)
- [Parameters](#parameters-2)

### mitt
Expand All @@ -112,6 +117,24 @@ Mitt: Tiny (~200b) functional event emitter / pubsub.

Returns **Mitt**

### emit

#### Properties

- `all` **EventHandlerMap** Contains all registered event handlers.

### emit

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

Note: Manually firing "\*" handlers is not supported.

#### Parameters

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

### on

Register an event handler for the given type.
Expand All @@ -130,18 +153,6 @@ Remove an event handler for the given type.
- `type` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))** Type of event to unregister `handler` from, or `"*"`
- `handler` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** Handler function to remove

### emit

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

Note: Manually firing "\*" handlers is not supported.

#### Parameters

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

## Contribute

First off, thanks for taking the time to contribute!
Expand Down
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export type WildCardEventHandlerList = Array<WildcardHandler>;
export type EventHandlerMap = Map<EventType, EventHandlerList | WildCardEventHandlerList>;

export interface Emitter {
all: EventHandlerMap;

on(type: EventType, handler: Handler): void;
on(type: '*', handler: WildcardHandler): void;

Expand All @@ -30,8 +32,13 @@ export interface Emitter {
export default function mitt(all?: EventHandlerMap): Emitter {
all = all || new Map();

/**
* @property {EventHandlerMap} all Contains all registered event handlers.
*/
return {

all,

/**
* Register an event handler for the given type.
* @param {string|symbol} type Type of event to listen for, or `"*"` for all events
Expand Down
8 changes: 8 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ describe('mitt#', () => {
inst = mitt(events);
});

describe('properties', () => {
it('should expose the event handler map', () => {
expect(inst)
.to.have.property('all')
.that.is.a('map');
});
});

describe('on()', () => {
it('should be a function', () => {
expect(inst)
Expand Down