Skip to content

Commit

Permalink
fix(wildcard): should not allow emitting the wildcard
Browse files Browse the repository at this point in the history
because the listeners will be triggered twice

fixes #5
  • Loading branch information
tunnckoCore committed Mar 12, 2017
1 parent a1b2fd8 commit 8bdbe13
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ See [JSBin Example](http://jsbin.com/muqujavolu/edit?js,console).
**Params**

* `name` **{String}**: The name of the event to invoke
* `args` **{any}**: Any number of arguments of any type of value, passed to each listener
* `args` **{any}**: Maximum 3 arguments of any type of value, passed to each listener
* `returns` **{Object}**: The `dush` instance for chaining

**Example**
Expand Down
8 changes: 5 additions & 3 deletions dist/dush.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,16 @@ 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
* @param {any} `args` Maximum 3 arguments of any type of value, passed to each listener
* @return {Object} The `dush` instance for chaining
* @api public
*/

emit: function emit (name, a, b, c) {
(all[name] || []).map(function (handler) { handler(a, b, c); });
(all['*'] || []).map(function (handler) { handler(name, a, b, c); });
if (name !== '*') {
(all[name] || []).map(function (handler) { handler(a, b, c); });
(all['*'] || []).map(function (handler) { handler(name, a, b, c); });
}

return app
}
Expand Down
8 changes: 5 additions & 3 deletions dist/dush.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,16 @@ 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
* @param {any} `args` Maximum 3 arguments of any type of value, passed to each listener
* @return {Object} The `dush` instance for chaining
* @api public
*/

emit: function emit (name, a, b, c) {
(all[name] || []).map(function (handler) { handler(a, b, c); });
(all['*'] || []).map(function (handler) { handler(name, a, b, c); });
if (name !== '*') {
(all[name] || []).map(function (handler) { handler(a, b, c); });
(all['*'] || []).map(function (handler) { handler(name, a, b, c); });
}

return app
}
Expand Down
2 changes: 1 addition & 1 deletion dist/dush.umd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified dist/dush.umd.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/dush.umd.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,16 @@ 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
* @param {any} `args` Maximum 3 arguments of any type of value, passed to each listener
* @return {Object} The `dush` instance for chaining
* @api public
*/

emit (name, a, b, c) {
(all[name] || []).map((handler) => { handler(a, b, c) });
(all['*'] || []).map((handler) => { handler(name, a, b, c) })
if (name !== '*') {
(all[name] || []).map((handler) => { handler(a, b, c) });
(all['*'] || []).map((handler) => { handler(name, a, b, c) })
}

return app
}
Expand Down
21 changes: 21 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,24 @@ test('should return app if .use(plugin) dont', function (done) {
test.strictEqual(app.qux, 'zzz')
done()
})

test('should not allow emitting the wildcard (issue#5)', function (done) {
var emitter = dush()

emitter.on('*', function (name, a, b, c) {
test.strictEqual(name, 'foo')
test.strictEqual(a, 1)
test.strictEqual(b, 2)
test.strictEqual(c, 3)
})
emitter.on('foo', function (a, b, c) {
test.strictEqual(a, 1)
test.strictEqual(b, 2)
test.strictEqual(c, 3)
})

emitter.emit('*', 4, 5, 6)
emitter.emit('foo', 1, 2, 3)
emitter.emit('*', 555)
done()
})

0 comments on commit 8bdbe13

Please sign in to comment.