Expose `all` property
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Relates to #70 #72 #102
authored by Jakob Linskeseder · committed by Jakob Linskeseder · parent 5959175 · Browse files · 3 files·+41 added−15 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+41 added−15 removed
- tests:+8 added−0 removed
- other:+33 added−15 removed
Provenance
- Commit
2c85f09- Tree
ebd2725- Author
Jakob Linskeseder <jakob.linskeseder@gmail.com>- Committer
Jakob Linskeseder <jakob.linskeseder@gmail.com>- Parent
5959175
3 files changed+41 added−15 removed
modified:src/index.ts+7 added
11 unmodified lines121314151617181920219 unmodified lines31323334353637383940414243444540 unmodified lines11 unmodified lines// A map of event types and their corresponding event handlers.export type EventHandlerMap = Map<EventType, EventHandlerList | WildCardEventHandlerList>;
export interface Emitter { all: EventHandlerMap;
on(type: EventType, handler: Handler): void; on(type: '*', handler: WildcardHandler): void;
off(type: EventType, handler: Handler): void;9 unmodified lines */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 * @param {Function} handler Function to call in response to given event40 unmodified linesmodified:test/index.js+8 added
28 unmodified lines29 events = new Map();30 inst = mitt(events);31 });32 33+ describe('properties', () => {34+ it('should expose the event handler map', () => {35+ expect(inst)36+ .to.have.property('all')37+ .that.is.a('map');38+ });39+ });40+41 describe('on()', () => {42 it('should be a function', () => {43 expect(inst)44 .to.have.property('on')146 unmodified linesmodified:README.md+26 added-15 removed
67 unmodified lines68 69 // fire an event70 emitter.emit('foo', { a: 'b' })71 72+// clearing all events73+emitter.all.clear()74+75 // working with handler references:76 function onFoo() {}77 emitter.on('foo', onFoo) // listen78 emitter.off('foo', onFoo) // unlisten22 unmodified lines101 102 #### Table of Contents103 104 - [mitt](#mitt)102-- [on](#on)105+- [emit](#emit)106+ - [Properties](#properties)107+- [emit](#emit-1)108 - [Parameters](#parameters)104-- [off](#off)109+- [on](#on)110 - [Parameters](#parameters-1)106-- [emit](#emit)111+- [off](#off)112 - [Parameters](#parameters-2)113 114 ### mitt115 116 Mitt: Tiny (~200b) functional event emitter / pubsub.117 118 Returns **Mitt** 119 120+### emit121+122+#### Properties123+124+- `all` **EventHandlerMap** Contains all registered event handlers.125+126+### emit127+128+Invoke all handlers for the given type.129+If present, `"*"` handlers are invoked after type-matched handlers.130+131+Note: Manually firing "\*" handlers is not supported.132+133+#### Parameters134+135+- `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 invoke136+- `evt` **Any?** Any value (object is recommended and powerful), passed to each handler137+138 ### on139 140 Register an event handler for the given type.141 10 unmodified lines152 153 - `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 `"*"`154 - `handler` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** Handler function to remove155 133-### emit134-135-Invoke all handlers for the given type.136-If present, `"*"` handlers are invoked after type-matched handlers.137-138-Note: Manually firing "\*" handlers is not supported.139-140-#### Parameters141-142-- `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 invoke143-- `evt` **Any?** Any value (object is recommended and powerful), passed to each handler144-156 ## Contribute157 158 First off, thanks for taking the time to contribute!159 Now, take a moment to be sure your contributions make sense to everyone else.23 unmodified lines