From 2c85f0900f5fd1d12844d0f61cafa27a739d4346 Mon Sep 17 00:00:00 2001 From: Jakob Linskeseder Date: Wed, 3 Jun 2020 21:41:20 +0200 Subject: [PATCH] Expose `all` property Relates to #70 #72 #102 --- README.md | 41 ++++++++++++++++++++++++++--------------- src/index.ts | 7 +++++++ test/index.js | 8 ++++++++ 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index b6a8022..3a5d4a2 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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. @@ -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! diff --git a/src/index.ts b/src/index.ts index cfb1cf3..d68188d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,6 +13,8 @@ export type WildCardEventHandlerList = Array; export type EventHandlerMap = Map; export interface Emitter { + all: EventHandlerMap; + on(type: EventType, handler: Handler): void; on(type: '*', handler: WildcardHandler): void; @@ -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 diff --git a/test/index.js b/test/index.js index a837d36..473e92e 100644 --- a/test/index.js +++ b/test/index.js @@ -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)