diff --git a/dist/dush.common.js b/dist/dush.common.js index 89987bc..7f8e79f 100644 --- a/dist/dush.common.js +++ b/dist/dush.common.js @@ -140,7 +140,7 @@ function dush () { } var fn = once ? func : handler; - fn.sourceString = handler.toString(); + fn.__sourceString = handler.toString(); e.push(fn); return app @@ -221,7 +221,7 @@ function dush () { off: function off (name, handler) { if (handler && app._allEvents[name]) { var fnStr = handler.toString(); - app._allEvents[name] = app._allEvents[name].filter(function (func) { return func.sourceString !== fnStr; }); + app._allEvents[name] = app._allEvents[name].filter(function (func) { return func.__sourceString !== fnStr; }); } else if (name) { app._allEvents[name] = []; } else { diff --git a/dist/dush.es.js b/dist/dush.es.js index b496676..0ab240d 100644 --- a/dist/dush.es.js +++ b/dist/dush.es.js @@ -138,7 +138,7 @@ function dush () { } var fn = once ? func : handler; - fn.sourceString = handler.toString(); + fn.__sourceString = handler.toString(); e.push(fn); return app @@ -219,7 +219,7 @@ function dush () { off: function off (name, handler) { if (handler && app._allEvents[name]) { var fnStr = handler.toString(); - app._allEvents[name] = app._allEvents[name].filter(function (func) { return func.sourceString !== fnStr; }); + app._allEvents[name] = app._allEvents[name].filter(function (func) { return func.__sourceString !== fnStr; }); } else if (name) { app._allEvents[name] = []; } else { diff --git a/dist/dush.umd.js b/dist/dush.umd.js index 948b6b7..a78837e 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={_allEvents: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._allEvents[n]||(e._allEvents[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._allEvents[n]){var l=t.toString();e._allEvents[n]=e._allEvents[n].filter(function(n){return n.sourceString!==l})}else n?e._allEvents[n]=[]:e._allEvents=Object.create(null);return e},emit:function(n){if("*"!==n){var t=[].slice.call(arguments);(e._allEvents[n]||[]).map(function(n){n.apply(n,t.slice(1))}),(e._allEvents["*"]||[]).map(function(n){n.apply(n,t)})}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={_allEvents: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._allEvents[n]||(e._allEvents[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._allEvents[n]){var l=t.toString();e._allEvents[n]=e._allEvents[n].filter(function(n){return n.__sourceString!==l})}else n?e._allEvents[n]=[]:e._allEvents=Object.create(null);return e},emit:function(n){if("*"!==n){var t=[].slice.call(arguments);(e._allEvents[n]||[]).map(function(n){n.apply(n,t.slice(1))}),(e._allEvents["*"]||[]).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 b7c7aac..f59bd13 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 402d85e..30f98e1 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._allEvents) // => {}\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 _allEvents = 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/fakajazafu/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._allEvents)\n * // => { foo: [Function, Function], bar: [Functon] }\n *\n * console.log(emitter._allEvents.foo.length) // => 2\n * console.log(emitter._allEvents.bar.length) // => 1\n * ```\n *\n * @name ._allEvents\n * @type {Object} `_allEvents` a key/value store of all events and their listeners\n * @api public\n */\n\n _allEvents,\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 * @param {Boolean} `once` Make `handler` be called only once,\n * the `.once` method use this internally\n * @return {Object} The `dush` instance for chaining\n * @api public\n */\n\n on (name, handler, once) {\n let e = app._allEvents[name] || (app._allEvents[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._allEvents[name]) {\n const fnStr = handler.toString()\n app._allEvents[name] = app._allEvents[name].filter((func) => func.sourceString !== fnStr)\n } else if (name) {\n app._allEvents[name] = []\n } else {\n app._allEvents = 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._allEvents[name] || []).map((handler) => { handler.apply(handler, args.slice(1)) });\n (app._allEvents['*'] || []).map((handler) => { handler.apply(handler, args) })\n }\n\n return app\n }\n }\n\n return app\n}\n"],"names":["dush","let","_allEvents","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,GAAaC,OAAOC,OAAO,MACzBC,GA8BJH,WAAAA,EA8BAI,aAAKC,GAEH,MADYA,GAAOF,IACLA,GAiChBG,YAAIC,EAAMC,EAASC,GAGjB,QAASC,KACFA,EAAKC,SACRR,EAAIS,IAAIL,EAAMG,GACdF,EAAQK,MAAML,EAASM,WACvBJ,EAAKC,QAAS,GANlBZ,GAAIgB,GAAIZ,EAAIH,WAAWO,KAAUJ,EAAIH,WAAWO,OAU5CS,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,WAAWO,GAAO,CACnCa,GAAMC,GAAQb,EAAQU,UACtBf,GAAIH,WAAWO,GAAQJ,EAAIH,WAAWO,GAAMe,OAAO,SAACZ,SAASA,GAAKO,eAAiBI,QAC1Ed,GACTJ,EAAIH,WAAWO,MAEfJ,EAAIH,WAAaC,OAAOC,OAAO,KAGjC,OAAOC,IAoCToB,cAAMhB,GACJ,GAAa,MAATA,EAAc,CAChB,GAAIiB,MAAUC,MAAMC,KAAKZ,YACxBX,EAAIH,WAAWO,QAAaoB,IAAI,SAACnB,GAAcA,EAAQK,MAAML,EAASgB,EAAKC,MAAM,OACjFtB,EAAIH,WAAW,UAAY2B,IAAI,SAACnB,GAAcA,EAAQK,MAAML,EAASgB,KAGxE,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._allEvents) // => {}\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 _allEvents = 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/fakajazafu/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._allEvents)\n * // => { foo: [Function, Function], bar: [Functon] }\n *\n * console.log(emitter._allEvents.foo.length) // => 2\n * console.log(emitter._allEvents.bar.length) // => 1\n * ```\n *\n * @name ._allEvents\n * @type {Object} `_allEvents` a key/value store of all events and their listeners\n * @api public\n */\n\n _allEvents,\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 * @param {Boolean} `once` Make `handler` be called only once,\n * the `.once` method use this internally\n * @return {Object} The `dush` instance for chaining\n * @api public\n */\n\n on (name, handler, once) {\n let e = app._allEvents[name] || (app._allEvents[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._allEvents[name]) {\n const fnStr = handler.toString()\n app._allEvents[name] = app._allEvents[name].filter((func) => func.__sourceString !== fnStr)\n } else if (name) {\n app._allEvents[name] = []\n } else {\n app._allEvents = 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._allEvents[name] || []).map((handler) => { handler.apply(handler, args.slice(1)) });\n (app._allEvents['*'] || []).map((handler) => { handler.apply(handler, args) })\n }\n\n return app\n }\n }\n\n return app\n}\n"],"names":["dush","let","_allEvents","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,GAAaC,OAAOC,OAAO,MACzBC,GA8BJH,WAAAA,EA8BAI,aAAKC,GAEH,MADYA,GAAOF,IACLA,GAiChBG,YAAIC,EAAMC,EAASC,GAGjB,QAASC,KACFA,EAAKC,SACRR,EAAIS,IAAIL,EAAMG,GACdF,EAAQK,MAAML,EAASM,WACvBJ,EAAKC,QAAS,GANlBZ,GAAIgB,GAAIZ,EAAIH,WAAWO,KAAUJ,EAAIH,WAAWO,OAU5CS,EAAKP,EAAOC,EAAOF,CAIvB,OAHAQ,GAAGC,eAAiBT,EAAQU,WAE5BH,EAAEI,KAAKH,GACAb,GAmCTM,cAAMF,EAAMC,GAEV,MADAL,GAAIG,GAAGC,EAAMC,GAAS,GACfL,GAsCTS,aAAKL,EAAMC,GACT,GAAIA,GAAWL,EAAIH,WAAWO,GAAO,CACnCa,GAAMC,GAAQb,EAAQU,UACtBf,GAAIH,WAAWO,GAAQJ,EAAIH,WAAWO,GAAMe,OAAO,SAACZ,SAASA,GAAKO,iBAAmBI,QAC5Ed,GACTJ,EAAIH,WAAWO,MAEfJ,EAAIH,WAAaC,OAAOC,OAAO,KAGjC,OAAOC,IAoCToB,cAAMhB,GACJ,GAAa,MAATA,EAAc,CAChB,GAAIiB,MAAUC,MAAMC,KAAKZ,YACxBX,EAAIH,WAAWO,QAAaoB,IAAI,SAACnB,GAAcA,EAAQK,MAAML,EAASgB,EAAKC,MAAM,OACjFtB,EAAIH,WAAW,UAAY2B,IAAI,SAACnB,GAAcA,EAAQK,MAAML,EAASgB,KAGxE,MAAOrB,IAIX,OAAOA"} \ No newline at end of file diff --git a/src/index.js b/src/index.js index ca11778..8c9dcdd 100644 --- a/src/index.js +++ b/src/index.js @@ -140,7 +140,7 @@ export default function dush () { } var fn = once ? func : handler - fn.sourceString = handler.toString() + fn.__sourceString = handler.toString() e.push(fn) return app @@ -221,7 +221,7 @@ export default function dush () { off (name, handler) { if (handler && app._allEvents[name]) { const fnStr = handler.toString() - app._allEvents[name] = app._allEvents[name].filter((func) => func.sourceString !== fnStr) + app._allEvents[name] = app._allEvents[name].filter((func) => func.__sourceString !== fnStr) } else if (name) { app._allEvents[name] = [] } else {