Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce size to 115 B. #18

Merged
merged 1 commit into from
Oct 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Simple and tiny event emitter library for JavaScript.

* No Node.js [EventEmitter] compatibility.
* Only 118 bytes (minified and gzipped). It uses [Size Limit] to control size.
* Only 115 bytes (minified and gzipped). It uses [Size Limit] to control size.
* `on` method returns `unbind` function. You don’t need to save
callback to variable for `removeListener`.
* No aliases, just `emit` and `on` methods.
Expand Down
49 changes: 25 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,31 @@
}
).prototype = {

/**
* Calls each of the listeners registered for a given event.
*
* @param {string} event The event name.
* @param {...*} arguments The arguments for listeners.
*
* @return {undefined}
*
* @example
* ee.emit('tick', tickType, tickDuration)
*
* @alias NanoEvents#emit
* @method
*/
emit: function emit (event) {
// event variable is reused and repurposed, now it's an array of handlers
event = this.events[event]
if (!event || !event[0]) return // event[0] === Array.isArray(event)

var args = event.slice.call(arguments, 1)
event.slice().map(function (i) {
i.apply(this, args) // this === global or window
})
},

/**
* Add a listener for a given event.
*
Expand Down Expand Up @@ -68,29 +93,5 @@
// -1 >>> 0 === 0xFFFFFFFF, max possible array length
event.splice(event.indexOf(cb) >>> 0, 1)
}
},

/**
* Calls each of the listeners registered for a given event.
*
* @param {string} event The event name.
* @param {...*} arguments The arguments for listeners.
*
* @return {undefined}
*
* @example
* ee.emit('tick', tickType, tickDuration)
*
* @alias NanoEvents#emit
* @method
*/
emit: function emit (event) {
var list = this.events[event]
if (!list || !list[0]) return // list[0] === Array.isArray(list)

var args = list.slice.call(arguments, 1)
list.slice().map(function (i) {
i.apply(this, args) // this === global or window
})
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"size-limit": [
{
"path": "index.js",
"limit": "118 B"
"limit": "115 B"
}
],
"jest": {
Expand Down