Skip to content

Commit

Permalink
Merge branch 'expose-all' of https://github.com/jaylinski/mitt
Browse files Browse the repository at this point in the history
  • Loading branch information
developit committed Jul 15, 2020
2 parents 5116df4 + 76a353c commit 0f41ead
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
8 changes: 8 additions & 0 deletions README.md
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,6 +102,7 @@ const emitter: mitt.Emitter = mitt();
#### Table of Contents

- [mitt](#mitt)
- [all](#all)
- [on](#on)
- [Parameters](#parameters)
- [off](#off)
Expand All @@ -112,6 +116,10 @@ Mitt: Tiny (~200b) functional event emitter / pubsub.

Returns **Mitt**

### all

A Map of event names to registered handler functions.

### on

Register an event handler for the given type.
Expand Down
15 changes: 11 additions & 4 deletions src/index.ts
Expand Up @@ -13,6 +13,8 @@ export type WildCardEventHandlerList = Array<WildcardHandler>;
export type EventHandlerMap = Map<EventType, EventHandlerList | WildCardEventHandlerList>;

export interface Emitter {
all: EventHandlerMap;

on<T = any>(type: EventType, handler: Handler<T>): void;
on(type: '*', handler: WildcardHandler): void;

Expand All @@ -23,15 +25,21 @@ export interface Emitter {
emit(type: '*', event?: any): void;
}

/** Mitt: Tiny (~200b) functional event emitter / pubsub.
* @name mitt
* @returns {Mitt}
/**
* Mitt: Tiny (~200b) functional event emitter / pubsub.
* @name mitt
* @returns {Mitt}
*/
export default function mitt(all?: EventHandlerMap): Emitter {
all = all || new Map();

return {

/**
* A Map of event names to registered handler functions.
*/
all,

/**
* Register an event handler for the given type.
* @param {string|symbol} type Type of event to listen for, or `"*"` for all events
Expand All @@ -48,7 +56,6 @@ export default function mitt(all?: EventHandlerMap): Emitter {

/**
* Remove an event handler for the given type.
*
* @param {string|symbol} type Type of event to unregister `handler` from, or `"*"`
* @param {Function} handler Handler function to remove
* @memberOf mitt
Expand Down
8 changes: 8 additions & 0 deletions test/index_test.ts
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

0 comments on commit 0f41ead

Please sign in to comment.