diff --git a/README.md b/README.md index 846eafb..a53e33d 100644 --- a/README.md +++ b/README.md @@ -189,13 +189,14 @@ console.log(emitter._allEvents.foo.length) // => 2 console.log(emitter._allEvents.bar.length) // => 1 ``` -### [.use](src/index.js#L96) +### [.use](src/index.js#L99) > Invokes `plugin` function immediately, which is passed with `app` instance. You can use it for adding more methods or properties to the instance. Useful if you want to make dush to work with DOM for example. **Params** -* `plugin` **{Function}**: A function passed with `(app)` signature -* `returns` **{Object}**: The `dush` instance for chaining +* `plugin` **{Function}**: A function passed with `(app, options)` signature +* `options` **{Object}**: optional, passed as second argument to `plugin` function +* `returns` **{Object}**: self "app" for chaining **Example** @@ -215,7 +216,7 @@ console.log(app.foo) // => 'bar' app.hello('World') ``` -### [.on](src/index.js#L131) +### [.on](src/index.js#L134) > Add `handler` for `name` event. See [JSBin Example](http://jsbin.com/xeketuruto/edit?js,console). @@ -225,7 +226,7 @@ See [JSBin Example](http://jsbin.com/xeketuruto/edit?js,console). * `name` **{String}**: Type of event to listen for, or `'*'` for all events * `handler` **{Function}**: Function to call in response to given event * `once` **{Boolean}**: Make `handler` be called only once, the `.once` method use this internally -* `returns` **{Object}**: The `dush` instance for chaining +* `returns` **{Object}**: self "app" for chaining **Example** @@ -243,7 +244,7 @@ emitter emitter.emit('hi', 'world') ``` -### [.once](src/index.js#L181) +### [.once](src/index.js#L184) > Add `handler` for `name` event that will be called only one time. See [JSBin Example](http://jsbin.com/teculorima/edit?js,console). @@ -252,7 +253,7 @@ See [JSBin Example](http://jsbin.com/teculorima/edit?js,console). * `name` **{String}**: Type of event to listen for, or `'*'` for all events * `handler` **{Function}**: Function to call in response to given event -* `returns` **{Object}**: The `dush` instance for chaining +* `returns` **{Object}**: self "app" for chaining **Example** @@ -273,7 +274,7 @@ emitter console.log(called) // => 1 ``` -### [.off](src/index.js#L221) +### [.off](src/index.js#L224) > Remove `handler` for `name` event. If `handler` not passed will remove **all** listeners for that `name` event. See [JSBin Example](http://jsbin.com/nujucoquvi/3/edit?js,console). @@ -282,7 +283,7 @@ See [JSBin Example](http://jsbin.com/nujucoquvi/3/edit?js,console). * `name` **{String}**: Type of event to listen for, or `'*'` for all events * `handler` **{Function}**: Function to call in response to given event -* `returns` **{Object}**: The `dush` instance for chaining +* `returns` **{Object}**: self "app" for chaining **Example** @@ -306,7 +307,7 @@ emitter.off('foo') emitter.emit('foo') ``` -### [.emit](src/index.js#L267) +### [.emit](src/index.js#L270) > Invoke all handlers for given `name` event. If present, `'*'` listeners are invoked too with `(type, ...rest)` signature, where the `type` argument is a string representing the name of the called event; and all of the rest arguments. See [JSBin Example](http://jsbin.com/muqujavolu/edit?js,console). @@ -315,7 +316,7 @@ See [JSBin Example](http://jsbin.com/muqujavolu/edit?js,console). * `name` **{String}**: The name of the event to invoke * `args` **{any}**: Any number of arguments of any type of value, passed to each listener -* `returns` **{Object}**: The `dush` instance for chaining +* `returns` **{Object}**: self "app" for chaining **Example** diff --git a/dist/dush.common.js b/dist/dush.common.js index 7f8e79f..c13d129 100644 --- a/dist/dush.common.js +++ b/dist/dush.common.js @@ -71,6 +71,8 @@ function dush () { * or properties to the instance. Useful if you want to make * dush to work with DOM for example. * + * **Example** + * * ```js * const app = dush() * @@ -88,13 +90,14 @@ function dush () { * ``` * * @name .use - * @param {Function} `plugin` A function passed with `(app)` signature - * @return {Object} The `dush` instance for chaining + * @param {Function} `plugin` A function passed with `(app, options)` signature + * @param {Object} `options` optional, passed as second argument to `plugin` function + * @return {Object} self "app" for chaining * @api public */ - use: function use (plugin) { - var ret = plugin(app); + use: function use (plugin, options) { + var ret = plugin(app, options); return ret || app }, @@ -124,7 +127,7 @@ function dush () { * @param {Function} `handler` Function to call in response to given event * @param {Boolean} `once` Make `handler` be called only once, * the `.once` method use this internally - * @return {Object} The `dush` instance for chaining + * @return {Object} self "app" for chaining * @api public */ @@ -174,7 +177,7 @@ function dush () { * @name .once * @param {String} `name` Type of event to listen for, or `'*'` for all events * @param {Function} `handler` Function to call in response to given event - * @return {Object} The `dush` instance for chaining + * @return {Object} self "app" for chaining * @api public */ @@ -214,7 +217,7 @@ function dush () { * @name .off * @param {String} `name` Type of event to listen for, or `'*'` for all events * @param {Function} `handler` Function to call in response to given event - * @return {Object} The `dush` instance for chaining + * @return {Object} self "app" for chaining * @api public */ @@ -260,7 +263,7 @@ function dush () { * @name .emit * @param {String} `name` The name of the event to invoke * @param {any} `args` Any number of arguments of any type of value, passed to each listener - * @return {Object} The `dush` instance for chaining + * @return {Object} self "app" for chaining * @api public */ diff --git a/dist/dush.es.js b/dist/dush.es.js index 0ab240d..03b206b 100644 --- a/dist/dush.es.js +++ b/dist/dush.es.js @@ -69,6 +69,8 @@ function dush () { * or properties to the instance. Useful if you want to make * dush to work with DOM for example. * + * **Example** + * * ```js * const app = dush() * @@ -86,13 +88,14 @@ function dush () { * ``` * * @name .use - * @param {Function} `plugin` A function passed with `(app)` signature - * @return {Object} The `dush` instance for chaining + * @param {Function} `plugin` A function passed with `(app, options)` signature + * @param {Object} `options` optional, passed as second argument to `plugin` function + * @return {Object} self "app" for chaining * @api public */ - use: function use (plugin) { - var ret = plugin(app); + use: function use (plugin, options) { + var ret = plugin(app, options); return ret || app }, @@ -122,7 +125,7 @@ function dush () { * @param {Function} `handler` Function to call in response to given event * @param {Boolean} `once` Make `handler` be called only once, * the `.once` method use this internally - * @return {Object} The `dush` instance for chaining + * @return {Object} self "app" for chaining * @api public */ @@ -172,7 +175,7 @@ function dush () { * @name .once * @param {String} `name` Type of event to listen for, or `'*'` for all events * @param {Function} `handler` Function to call in response to given event - * @return {Object} The `dush` instance for chaining + * @return {Object} self "app" for chaining * @api public */ @@ -212,7 +215,7 @@ function dush () { * @name .off * @param {String} `name` Type of event to listen for, or `'*'` for all events * @param {Function} `handler` Function to call in response to given event - * @return {Object} The `dush` instance for chaining + * @return {Object} self "app" for chaining * @api public */ @@ -258,7 +261,7 @@ function dush () { * @name .emit * @param {String} `name` The name of the event to invoke * @param {any} `args` Any number of arguments of any type of value, passed to each listener - * @return {Object} The `dush` instance for chaining + * @return {Object} self "app" for chaining * @api public */ diff --git a/dist/dush.umd.js b/dist/dush.umd.js index a78837e..0fa6265 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,t){return n(e,t)||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 f59bd13..42e8eb0 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 30f98e1..4cbf440 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,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 +{"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 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} 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 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} 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) => { 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","options","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,EAiCAI,aAAKC,EAAQC,GAEX,MADYD,GAAOF,EAAKG,IACVH,GAiChBI,YAAIC,EAAMC,EAASC,GAGjB,QAASC,KACFA,EAAKC,SACRT,EAAIU,IAAIL,EAAMG,GACdF,EAAQK,MAAML,EAASM,WACvBJ,EAAKC,QAAS,GANlBb,GAAIiB,GAAIb,EAAIH,WAAWQ,KAAUL,EAAIH,WAAWQ,OAU5CS,EAAKP,EAAOC,EAAOF,CAIvB,OAHAQ,GAAGC,eAAiBT,EAAQU,WAE5BH,EAAEI,KAAKH,GACAd,GAmCTO,cAAMF,EAAMC,GAEV,MADAN,GAAII,GAAGC,EAAMC,GAAS,GACfN,GAsCTU,aAAKL,EAAMC,GACT,GAAIA,GAAWN,EAAIH,WAAWQ,GAAO,CACnCa,GAAMC,GAAQb,EAAQU,UACtBhB,GAAIH,WAAWQ,GAAQL,EAAIH,WAAWQ,GAAMe,OAAO,SAACZ,SAASA,GAAKO,iBAAmBI,QAC5Ed,GACTL,EAAIH,WAAWQ,MAEfL,EAAIH,WAAaC,OAAOC,OAAO,KAGjC,OAAOC,IAoCTqB,cAAMhB,GACJ,GAAa,MAATA,EAAc,CAChB,GAAIiB,MAAUC,MAAMC,KAAKZ,YACxBZ,EAAIH,WAAWQ,QAAaoB,IAAI,SAACnB,GAAcA,EAAQK,MAAML,EAASgB,EAAKC,MAAM,OACjFvB,EAAIH,WAAW,UAAY4B,IAAI,SAACnB,GAAcA,EAAQK,MAAML,EAASgB,KAGxE,MAAOtB,IAIX,OAAOA"} \ No newline at end of file diff --git a/src/index.js b/src/index.js index 8c9dcdd..e57cf2c 100644 --- a/src/index.js +++ b/src/index.js @@ -71,6 +71,8 @@ export default function dush () { * or properties to the instance. Useful if you want to make * dush to work with DOM for example. * + * **Example** + * * ```js * const app = dush() * @@ -88,13 +90,14 @@ export default function dush () { * ``` * * @name .use - * @param {Function} `plugin` A function passed with `(app)` signature - * @return {Object} The `dush` instance for chaining + * @param {Function} `plugin` A function passed with `(app, options)` signature + * @param {Object} `options` optional, passed as second argument to `plugin` function + * @return {Object} self "app" for chaining * @api public */ - use (plugin) { - const ret = plugin(app) + use (plugin, options) { + const ret = plugin(app, options) return ret || app }, @@ -124,7 +127,7 @@ export default function dush () { * @param {Function} `handler` Function to call in response to given event * @param {Boolean} `once` Make `handler` be called only once, * the `.once` method use this internally - * @return {Object} The `dush` instance for chaining + * @return {Object} self "app" for chaining * @api public */ @@ -174,7 +177,7 @@ export default function dush () { * @name .once * @param {String} `name` Type of event to listen for, or `'*'` for all events * @param {Function} `handler` Function to call in response to given event - * @return {Object} The `dush` instance for chaining + * @return {Object} self "app" for chaining * @api public */ @@ -214,7 +217,7 @@ export default function dush () { * @name .off * @param {String} `name` Type of event to listen for, or `'*'` for all events * @param {Function} `handler` Function to call in response to given event - * @return {Object} The `dush` instance for chaining + * @return {Object} self "app" for chaining * @api public */ @@ -260,7 +263,7 @@ export default function dush () { * @name .emit * @param {String} `name` The name of the event to invoke * @param {any} `args` Any number of arguments of any type of value, passed to each listener - * @return {Object} The `dush` instance for chaining + * @return {Object} self "app" for chaining * @api public */