Skip to content
Myr

Migrate to TypeScript and use Map (#99)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Migrate to TypeScript & Microbundle, move to Map for event handler storage

authored by Jack Franklin · committed by GitHub · parent cfd246c · Browse files · 8 files+152 added−221 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

8 files+152 added−221 removed

  • tests:+35 added−28 removed
  • other:+117 added−193 removed

8 files+152 added−221 removed

deleted:src/index.js-63 removed
added:src/index.ts+75 added
modified:test/index.js+35 added-28 removed
10 unmodified lines
11describe('mitt#', () => {12 let events, inst;1314 beforeEach( () => {15 events = Object.create(null);15 events = new Map();16 inst = mitt(events);17 });1819 describe('on()', () => {
3 unmodified lines
23 .that.is.a('function');24 });2526 it('should register handler for new type', () => {27 let foo = () => {};27 const foo = () => {};28 inst.on('foo', foo);2930 expect(events).to.have.property('foo').that.deep.equals([foo]);30 expect(events.get('foo')).to.deep.equal([foo]);31 });3233 it('should register handlers for any type strings', () => {34 let foo = () => {};34 const foo = () => {};35 inst.on('constructor', foo);3637 expect(events).to.have.property('constructor').that.deep.equals([foo]);37 expect(events.get('constructor')).to.deep.equal([foo]);38 });3940 it('should append handler for existing type', () => {41 let foo = () => {};42 let bar = () => {};41 const foo = () => {};42 const bar = () => {};43 inst.on('foo', foo);44 inst.on('foo', bar);4546 expect(events).to.have.property('foo').that.deep.equals([foo, bar]);46 expect(events.get('foo')).to.deep.equal([foo, bar]);47 });4849 it('should NOT normalize case', () => {50 let foo = () => {};50 const foo = () => {};51 inst.on('FOO', foo);52 inst.on('Bar', foo);53 inst.on('baz:baT!', foo);5455 expect(events).to.have.property('FOO').that.deep.equals([foo]);56 expect(events).to.not.have.property('foo');57 expect(events).to.have.property('Bar').that.deep.equals([foo]);58 expect(events).to.not.have.property('bar');59 expect(events).to.have.property('baz:baT!').that.deep.equals([foo]);55 expect(events.get('FOO')).to.deep.equal([foo]);56 expect(events.has('foo')).to.equal(false);57 expect(events.get('Bar')).to.deep.equal([foo]);58 expect(events.has('bar')).to.equal(false);59 expect(events.get('baz:baT!')).to.deep.equal([foo]);60 });6162 it('can take symbols for event types', () => {63 const foo = () => {};64 const eventType = Symbol('eventType');65 inst.on(eventType, foo);66 expect(events.get(eventType)).to.deep.equal([foo]);67 });68 });6970 describe('off()', () => {71 it('should be a function', () => {
2 unmodified lines
74 .that.is.a('function');75 });7677 it('should remove handler for type', () => {71 let foo = () => {};78 const foo = () => {};79 inst.on('foo', foo);80 inst.off('foo', foo);8175 expect(events).to.have.property('foo').that.is.empty;82 expect(events.get('foo')).to.be.empty;83 });8485 it('should NOT normalize case', () => {79 let foo = () => {};86 const foo = () => {};87 inst.on('FOO', foo);88 inst.on('Bar', foo);89 inst.on('baz:bat!', foo);9091 inst.off('FOO', foo);92 inst.off('Bar', foo);93 inst.off('baz:baT!', foo);9488 expect(events).to.have.property('FOO').that.is.empty;89 expect(events).to.not.have.property('foo');90 expect(events).to.have.property('Bar').that.is.empty;91 expect(events).to.not.have.property('bar');92 expect(events).to.have.property('baz:bat!').with.length(1);95 expect(events.get('FOO')).to.be.empty;96 expect(events.has('foo')).to.equal(false);97 expect(events.get('Bar')).to.be.empty;98 expect(events.has('bar')).to.equal(false);99 expect(events.get('baz:bat!')).to.have.lengthOf(1);100 });101 });102103 describe('emit()', () => {
3 unmodified lines
107 .that.is.a('function');108 });109110 it('should invoke handler for type', () => {104 let event = { a: 'b' };111 const event = { a: 'b' };112113 inst.on('foo', (one, two) => {114 expect(one).to.deep.equal(event);115 expect(two).to.be.an('undefined');
2 unmodified lines
118 inst.emit('foo', event);119 });120121 it('should NOT ignore case', () => {115 let onFoo = spy(),122 const onFoo = spy(),123 onFOO = spy();117 events.Foo = [onFoo];118 events.FOO = [onFOO];124 events.set('Foo', [onFoo]);125 events.set('FOO', [onFOO]);126127 inst.emit('Foo', 'Foo arg');128 inst.emit('FOO', 'FOO arg');129
1 unmodified line
131 expect(onFOO).to.have.been.calledOnce.and.calledWith('FOO arg');132 });133134 it('should invoke * handlers', () => {128 let star = spy(),135 const star = spy(),136 ea = { a: 'a' },137 eb = { b: 'b' };138132 events['*'] = [star];139 events.set('*', [star]);140141 inst.emit('foo', ea);142 expect(star).to.have.been.calledOnce.and.calledWith('foo', ea);143 star.reset();
6 unmodified lines
deleted:mitt.d.ts-53 removed
modified:package.json+25 added-35 removed
modified:README.md+17 added-17 removed
93 unmodified lines
94## API9596<!-- Generated by documentation.js. Update this documentation by updating the source code. -->9798#### Table of Contents99100- [mitt](#mitt)101- [on](#on)102 - [Parameters](#parameters)103- [off](#off)104 - [Parameters](#parameters-1)105- [emit](#emit)106 - [Parameters](#parameters-2)107108### mitt109110Mitt: Tiny (~200b) functional event emitter / pubsub.111102**Parameters**103104- `all` **EventHandlerMap** 105112Returns **Mitt** 113114### on115116Register an event handler for the given type.117112**Parameters**118#### Parameters119114- `type` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Type of event to listen for, or `"*"` for all events120- `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 listen for, or `"*"` for all events121- `handler` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** Function to call in response to given event122123### off124125Remove an event handler for the given type.126121**Parameters**127#### Parameters128123- `type` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Type of event to unregister `handler` from, or `"*"`129- `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 `"*"`130- `handler` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** Handler function to remove131132### emit133134Invoke all handlers for the given type.135If present, `"*"` handlers are invoked after type-matched handlers.136131_Note: Manually firing "*" handlers is not supported._137Note: Manually firing "\*" handlers is not supported.138133**Parameters**139#### Parameters140135- `type` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** The event type to invoke141- `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 invoke142- `evt` **Any?** Any value (object is recommended and powerful), passed to each handler143144## Contribute145146First off, thanks for taking the time to contribute!147Now, take a moment to be sure your contributions make sense to everyone else.148143Development Start:144145This project is typed with Flow Type annotations. To ensure you have the proper typings for this project run146147`flow-typed install`148149### Reporting Issues150151Found a problem? Want a new feature? First of all see if your issue or idea has [already been reported](../../issues).152If don't, just open a [new clear and descriptive issue](../../issues/new).
18 unmodified lines
deleted:rollup.config.js-20 removed
deleted:typings.json-5 removed
1{2 "name": "mitt",3 "main": "mitt.d.ts",4 "version": false5}