diff --git a/dist/dush.common.js b/dist/dush.common.js index 32bb5a3..b900754 100644 --- a/dist/dush.common.js +++ b/dist/dush.common.js @@ -214,11 +214,11 @@ function dush () { */ off: function off (name, handler) { - if (handler && all[name]) { + if (handler && app.all[name]) { var fnStr = handler.toString(); - all[name] = all[name].filter(function (func) { return func.sourceString !== fnStr; }); + app.all[name] = app.all[name].filter(function (func) { return func.sourceString !== fnStr; }); } else if (name) { - all[name] = []; + app.all[name] = []; } else { app.all = Object.create(null); } @@ -262,8 +262,8 @@ function dush () { emit: function emit (name) { if (name !== '*') { var args = [].slice.call(arguments); - (all[name] || []).map(function (handler) { handler.apply(handler, args.slice(1)); }); - (all['*'] || []).map(function (handler) { handler.apply(handler, args); }); + (app.all[name] || []).map(function (handler) { handler.apply(handler, args.slice(1)); }); + (app.all['*'] || []).map(function (handler) { handler.apply(handler, args); }); } return app diff --git a/dist/dush.es.js b/dist/dush.es.js index a46bb25..5b80a54 100644 --- a/dist/dush.es.js +++ b/dist/dush.es.js @@ -212,11 +212,11 @@ function dush () { */ off: function off (name, handler) { - if (handler && all[name]) { + if (handler && app.all[name]) { var fnStr = handler.toString(); - all[name] = all[name].filter(function (func) { return func.sourceString !== fnStr; }); + app.all[name] = app.all[name].filter(function (func) { return func.sourceString !== fnStr; }); } else if (name) { - all[name] = []; + app.all[name] = []; } else { app.all = Object.create(null); } @@ -260,8 +260,8 @@ function dush () { emit: function emit (name) { if (name !== '*') { var args = [].slice.call(arguments); - (all[name] || []).map(function (handler) { handler.apply(handler, args.slice(1)); }); - (all['*'] || []).map(function (handler) { handler.apply(handler, args); }); + (app.all[name] || []).map(function (handler) { handler.apply(handler, args.slice(1)); }); + (app.all['*'] || []).map(function (handler) { handler.apply(handler, args); }); } return app diff --git a/dist/dush.umd.js b/dist/dush.umd.js index 154b14d..cf1ad7a 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(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}); +!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,l){function r(){r.called||(e.off(n,r),t.apply(t,arguments),r.called=!0)}var u=e.all[n]||(e.all[n]=[]),o=l?r:t;return o.sourceString=t.toString(),u.push(o),e},once:function(n,t){return e.on(n,t,!0),e},off:function(n,t){if(t&&e.all[n]){var l=t.toString();e.all[n]=e.all[n].filter(function(n){return n.sourceString!==l})}else n?e.all[n]=[]:e.all=Object.create(null);return e},emit:function(n){if("*"!==n){var t=[].slice.call(arguments);(e.all[n]||[]).map(function(n){n.apply(n,t.slice(1))}),(e.all["*"]||[]).map(function(n){n.apply(n,t)})}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 0900f45..5816593 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 c1d8052..41b32b8 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 = 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 +{"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 && app.all[name]) {\n const fnStr = handler.toString()\n app.all[name] = app.all[name].filter((func) => func.sourceString !== fnStr)\n } else if (name) {\n app.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 (app.all[name] || []).map((handler) => { handler.apply(handler, args.slice(1)) });\n (app.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,GAAWL,EAAIH,IAAIO,GAAO,CAC5Ba,GAAMC,GAAQb,EAAQU,UACtBf,GAAIH,IAAIO,GAAQJ,EAAIH,IAAIO,GAAMe,OAAO,SAACZ,SAASA,GAAKO,eAAiBI,QAC5Dd,GACTJ,EAAIH,IAAIO,MAERJ,EAAIH,IAAMC,OAAOC,OAAO,KAG1B,OAAOC,IAoCToB,cAAMhB,GACJ,GAAa,MAATA,EAAc,CAChB,GAAIiB,MAAUC,MAAMC,KAAKZ,YACxBX,EAAIH,IAAIO,QAAaoB,IAAI,SAACnB,GAAcA,EAAQK,MAAML,EAASgB,EAAKC,MAAM,OAC1EtB,EAAIH,IAAI,UAAY2B,IAAI,SAACnB,GAAcA,EAAQK,MAAML,EAASgB,KAGjE,MAAOrB,IAIX,OAAOA"} \ No newline at end of file diff --git a/src/index.js b/src/index.js index 052b0be..dc03c07 100644 --- a/src/index.js +++ b/src/index.js @@ -214,11 +214,11 @@ export default function dush () { */ off (name, handler) { - if (handler && all[name]) { + if (handler && app.all[name]) { const fnStr = handler.toString() - all[name] = all[name].filter((func) => func.sourceString !== fnStr) + app.all[name] = app.all[name].filter((func) => func.sourceString !== fnStr) } else if (name) { - all[name] = [] + app.all[name] = [] } else { app.all = Object.create(null) } @@ -262,8 +262,8 @@ export default function dush () { emit (name) { if (name !== '*') { var args = [].slice.call(arguments); - (all[name] || []).map((handler) => { handler.apply(handler, args.slice(1)) }); - (all['*'] || []).map((handler) => { handler.apply(handler, args) }) + (app.all[name] || []).map((handler) => { handler.apply(handler, args.slice(1)) }); + (app.all['*'] || []).map((handler) => { handler.apply(handler, args) }) } return app