https://github.com/marcbachmann/muemit
An EventEmitter implementation for the browser that imitates the nodejs EventEmitter api.
https://github.com/marcbachmann/muemit
Last synced: about 1 year ago
JSON representation
An EventEmitter implementation for the browser that imitates the nodejs EventEmitter api.
- Host: GitHub
- URL: https://github.com/marcbachmann/muemit
- Owner: marcbachmann
- Created: 2015-08-07T14:53:52.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2023-12-15T08:16:37.000Z (over 2 years ago)
- Last Synced: 2025-03-11T01:54:28.513Z (over 1 year ago)
- Language: JavaScript
- Size: 49.8 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# muemit
[](https://greenkeeper.io/)
An tiny EventEmitter implementation that imitates the node.js EventEmitter API.
Meant to run in the browser - at a size of 816bytes (minified & gzipped).
## API
### Initialization
```
var EventEmitter = require('muemit')
var emitter = new EventEmitter()
```
### emitter.addListener(event, listener)
### emitter.on(event, listener)
Adds a listener to the end of the listeners array for the specified event. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of event and listener will result in the listener being added multiple times.
```
var something = new EventEmitter()
something.on('data', function (data) {
console.log("Here's our data object");
});
getSomeData(function(err, data) {
if (err) return something.emit('error', err)
something.emit('data', data)
})
```
This method emits an `addListener` event.
### emitter.once(event, listener)
Adds a one time listener for the event. This listener is invoked only the next time the event is fired, after which it is removed.
```
var something = new EventEmitter()
something.once('data', function () {
console.log("Here's our first data object!");
});
getSomeData(function(data, err) {
something.emit('data')
})
```
### emitter.emit(event[, arg1][, arg2][, ...])
Execute each of the listeners in order with the supplied arguments.
Returns true if event had listeners, false otherwise.
### emitter.removeListener(event, listener)
Remove a listener from the listener array for the specified event. Caution: changes array indices in the listener array behind the listener.
This method emits an `removeListener` event.
### emitter.removeAllListeners([event])
Removes all listeners, or those of the specified event.
It sends a `removeListener` event for every listener that got removed.
Returns emitter, so calls can be chained.
### emitter.listeners(event)
Returns an array with all listeners of a specific event.
### EventEmitter.listenerCount(emitter, event)
Return the number of listeners for a given event.