diff --git a/dist/dush.common.js b/dist/dush.common.js index 3ed17bc..32bb5a3 100644 --- a/dist/dush.common.js +++ b/dist/dush.common.js @@ -124,7 +124,7 @@ function dush () { */ on: function on (name, handler, once) { - var e = all[name] || (all[name] = []); + var e = app.all[name] || (app.all[name] = []); function func () { if (!func.called) { @@ -217,8 +217,10 @@ function dush () { if (handler && all[name]) { var fnStr = handler.toString(); all[name] = all[name].filter(function (func) { return func.sourceString !== fnStr; }); - } else { + } else if (name) { all[name] = []; + } else { + app.all = Object.create(null); } return app diff --git a/dist/dush.es.js b/dist/dush.es.js index 1284f75..a46bb25 100644 --- a/dist/dush.es.js +++ b/dist/dush.es.js @@ -122,7 +122,7 @@ function dush () { */ on: function on (name, handler, once) { - var e = all[name] || (all[name] = []); + var e = app.all[name] || (app.all[name] = []); function func () { if (!func.called) { @@ -215,8 +215,10 @@ function dush () { if (handler && all[name]) { var fnStr = handler.toString(); all[name] = all[name].filter(function (func) { return func.sourceString !== fnStr; }); - } else { + } else if (name) { all[name] = []; + } else { + app.all = Object.create(null); } return app diff --git a/dist/dush.umd.js b/dist/dush.umd.js index 8aa3b9b..154b14d 100644 --- a/dist/dush.umd.js +++ b/dist/dush.umd.js @@ -1,2 +1,2 @@ -!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):n.dush=e()}(this,function(){function n(){var n=Object.create(null),e={all:n,use:function(n){return n(e)||e},on:function(t,o,r){function u(){u.called||(e.off(t,u),o.apply(o,arguments),u.called=!0)}var i=n[t]||(n[t]=[]),f=r?u:o;return f.sourceString=o.toString(),i.push(f),e},once:function(n,t){return e.on(n,t,!0),e},off:function(t,o){if(o&&n[t]){var r=o.toString();n[t]=n[t].filter(function(n){return n.sourceString!==r})}else n[t]=[];return e},emit:function(t){if("*"!==t){var o=[].slice.call(arguments);(n[t]||[]).map(function(n){n.apply(n,o.slice(1))}),(n["*"]||[]).map(function(n){n.apply(n,o)})}return e}};return e}return n}); +!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):n.dush=e()}(this,function(){function n(){var n=Object.create(null),e={all:n,use:function(n){return n(e)||e},on:function(n,t,r){function u(){u.called||(e.off(n,u),t.apply(t,arguments),u.called=!0)}var o=e.all[n]||(e.all[n]=[]),i=r?u:t;return i.sourceString=t.toString(),o.push(i),e},once:function(n,t){return e.on(n,t,!0),e},off:function(t,r){if(r&&n[t]){var u=r.toString();n[t]=n[t].filter(function(n){return n.sourceString!==u})}else t?n[t]=[]:e.all=Object.create(null);return e},emit:function(t){if("*"!==t){var r=[].slice.call(arguments);(n[t]||[]).map(function(n){n.apply(n,r.slice(1))}),(n["*"]||[]).map(function(n){n.apply(n,r)})}return e}};return e}return n}); //# sourceMappingURL=dush.umd.js.map diff --git a/dist/dush.umd.js.gz b/dist/dush.umd.js.gz index 0841552..0900f45 100644 Binary files a/dist/dush.umd.js.gz and b/dist/dush.umd.js.gz differ diff --git a/dist/dush.umd.js.map b/dist/dush.umd.js.map index a68cdb3..c1d8052 100644 --- a/dist/dush.umd.js.map +++ b/dist/dush.umd.js.map @@ -1 +1 @@ -{"version":3,"file":"dush.umd.js","sources":["../src/index.js"],"sourcesContent":["/*!\n * dush \n *\n * Copyright (c) Charlike Mike Reagent <@tunnckoCore> (https://i.am.charlike.online)\n * Released under the MIT license.\n */\n\n'use strict'\n\n/**\n * > A constructor function that returns an object\n * with a few methods.\n *\n * See [JSBin Example](http://jsbin.com/mepemeluhi/edit?js,console).\n *\n * **Example**\n *\n * ```js\n * const dush = require('dush')\n * const emitter = dush()\n *\n * console.log(emitter.all) // => {}\n * console.log(emitter.on) // => Function\n * console.log(emitter.once) // => Function\n * console.log(emitter.off) // => Function\n * console.log(emitter.emit) // => Function\n * ```\n *\n * @name dush()\n * @return {Object} methods\n * @api public\n */\n\nexport default function dush () {\n let all = Object.create(null)\n const app = {\n /**\n * > An listeners map of all registered events\n * and their listeners. A key/value store, where 1) value\n * is an array of event listeners for the key and 2) key\n * is the name of the event.\n *\n * See [JSBin Example](http://jsbin.com/zuwayalovi/edit?js,console).\n *\n * **Example**\n *\n * ```js\n * const emitter = dush()\n *\n * emitter.on('foo', () => {})\n * emitter.on('foo', () => {})\n * emitter.on('bar', () => {})\n *\n * console.log(emitter.all)\n * // => { foo: [Function, Function], bar: [Functon] }\n * ```\n *\n * @name .all\n * @type {Object} `all` a key/value store of all events and their listeners\n * @api public\n */\n\n all,\n\n /**\n * > Invokes `plugin` function immediately, which is passed\n * with `app` instance. You can use it for adding more methods\n * or properties to the instance. Useful if you want to make\n * dush to work with DOM for example.\n *\n * ```js\n * const app = dush()\n *\n * app.on('hi', (str) => {\n * console.log(str) // => 'Hello World!!'\n * })\n *\n * app.use((app) => {\n * app.foo = 'bar'\n * app.hello = (place) => app.emit('hi', `Hello ${place}!!`)\n * })\n *\n * console.log(app.foo) // => 'bar'\n * app.hello('World')\n * ```\n *\n * @name .use\n * @param {Function} `plugin` A function passed with `(app)` signature\n * @return {Object} The `dush` instance for chaining\n * @api public\n */\n\n use (plugin) {\n const ret = plugin(app)\n return ret || app\n },\n\n /**\n * > Add `handler` for `name` event.\n *\n * See [JSBin Example](http://jsbin.com/xeketuruto/edit?js,console).\n *\n * **Example**\n *\n * ```js\n * const emitter = dush()\n *\n * emitter\n * .on('hi', (place) => {\n * console.log(`hello ${place}!`) // => 'hello world!'\n * })\n * .on('hi', (place) => {\n * console.log(`hi ${place}, yeah!`) // => 'hi world, yeah!'\n * })\n *\n * emitter.emit('hi', 'world')\n * ```\n *\n * @name .on\n * @param {String} `name` Type of event to listen for, or `'*'` for all events\n * @param {Function} `handler` Function to call in response to given event\n * @return {Object} The `dush` instance for chaining\n * @api public\n */\n\n on (name, handler, once) {\n let e = all[name] || (all[name] = [])\n\n function func () {\n if (!func.called) {\n app.off(name, func)\n handler.apply(handler, arguments)\n func.called = true\n }\n }\n\n var fn = once ? func : handler\n fn.sourceString = handler.toString()\n\n e.push(fn)\n return app\n },\n\n /**\n * > Add `handler` for `name` event that\n * will be called only one time.\n *\n * See [JSBin Example](http://jsbin.com/teculorima/edit?js,console).\n *\n * **Example**\n *\n * ```js\n * const emitter = dush()\n * let called = 0\n *\n * emitter.once('foo', () => {\n * console.log('called only once')\n * called++\n * })\n *\n * emitter\n * .emit('foo', 111)\n * .emit('foo', 222)\n * .emit('foo', 333)\n *\n * console.log(called) // => 1\n * ```\n *\n * @name .once\n * @param {String} `name` Type of event to listen for, or `'*'` for all events\n * @param {Function} `handler` Function to call in response to given event\n * @return {Object} The `dush` instance for chaining\n * @api public\n */\n\n once (name, handler) {\n app.on(name, handler, true)\n return app\n },\n\n /**\n * > Remove `handler` for `name` event. If `handler` not\n * passed will remove **all** listeners for that `name` event.\n *\n * See [JSBin Example](http://jsbin.com/nujucoquvi/3/edit?js,console).\n *\n * **Example**\n *\n * ```js\n * const emitter = dush()\n *\n * const handler = () => {\n * console.log('not called')\n * }\n *\n * emitter.on('foo', handler)\n * emitter.off('foo', handler)\n *\n * emitter.on('foo', (abc) => {\n * console.log('called', abc) // => 'called 123'\n * })\n * emitter.emit('foo', 123)\n *\n * // or removing all listeners of `foo`\n * emitter.off('foo')\n * emitter.emit('foo')\n * ```\n *\n * @name .off\n * @param {String} `name` Type of event to listen for, or `'*'` for all events\n * @param {Function} `handler` Function to call in response to given event\n * @return {Object} The `dush` instance for chaining\n * @api public\n */\n\n off (name, handler) {\n if (handler && all[name]) {\n const fnStr = handler.toString()\n all[name] = all[name].filter((func) => func.sourceString !== fnStr)\n } else {\n all[name] = []\n }\n\n return app\n },\n\n /**\n * > Invoke all handlers for given `name` event.\n * If present, `'*'` listeners are invoked too with `(type, ...rest)` signature,\n * where the `type` argument is a string representing the name of the\n * called event; and all of the rest arguments.\n *\n * See [JSBin Example](http://jsbin.com/muqujavolu/edit?js,console).\n *\n * **Example**\n *\n * ```js\n * const emitter = dush()\n *\n * emitter.on('foo', (a, b, c) => {\n * console.log(`${a}, ${b}, ${c}`) // => 1, 2, 3\n * })\n *\n * emitter.on('*', (name, a, b, c) => {\n * console.log(`name is: ${name}`)\n * console.log(`rest args are: ${a}, ${b}, ${c}`)\n * })\n *\n * emitter.emit('foo', 1, 2, 3)\n * emitter.emit('bar', 555)\n * ```\n *\n * @name .emit\n * @param {String} `name` The name of the event to invoke\n * @param {any} `args` Any number of arguments of any type of value, passed to each listener\n * @return {Object} The `dush` instance for chaining\n * @api public\n */\n\n emit (name) {\n if (name !== '*') {\n var args = [].slice.call(arguments);\n (all[name] || []).map((handler) => { handler.apply(handler, args.slice(1)) });\n (all['*'] || []).map((handler) => { handler.apply(handler, args) })\n }\n\n return app\n }\n }\n\n return app\n}\n"],"names":["dush","let","all","Object","create","app","use","plugin","on","name","handler","once","func","called","off","apply","arguments","e","fn","sourceString","toString","push","const","fnStr","filter","emit","args","slice","call","map"],"mappings":"mKAiCA,QAAwBA,KACtBC,GAAIC,GAAMC,OAAOC,OAAO,MAClBC,GA2BJH,IAAAA,EA8BAI,aAAKC,GAEH,MADYA,GAAOF,IACLA,GA+BhBG,YAAIC,EAAMC,EAASC,GAGjB,QAASC,KACFA,EAAKC,SACRR,EAAIS,IAAIL,EAAMG,GACdF,EAAQK,MAAML,EAASM,WACvBJ,EAAKC,QAAS,GANlBZ,GAAIgB,GAAIf,EAAIO,KAAUP,EAAIO,OAUtBS,EAAKP,EAAOC,EAAOF,CAIvB,OAHAQ,GAAGC,aAAeT,EAAQU,WAE1BH,EAAEI,KAAKH,GACAb,GAmCTM,cAAMF,EAAMC,GAEV,MADAL,GAAIG,GAAGC,EAAMC,GAAS,GACfL,GAsCTS,aAAKL,EAAMC,GACT,GAAIA,GAAWR,EAAIO,GAAO,CACxBa,GAAMC,GAAQb,EAAQU,UACtBlB,GAAIO,GAAQP,EAAIO,GAAMe,OAAO,SAACZ,SAASA,GAAKO,eAAiBI,QAE7DrB,GAAIO,KAGN,OAAOJ,IAoCToB,cAAMhB,GACJ,GAAa,MAATA,EAAc,CAChB,GAAIiB,MAAUC,MAAMC,KAAKZ,YACxBd,EAAIO,QAAaoB,IAAI,SAACnB,GAAcA,EAAQK,MAAML,EAASgB,EAAKC,MAAM,OACtEzB,EAAI,UAAY2B,IAAI,SAACnB,GAAcA,EAAQK,MAAML,EAASgB,KAG7D,MAAOrB,IAIX,OAAOA"} \ No newline at end of file +{"version":3,"file":"dush.umd.js","sources":["../src/index.js"],"sourcesContent":["/*!\n * dush \n *\n * Copyright (c) Charlike Mike Reagent <@tunnckoCore> (https://i.am.charlike.online)\n * Released under the MIT license.\n */\n\n'use strict'\n\n/**\n * > A constructor function that returns an object\n * with a few methods.\n *\n * See [JSBin Example](http://jsbin.com/mepemeluhi/edit?js,console).\n *\n * **Example**\n *\n * ```js\n * const dush = require('dush')\n * const emitter = dush()\n *\n * console.log(emitter.all) // => {}\n * console.log(emitter.on) // => Function\n * console.log(emitter.once) // => Function\n * console.log(emitter.off) // => Function\n * console.log(emitter.emit) // => Function\n * ```\n *\n * @name dush()\n * @return {Object} methods\n * @api public\n */\n\nexport default function dush () {\n let all = Object.create(null)\n const app = {\n /**\n * > An listeners map of all registered events\n * and their listeners. A key/value store, where 1) value\n * is an array of event listeners for the key and 2) key\n * is the name of the event.\n *\n * See [JSBin Example](http://jsbin.com/zuwayalovi/edit?js,console).\n *\n * **Example**\n *\n * ```js\n * const emitter = dush()\n *\n * emitter.on('foo', () => {})\n * emitter.on('foo', () => {})\n * emitter.on('bar', () => {})\n *\n * console.log(emitter.all)\n * // => { foo: [Function, Function], bar: [Functon] }\n * ```\n *\n * @name .all\n * @type {Object} `all` a key/value store of all events and their listeners\n * @api public\n */\n\n all,\n\n /**\n * > Invokes `plugin` function immediately, which is passed\n * with `app` instance. You can use it for adding more methods\n * or properties to the instance. Useful if you want to make\n * dush to work with DOM for example.\n *\n * ```js\n * const app = dush()\n *\n * app.on('hi', (str) => {\n * console.log(str) // => 'Hello World!!'\n * })\n *\n * app.use((app) => {\n * app.foo = 'bar'\n * app.hello = (place) => app.emit('hi', `Hello ${place}!!`)\n * })\n *\n * console.log(app.foo) // => 'bar'\n * app.hello('World')\n * ```\n *\n * @name .use\n * @param {Function} `plugin` A function passed with `(app)` signature\n * @return {Object} The `dush` instance for chaining\n * @api public\n */\n\n use (plugin) {\n const ret = plugin(app)\n return ret || app\n },\n\n /**\n * > Add `handler` for `name` event.\n *\n * See [JSBin Example](http://jsbin.com/xeketuruto/edit?js,console).\n *\n * **Example**\n *\n * ```js\n * const emitter = dush()\n *\n * emitter\n * .on('hi', (place) => {\n * console.log(`hello ${place}!`) // => 'hello world!'\n * })\n * .on('hi', (place) => {\n * console.log(`hi ${place}, yeah!`) // => 'hi world, yeah!'\n * })\n *\n * emitter.emit('hi', 'world')\n * ```\n *\n * @name .on\n * @param {String} `name` Type of event to listen for, or `'*'` for all events\n * @param {Function} `handler` Function to call in response to given event\n * @return {Object} The `dush` instance for chaining\n * @api public\n */\n\n on (name, handler, once) {\n let e = app.all[name] || (app.all[name] = [])\n\n function func () {\n if (!func.called) {\n app.off(name, func)\n handler.apply(handler, arguments)\n func.called = true\n }\n }\n\n var fn = once ? func : handler\n fn.sourceString = handler.toString()\n\n e.push(fn)\n return app\n },\n\n /**\n * > Add `handler` for `name` event that\n * will be called only one time.\n *\n * See [JSBin Example](http://jsbin.com/teculorima/edit?js,console).\n *\n * **Example**\n *\n * ```js\n * const emitter = dush()\n * let called = 0\n *\n * emitter.once('foo', () => {\n * console.log('called only once')\n * called++\n * })\n *\n * emitter\n * .emit('foo', 111)\n * .emit('foo', 222)\n * .emit('foo', 333)\n *\n * console.log(called) // => 1\n * ```\n *\n * @name .once\n * @param {String} `name` Type of event to listen for, or `'*'` for all events\n * @param {Function} `handler` Function to call in response to given event\n * @return {Object} The `dush` instance for chaining\n * @api public\n */\n\n once (name, handler) {\n app.on(name, handler, true)\n return app\n },\n\n /**\n * > Remove `handler` for `name` event. If `handler` not\n * passed will remove **all** listeners for that `name` event.\n *\n * See [JSBin Example](http://jsbin.com/nujucoquvi/3/edit?js,console).\n *\n * **Example**\n *\n * ```js\n * const emitter = dush()\n *\n * const handler = () => {\n * console.log('not called')\n * }\n *\n * emitter.on('foo', handler)\n * emitter.off('foo', handler)\n *\n * emitter.on('foo', (abc) => {\n * console.log('called', abc) // => 'called 123'\n * })\n * emitter.emit('foo', 123)\n *\n * // or removing all listeners of `foo`\n * emitter.off('foo')\n * emitter.emit('foo')\n * ```\n *\n * @name .off\n * @param {String} `name` Type of event to listen for, or `'*'` for all events\n * @param {Function} `handler` Function to call in response to given event\n * @return {Object} The `dush` instance for chaining\n * @api public\n */\n\n off (name, handler) {\n if (handler && all[name]) {\n const fnStr = handler.toString()\n all[name] = all[name].filter((func) => func.sourceString !== fnStr)\n } else if (name) {\n all[name] = []\n } else {\n app.all = Object.create(null)\n }\n\n return app\n },\n\n /**\n * > Invoke all handlers for given `name` event.\n * If present, `'*'` listeners are invoked too with `(type, ...rest)` signature,\n * where the `type` argument is a string representing the name of the\n * called event; and all of the rest arguments.\n *\n * See [JSBin Example](http://jsbin.com/muqujavolu/edit?js,console).\n *\n * **Example**\n *\n * ```js\n * const emitter = dush()\n *\n * emitter.on('foo', (a, b, c) => {\n * console.log(`${a}, ${b}, ${c}`) // => 1, 2, 3\n * })\n *\n * emitter.on('*', (name, a, b, c) => {\n * console.log(`name is: ${name}`)\n * console.log(`rest args are: ${a}, ${b}, ${c}`)\n * })\n *\n * emitter.emit('foo', 1, 2, 3)\n * emitter.emit('bar', 555)\n * ```\n *\n * @name .emit\n * @param {String} `name` The name of the event to invoke\n * @param {any} `args` Any number of arguments of any type of value, passed to each listener\n * @return {Object} The `dush` instance for chaining\n * @api public\n */\n\n emit (name) {\n if (name !== '*') {\n var args = [].slice.call(arguments);\n (all[name] || []).map((handler) => { handler.apply(handler, args.slice(1)) });\n (all['*'] || []).map((handler) => { handler.apply(handler, args) })\n }\n\n return app\n }\n }\n\n return app\n}\n"],"names":["dush","let","all","Object","create","app","use","plugin","on","name","handler","once","func","called","off","apply","arguments","e","fn","sourceString","toString","push","const","fnStr","filter","emit","args","slice","call","map"],"mappings":"mKAiCA,QAAwBA,KACtBC,GAAIC,GAAMC,OAAOC,OAAO,MAClBC,GA2BJH,IAAAA,EA8BAI,aAAKC,GAEH,MADYA,GAAOF,IACLA,GA+BhBG,YAAIC,EAAMC,EAASC,GAGjB,QAASC,KACFA,EAAKC,SACRR,EAAIS,IAAIL,EAAMG,GACdF,EAAQK,MAAML,EAASM,WACvBJ,EAAKC,QAAS,GANlBZ,GAAIgB,GAAIZ,EAAIH,IAAIO,KAAUJ,EAAIH,IAAIO,OAU9BS,EAAKP,EAAOC,EAAOF,CAIvB,OAHAQ,GAAGC,aAAeT,EAAQU,WAE1BH,EAAEI,KAAKH,GACAb,GAmCTM,cAAMF,EAAMC,GAEV,MADAL,GAAIG,GAAGC,EAAMC,GAAS,GACfL,GAsCTS,aAAKL,EAAMC,GACT,GAAIA,GAAWR,EAAIO,GAAO,CACxBa,GAAMC,GAAQb,EAAQU,UACtBlB,GAAIO,GAAQP,EAAIO,GAAMe,OAAO,SAACZ,SAASA,GAAKO,eAAiBI,QACpDd,GACTP,EAAIO,MAEJJ,EAAIH,IAAMC,OAAOC,OAAO,KAG1B,OAAOC,IAoCToB,cAAMhB,GACJ,GAAa,MAATA,EAAc,CAChB,GAAIiB,MAAUC,MAAMC,KAAKZ,YACxBd,EAAIO,QAAaoB,IAAI,SAACnB,GAAcA,EAAQK,MAAML,EAASgB,EAAKC,MAAM,OACtEzB,EAAI,UAAY2B,IAAI,SAACnB,GAAcA,EAAQK,MAAML,EAASgB,KAG7D,MAAOrB,IAIX,OAAOA"} \ No newline at end of file diff --git a/src/index.js b/src/index.js index 8b0b87e..052b0be 100644 --- a/src/index.js +++ b/src/index.js @@ -124,7 +124,7 @@ export default function dush () { */ on (name, handler, once) { - let e = all[name] || (all[name] = []) + let e = app.all[name] || (app.all[name] = []) function func () { if (!func.called) { @@ -217,8 +217,10 @@ export default function dush () { if (handler && all[name]) { const fnStr = handler.toString() all[name] = all[name].filter((func) => func.sourceString !== fnStr) - } else { + } else if (name) { all[name] = [] + } else { + app.all = Object.create(null) } return app diff --git a/test.js b/test.js index c533b01..8a4641e 100644 --- a/test.js +++ b/test.js @@ -267,3 +267,25 @@ test('should `.on` work as `.once` if third argument is true', function (done) { test.strictEqual(calls, 1) done() }) + +test('should `.off()` remove all listeners', function (done) { + var app = dush() + var fixture = function () {} + app.on('a', fixture) + app.once('a', fixture) + app.on('a', fixture) + app.on('b', fixture) + app.once('b', fixture) + app.on('c', fixture) + + var evts = Object.keys(app.all) + test.strictEqual(evts.length, 3) + test.strictEqual(app.all.a.length, 3) + test.strictEqual(app.all.b.length, 2) + test.strictEqual(app.all.c.length, 1) + + app.off() + var allEvents = Object.keys(app.all) + test.strictEqual(allEvents.length, 0) + done() +})