diff options
| -rw-r--r-- | README.md | 1 | ||||
| -rw-r--r-- | dist-using-set/mitt.js | 2 | ||||
| -rw-r--r-- | dist-using-set/mitt.js.map | 1 | ||||
| -rw-r--r-- | dist-using-set/mitt.umd.js | 2 | ||||
| -rw-r--r-- | dist-using-set/mitt.umd.js.map | 1 | ||||
| -rw-r--r-- | src/index.js | 21 | ||||
| -rw-r--r-- | test/index.js | 54 |
7 files changed, 45 insertions, 37 deletions
@@ -62,7 +62,6 @@ If present, `"*"` handlers are invoked prior to type-matched handlers. - `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The event type to invoke - `arg1` **\[Any]** A value (first argument), passed to each handler - `arg2` **\[Any]** A value (second argument), passed to each handler -- `arg3` **\[Any]** A value (third argument), passed to each handler Returns **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the `mitt` instance for chaining diff --git a/dist-using-set/mitt.js b/dist-using-set/mitt.js new file mode 100644 index 0000000..441f142 --- /dev/null +++ b/dist-using-set/mitt.js @@ -0,0 +1,2 @@ +function n(){var n={all:Object.create(null),on:function(e,r){return t(e).add(r),n},off:function(e,r){return t(e).delete(r),n},emit:function(e,r,u,o){return t(e).forEach(function(n){return n(r,u,o)}),t("*").forEach(function(n){return n(e,r,u,o)}),n}},t=function(t){return n.all[t=t.toLowerCase()]||(n.all[t]=new Set)};return n}module.exports=n; +//# sourceMappingURL=mitt.js.map
\ No newline at end of file diff --git a/dist-using-set/mitt.js.map b/dist-using-set/mitt.js.map new file mode 100644 index 0000000..db6f0e1 --- /dev/null +++ b/dist-using-set/mitt.js.map @@ -0,0 +1 @@ +{"version":3,"sources":["../src/index.js"],"names":["mitt","let","ret","all","Object","create","on","type","handler","list","add","off","delete","emit","arg1","arg2","arg3","forEach","toLowerCase","Set"],"mappings":"AAIA,QAAwBA,KACvBC,GAAIC,IACHC,IAAKC,OAAOC,OAAO,MAUnBC,GAAE,SAACC,EAAMC,GAER,MADAC,GAAKF,GAAMG,IAAIF,GACRN,GAWRS,IAAG,SAACJ,EAAMC,GAET,MADAC,GAAKF,GAAMK,OAAOJ,GACXN,GAcRW,KAAI,SAACN,EAAMO,EAAMC,EAAMC,GAGtB,MAFAP,GAAKF,GAAMU,QAAQ,SAACT,GAAS,MAAGA,GAAQM,EAAMC,EAAMC,KACpDP,EAAK,KAAKQ,QAAQ,SAACT,GAAS,MAAGA,GAAQD,EAAMO,EAAMC,EAAMC,KAClDd,IAKLO,EAAO,SAACF,GACX,MAAOL,GAAIC,IAAII,EAAOA,EAAKW,iBAAmBhB,EAAIC,IAAII,GAAQ,GAAIY,MAGnE,OAAOjB","file":"mitt.js","sourcesContent":["/** Mitt: Tiny (~200b) functional event emitter / pubsub.\n * @name mitt\n * @returns {Mitt}\n */\nexport default function mitt () {\n\tlet ret = {\n\t\tall: Object.create(null),\n\n\t\t/**\n\t\t * Register an event handler for the given type.\n\t\t *\n\t\t * @param {String} type Type of event to listen for, or `\"*\"` for all events\n\t\t * @param {Function} handler Function to call in response to given event\n\t\t * @return {Object} the `mitt` instance for chaining\n\t\t * @memberOf mitt\n\t\t */\n\t\ton(type, handler) {\n\t\t\tlist(type).add(handler);\n\t\t\treturn ret;\n\t\t},\n\n\t\t/**\n\t\t * Remove an event handler for the given type.\n\t\t *\n\t\t * @param {String} type Type of event to unregister `handler` from, or `\"*\"`\n\t\t * @param {Function} handler Handler function to remove\n\t\t * @return {Object} the `mitt` instance for chaining\n\t\t * @memberOf mitt\n\t\t */\n\t\toff(type, handler) {\n\t\t\tlist(type).delete(handler);\n\t\t\treturn ret;\n\t\t},\n\n\t\t/**\n\t\t * Invoke all handlers for the given type.\n\t\t * If present, `\"*\"` handlers are invoked prior to type-matched handlers.\n\t\t *\n\t\t * @param {String} type The event type to invoke\n\t\t * @param {Any} [arg1] A value (first argument), passed to each handler\n\t\t * @param {Any} [arg2] A value (second argument), passed to each handler\n\t\t * @param {Any} [arg3] A value (third argument), passed to each handler\n\t\t * @return {Object} the `mitt` instance for chaining\n\t\t * @memberof mitt\n\t\t */\n\t\temit(type, arg1, arg2, arg3) {\n\t\t\tlist(type).forEach((handler) => handler(arg1, arg2, arg3));\n\t\t\tlist('*').forEach((handler) => handler(type, arg1, arg2, arg3));\n\t\t\treturn ret;\n\t\t}\n\t};\n\n\t// Get or create a named handler list\n\tlet list = (type) => {\n\t\treturn ret.all[type = type.toLowerCase()] || (ret.all[type] = new Set());\n\t};\n\n\treturn ret;\n}\n"]}
\ No newline at end of file diff --git a/dist-using-set/mitt.umd.js b/dist-using-set/mitt.umd.js new file mode 100644 index 0000000..9ca0e6b --- /dev/null +++ b/dist-using-set/mitt.umd.js @@ -0,0 +1,2 @@ +!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):n.mitt=e()}(this,function(){function n(){var n={all:Object.create(null),on:function(t,o){return e(t).add(o),n},off:function(t,o){return e(t).delete(o),n},emit:function(t,o,r,u){return e(t).forEach(function(n){return n(o,r,u)}),e("*").forEach(function(n){return n(t,o,r,u)}),n}},e=function(e){return n.all[e=e.toLowerCase()]||(n.all[e]=new Set)};return n}return n}); +//# sourceMappingURL=mitt.umd.js.map
\ No newline at end of file diff --git a/dist-using-set/mitt.umd.js.map b/dist-using-set/mitt.umd.js.map new file mode 100644 index 0000000..6ddf7b4 --- /dev/null +++ b/dist-using-set/mitt.umd.js.map @@ -0,0 +1 @@ +{"version":3,"sources":["../src/index.js"],"names":["mitt","let","ret","all","Object","create","on","type","handler","list","add","off","delete","emit","arg1","arg2","arg3","forEach","toLowerCase","Set"],"mappings":"mKAIA,QAAwBA,KACvBC,GAAIC,IACHC,IAAKC,OAAOC,OAAO,MAUnBC,GAAE,SAACC,EAAMC,GAER,MADAC,GAAKF,GAAMG,IAAIF,GACRN,GAWRS,IAAG,SAACJ,EAAMC,GAET,MADAC,GAAKF,GAAMK,OAAOJ,GACXN,GAcRW,KAAI,SAACN,EAAMO,EAAMC,EAAMC,GAGtB,MAFAP,GAAKF,GAAMU,QAAQ,SAACT,GAAS,MAAGA,GAAQM,EAAMC,EAAMC,KACpDP,EAAK,KAAKQ,QAAQ,SAACT,GAAS,MAAGA,GAAQD,EAAMO,EAAMC,EAAMC,KAClDd,IAKLO,EAAO,SAACF,GACX,MAAOL,GAAIC,IAAII,EAAOA,EAAKW,iBAAmBhB,EAAIC,IAAII,GAAQ,GAAIY,MAGnE,OAAOjB","file":"mitt.umd.js","sourcesContent":["/** Mitt: Tiny (~200b) functional event emitter / pubsub.\n * @name mitt\n * @returns {Mitt}\n */\nexport default function mitt () {\n\tlet ret = {\n\t\tall: Object.create(null),\n\n\t\t/**\n\t\t * Register an event handler for the given type.\n\t\t *\n\t\t * @param {String} type Type of event to listen for, or `\"*\"` for all events\n\t\t * @param {Function} handler Function to call in response to given event\n\t\t * @return {Object} the `mitt` instance for chaining\n\t\t * @memberOf mitt\n\t\t */\n\t\ton(type, handler) {\n\t\t\tlist(type).add(handler);\n\t\t\treturn ret;\n\t\t},\n\n\t\t/**\n\t\t * Remove an event handler for the given type.\n\t\t *\n\t\t * @param {String} type Type of event to unregister `handler` from, or `\"*\"`\n\t\t * @param {Function} handler Handler function to remove\n\t\t * @return {Object} the `mitt` instance for chaining\n\t\t * @memberOf mitt\n\t\t */\n\t\toff(type, handler) {\n\t\t\tlist(type).delete(handler);\n\t\t\treturn ret;\n\t\t},\n\n\t\t/**\n\t\t * Invoke all handlers for the given type.\n\t\t * If present, `\"*\"` handlers are invoked prior to type-matched handlers.\n\t\t *\n\t\t * @param {String} type The event type to invoke\n\t\t * @param {Any} [arg1] A value (first argument), passed to each handler\n\t\t * @param {Any} [arg2] A value (second argument), passed to each handler\n\t\t * @param {Any} [arg3] A value (third argument), passed to each handler\n\t\t * @return {Object} the `mitt` instance for chaining\n\t\t * @memberof mitt\n\t\t */\n\t\temit(type, arg1, arg2, arg3) {\n\t\t\tlist(type).forEach((handler) => handler(arg1, arg2, arg3));\n\t\t\tlist('*').forEach((handler) => handler(type, arg1, arg2, arg3));\n\t\t\treturn ret;\n\t\t}\n\t};\n\n\t// Get or create a named handler list\n\tlet list = (type) => {\n\t\treturn ret.all[type = type.toLowerCase()] || (ret.all[type] = new Set());\n\t};\n\n\treturn ret;\n}\n"]}
\ No newline at end of file diff --git a/src/index.js b/src/index.js index 5e11237..a67efb2 100644 --- a/src/index.js +++ b/src/index.js @@ -3,8 +3,9 @@ * @returns {Mitt} */ export default function mitt () { + let all = Object.create(null) let ret = { - all: Object.create(null), + all, /** * Register an event handler for the given type. @@ -15,7 +16,7 @@ export default function mitt () { * @memberOf mitt */ on(type, handler) { - list(type).add(handler); + (all[type] || (all[type] = [])).push(handler); return ret; }, @@ -28,7 +29,8 @@ export default function mitt () { * @memberOf mitt */ off(type, handler) { - list(type).delete(handler); + let e = all[type] || (all[type] = []); + e.splice(e.indexOf(handler) >>> 0, 1); return ret; }, @@ -39,21 +41,14 @@ export default function mitt () { * @param {String} type The event type to invoke * @param {Any} [arg1] A value (first argument), passed to each handler * @param {Any} [arg2] A value (second argument), passed to each handler - * @param {Any} [arg3] A value (third argument), passed to each handler * @return {Object} the `mitt` instance for chaining * @memberof mitt */ - emit(type, arg1, arg2, arg3) { - list(type).forEach((handler) => handler(arg1, arg2, arg3)); - list('*').forEach((handler) => handler(type, arg1, arg2, arg3)); + emit(type, arg1, arg2) { + (all[type] || []).map((handler) => { handler(arg1, arg2); }); + (all['*'] || []).map((handler) => { handler(type, arg1, arg2); }); return ret; } }; - - // Get or create a named handler list - let list = (type) => { - return ret.all[type = type.toLowerCase()] || (ret.all[type] = new Set()); - }; - return ret; } diff --git a/test/index.js b/test/index.js index d0bcefd..f552392 100644 --- a/test/index.js +++ b/test/index.js @@ -27,14 +27,14 @@ describe('mitt#', () => { let foo = () => {}; inst.on('foo', foo); - expect(events).to.have.property('foo').that.deep.equals(new Set([foo])); + expect(events).to.have.property('foo').that.deep.equals([foo]); }); it('should register handlers for any type strings', () => { let foo = () => {}; inst.on('constructor', foo); - expect(events).to.have.property('constructor').that.deep.equals(new Set([foo])); + expect(events).to.have.property('constructor').that.deep.equals([foo]); }); it('should append handler for existing type', () => { @@ -43,18 +43,20 @@ describe('mitt#', () => { inst.on('foo', foo); inst.on('foo', bar); - expect(events).to.have.property('foo').that.deep.equals(new Set([foo, foo, bar])); + expect(events).to.have.property('foo').that.deep.equals([foo, bar]); }); - it('should normalize case', () => { + it('should NOT normalize case', () => { let foo = () => {}; inst.on('FOO', foo); inst.on('Bar', foo); inst.on('baz:baT!', foo); - expect(events).to.have.property('foo').that.deep.equals(new Set([foo])); - expect(events).to.have.property('bar').that.deep.equals(new Set([foo])); - expect(events).to.have.property('baz:bat!').that.deep.equals(new Set([foo])); + expect(events).to.have.property('FOO').that.deep.equals([foo]); + expect(events).to.not.have.property('foo'); + expect(events).to.have.property('Bar').that.deep.equals([foo]); + expect(events).to.not.have.property('bar'); + expect(events).to.have.property('baz:baT!').that.deep.equals([foo]); }); }); @@ -75,17 +77,19 @@ describe('mitt#', () => { it('should normalize case', () => { let foo = () => {}; - inst.on('foo', foo); - inst.on('bar', foo); + inst.on('FOO', foo); + inst.on('Bar', foo); inst.on('baz:bat!', foo); inst.off('FOO', foo); inst.off('Bar', foo); inst.off('baz:baT!', foo); - expect(events).to.have.property('foo').that.is.empty; - expect(events).to.have.property('bar').that.is.empty; - expect(events).to.have.property('baz:bat!').that.is.empty; + expect(events).to.have.property('FOO').that.is.empty; + expect(events).to.not.have.property('foo'); + expect(events).to.have.property('Bar').that.is.empty; + expect(events).to.not.have.property('bar'); + expect(events).to.have.property('baz:baT!').that.is.empty; }); }); @@ -107,27 +111,31 @@ describe('mitt#', () => { inst.emit('foo', event); }); - it('should invoke handler with multiple arguments', () => { - inst.on('foo', (aaa, bbb, ccc) => { + it('should invoke handler with multiple (max 2) arguments', () => { + inst.on('foo', (aaa, bbb) => { expect(aaa).to.be.equal(111); expect(bbb).to.be.equal(222); - expect(ccc).to.be.equal(333); }); - inst.emit('foo', 111, 222, 333); + inst.emit('foo', 111, 222); }); - it('should ignore case', () => { + it('should NOT ignore case', () => { let foo = spy(), + bar = spy(), num = 123; - events.foo = [foo]; + events.Foo = [foo]; + events.FOO = [bar]; inst.emit('FOO', num); inst.emit('Foo', num); - let args = foo.args[0]; + let args1 = foo.args[0]; + let args2 = bar.args[0]; - expect(foo).to.have.been.calledTwice; - expect(args).to.be.deep.equal([num, undefined, undefined]); + expect(foo).to.have.been.calledOnce; + expect(bar).to.have.been.calledOnce; + expect(args1).to.be.deep.equal([num, undefined]); + expect(args2).to.be.deep.equal([num, undefined]); }); it('should invoke * handlers', () => { @@ -142,8 +150,8 @@ describe('mitt#', () => { args2 = star.args[1]; expect(star).to.have.been.calledTwice; - expect(args1).to.deep.equal(['foo', aa, undefined, undefined]); - expect(args2).to.deep.equal(['bar', aa, undefined, undefined]); + expect(args1).to.deep.equal(['foo', aa, undefined]); + expect(args2).to.deep.equal(['bar', aa, undefined]); }); }); }); |
