Skip to content

Commit

Permalink
Fix: emit with arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoborus committed Apr 15, 2016
1 parent 30e7f83 commit 1c255ff
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
@@ -1,7 +1,7 @@
arbitrary-emitter
=================

High performance event emitter with Map/Set sugar for browser and node.js apps
Event emitter with Map/Set sugar for node.js and browsers (<400 bytes when gzipped)

[![Build Status](https://travis-ci.org/jacoborus/arbitrary-emitter.svg?branch=master)](https://travis-ci.org/jacoborus/arbitrary-emitter) [![npm version](https://badge.fury.io/js/arbitrary-emitter.svg)](https://www.npmjs.com/package/arbitrary-emitter)

Expand Down
27 changes: 22 additions & 5 deletions arbitrary-emitter.js
Expand Up @@ -37,11 +37,28 @@ function arbitrary () {
emit (key) {
const link = links.get(key)
if (!link) return
if (arguments.length > 1) {
let args = arguments.slice(1)
link.forEach(fn => apply.call(fn, args))
} else {
link.forEach(fn => fn())
const l = arguments.length
switch (l) {
case 1: {
link.forEach(fn => fn())
break
}
case 2: {
link.forEach(fn => fn(arguments[1]))
break
}
case 3: {
link.forEach(fn => fn(arguments[1], arguments[2]))
break
}
default: {
let l = arguments.length
let args = new Array(l - 1)
for (let i = 1; i < l; ++i) {
args[i - 1] = arguments[i]
}
link.forEach(fn => apply.call(fn, fn, args))
}
}
},

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "arbitrary-emitter",
"version": "0.7.0",
"version": "0.7.1",
"description": "Event emitter with Map/Set sugar",
"main": "arbitrary-emitter.js",
"scripts": {
Expand Down
24 changes: 23 additions & 1 deletion tests/tests.js
Expand Up @@ -64,7 +64,7 @@ test('off action', t => {
t.end()
})

test('emit with arguments', t => {
test('emit actions in order', t => {
const emitter = ae()
const obj = {}
let control = ''
Expand All @@ -84,3 +84,25 @@ test('emit with arguments', t => {

t.end()
})

test('emit with arguments', t => {
const emitter = ae()
const obj = {}
let control = {
a: 0,
b: 0,
c: 0
}
emitter.on(obj, (a, b, c) => {
control.a = a
control.b = b
control.c = c
})

emitter.emit(obj, 1, 2, 3)
t.is(control.a, 1)
t.is(control.b, 2)
t.is(control.c, 3)

t.end()
})

0 comments on commit 1c255ff

Please sign in to comment.