diff --git a/dist/dush.common.js b/dist/dush.common.js index 820cf70..de12673 100644 --- a/dist/dush.common.js +++ b/dist/dush.common.js @@ -267,7 +267,8 @@ function dush () { emit: function emit (name) { if (name !== '*') { - var args = [].slice.call(arguments);(app._allEvents[name] || []).map(function (handler) { + var args = [].slice.call(arguments) + ;(app._allEvents[name] || []).map(function (handler) { handler.apply(handler, args.slice(1)); }) ;(app._allEvents['*'] || []).map(function (handler) { diff --git a/dist/dush.es.js b/dist/dush.es.js index 8b8235c..1a39105 100644 --- a/dist/dush.es.js +++ b/dist/dush.es.js @@ -265,7 +265,8 @@ function dush () { emit: function emit (name) { if (name !== '*') { - var args = [].slice.call(arguments);(app._allEvents[name] || []).map(function (handler) { + var args = [].slice.call(arguments) + ;(app._allEvents[name] || []).map(function (handler) { handler.apply(handler, args.slice(1)); }) ;(app._allEvents['*'] || []).map(function (handler) { diff --git a/dist/dush.umd.js b/dist/dush.umd.js index 4a96a5e..817f11b 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(){return function(){var n={_allEvents:Object.create(null),use:function(e,t){return e(n,t)||n},on:function(e,t,l){function u(){n.off(e,u),t.apply(t,arguments)}var f=n._allEvents[e]||(n._allEvents[e]=[]);u.fn=t;var o=l?u:t;return f.push(o),n},once:function(e,t){return n.on(e,t,!0),n},off:function(e,t){return t&&n._allEvents[e]?n._allEvents[e]=n._allEvents[e].filter(function(n){return n!==t&&n!==t.fn}):e?n._allEvents[e]=[]:n._allEvents=Object.create(null),n},emit:function(e){if("*"!==e){var t=[].slice.call(arguments);(n._allEvents[e]||[]).map(function(n){n.apply(n,t.slice(1))}),(n._allEvents["*"]||[]).map(function(n){n.apply(n,t)})}return n}};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(){"use strict";return function(){var n={_allEvents:Object.create(null),use:function(e,t){return e(n,t)||n},on:function(e,t,l){function u(){n.off(e,u),t.apply(t,arguments)}var f=n._allEvents[e]||(n._allEvents[e]=[]);u.fn=t;var o=l?u:t;return f.push(o),n},once:function(e,t){return n.on(e,t,!0),n},off:function(e,t){return t&&n._allEvents[e]?n._allEvents[e]=n._allEvents[e].filter(function(n){return n!==t&&n!==t.fn}):e?n._allEvents[e]=[]:n._allEvents=Object.create(null),n},emit:function(e){if("*"!==e){var t=[].slice.call(arguments);(n._allEvents[e]||[]).map(function(n){n.apply(n,t.slice(1))}),(n._allEvents["*"]||[]).map(function(n){n.apply(n,t)})}return n}};return n}}); //# sourceMappingURL=dush.umd.js.map diff --git a/dist/dush.umd.js.gz b/dist/dush.umd.js.gz index 38df050..42a8138 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 1511dc0..7edd644 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 * **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, options)` signature\n * @param {Object} `options` optional, passed as second argument to `plugin` function\n * @return {Object} self \"app\" for chaining\n * @api public\n */\n\n use (plugin, options) {\n const ret = plugin(app, options)\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} self \"app\" 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 app.off(name, func)\n handler.apply(handler, arguments)\n }\n func.fn = handler\n\n var fn = once ? func : handler\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} self \"app\" 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} self \"app\" for chaining\n * @api public\n */\n\n off (name, handler) {\n if (handler && app._allEvents[name]) {\n app._allEvents[name] = app._allEvents[name].filter(\n (func) => func !== handler && func !== handler.fn\n )\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} self \"app\" 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) => {\n handler.apply(handler, args.slice(1))\n })\n ;(app._allEvents['*'] || []).map((handler) => {\n handler.apply(handler, args)\n })\n }\n\n return app\n }\n }\n\n return app\n}\n"],"names":["let","app","_allEvents","Object","create","use","plugin","options","on","name","handler","once","func","off","apply","arguments","e","fn","push","filter","emit","args","slice","call","map"],"mappings":"0KAiCA,WACEA,IACMC,GA8BJC,WA/BeC,OAAOC,OAAO,MAgE7BC,aAAKC,EAAQC,GAEX,OADYD,EAAOL,EAAKM,IACVN,GAiChBO,YAAIC,EAAMC,EAASC,GAGjB,SAASC,IACPX,EAAIY,IAAIJ,EAAMG,GACdF,EAAQI,MAAMJ,EAASK,WAJzBf,IAAIgB,EAAIf,EAAIC,WAAWO,KAAUR,EAAIC,WAAWO,OAMhDG,EAAKK,GAAKP,EAEV,IAAIO,EAAKN,EAAOC,EAAOF,EAGvB,OADAM,EAAEE,KAAKD,GACAhB,GAmCTU,cAAMF,EAAMC,GAEV,OADAT,EAAIO,GAAGC,EAAMC,GAAS,GACfT,GAsCTY,aAAKJ,EAAMC,GAWT,OAVIA,GAAWT,EAAIC,WAAWO,GAC5BR,EAAIC,WAAWO,GAAQR,EAAIC,WAAWO,GAAMU,OAC1C,SAACP,UAASA,IAASF,GAAWE,IAASF,EAAQO,KAExCR,EACTR,EAAIC,WAAWO,MAEfR,EAAIC,WAAaC,OAAOC,OAAO,MAG1BH,GAoCTmB,cAAMX,GACJ,GAAa,MAATA,EAAc,CAChB,IAAIY,KAAUC,MAAMC,KAAKR,YACvBd,EAAIC,WAAWO,QAAae,IAAI,SAACd,GACjCA,EAAQI,MAAMJ,EAASW,EAAKC,MAAM,OAElCrB,EAAIC,WAAW,UAAYsB,IAAI,SAACd,GAChCA,EAAQI,MAAMJ,EAASW,KAI3B,OAAOpB,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 * **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, options)` signature\n * @param {Object} `options` optional, passed as second argument to `plugin` function\n * @return {Object} self \"app\" for chaining\n * @api public\n */\n\n use (plugin, options) {\n const ret = plugin(app, options)\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} self \"app\" 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 app.off(name, func)\n handler.apply(handler, arguments)\n }\n func.fn = handler\n\n var fn = once ? func : handler\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} self \"app\" 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} self \"app\" for chaining\n * @api public\n */\n\n off (name, handler) {\n if (handler && app._allEvents[name]) {\n app._allEvents[name] = app._allEvents[name].filter(\n (func) => func !== handler && func !== handler.fn\n )\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} self \"app\" 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) => {\n handler.apply(handler, args.slice(1))\n })\n ;(app._allEvents['*'] || []).map((handler) => {\n handler.apply(handler, args)\n })\n }\n\n return app\n }\n }\n\n return app\n}\n"],"names":["let","app","_allEvents","Object","create","use","plugin","options","on","name","handler","once","func","off","apply","arguments","e","fn","push","filter","emit","args","slice","call","map"],"mappings":"uLAiCe,WACbA,IACMC,GA8BJC,WA/BeC,OAAOC,OAAO,MAgE7BC,aAAKC,EAAQC,GAEX,OADYD,EAAOL,EAAKM,IACVN,GAiChBO,YAAIC,EAAMC,EAASC,GAGjB,SAASC,IACPX,EAAIY,IAAIJ,EAAMG,GACdF,EAAQI,MAAMJ,EAASK,WAJzBf,IAAIgB,EAAIf,EAAIC,WAAWO,KAAUR,EAAIC,WAAWO,OAMhDG,EAAKK,GAAKP,EAEV,IAAIO,EAAKN,EAAOC,EAAOF,EAGvB,OADAM,EAAEE,KAAKD,GACAhB,GAmCTU,cAAMF,EAAMC,GAEV,OADAT,EAAIO,GAAGC,EAAMC,GAAS,GACfT,GAsCTY,aAAKJ,EAAMC,GAWT,OAVIA,GAAWT,EAAIC,WAAWO,GAC5BR,EAAIC,WAAWO,GAAQR,EAAIC,WAAWO,GAAMU,OAC1C,SAACP,UAASA,IAASF,GAAWE,IAASF,EAAQO,KAExCR,EACTR,EAAIC,WAAWO,MAEfR,EAAIC,WAAaC,OAAOC,OAAO,MAG1BH,GAoCTmB,cAAMX,GACJ,GAAa,MAATA,EAAc,CAChB,IAAIY,KAAUC,MAAMC,KAAKR,YACvBd,EAAIC,WAAWO,QAAae,IAAI,SAACd,GACjCA,EAAQI,MAAMJ,EAASW,EAAKC,MAAM,OAElCrB,EAAIC,WAAW,UAAYsB,IAAI,SAACd,GAChCA,EAAQI,MAAMJ,EAASW,KAI3B,OAAOpB,IAIX,OAAOA"} \ No newline at end of file diff --git a/package.json b/package.json index 6a738ea..ba3b52e 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,8 @@ "standard-version": "^4.4.0" }, "files": [ - "dist/" + "dist", + "src" ], "keywords": [ "browser",