Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hughsk/prototype-emitter
Define EventEmitter listeners on a class instead of each individual instance
https://github.com/hughsk/prototype-emitter
Last synced: 12 days ago
JSON representation
Define EventEmitter listeners on a class instead of each individual instance
- Host: GitHub
- URL: https://github.com/hughsk/prototype-emitter
- Owner: hughsk
- License: mit
- Created: 2014-03-18T14:43:56.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2014-03-19T01:47:32.000Z (over 10 years ago)
- Last Synced: 2024-10-17T16:40:11.398Z (22 days ago)
- Language: JavaScript
- Size: 117 KB
- Stars: 6
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# prototype-emitter [![Flattr this!](https://api.flattr.com/button/flattr-badge-large.png)](https://flattr.com/submit/auto?user_id=hughskennedy&url=http://github.com/hughsk/prototype-emitter&title=prototype-emitter&description=hughsk/prototype-emitter%20on%20GitHub&language=en_GB&tags=flattr,github,javascript&category=software)[![experimental](http://hughsk.github.io/stability-badges/dist/experimental.svg)](http://github.com/hughsk/stability-badges) #
Define [EventEmitter](https://github.com/Gozala/events) listeners on a class
to save on memory consumption in exchange for a little bit of extra work
emitting the events themselves.Suitable when you've got a lot of mostly similar EventEmitters, and you're not
emitting enough events for it to be a performance bottleneck.See also: [bindle](http://github.com/hughsk/bindle)
## Usage ##
[![prototype-emitter](https://nodei.co/npm/prototype-emitter.png?mini=true)](https://nodei.co/npm/prototype-emitter)
### Emitter([BaseClass])
Returns a new `prototype-emitter` class. Optionally you can pass a `BaseClass`
in as the first argument to mixin `prototype-emitter` functionality.Once created or mixed in, you can add event listeners directly on the
class before (or after) creating instances of it. These listeners will
be available on each instance, so calling `ee.emit` will trigger these
listeners too.``` javascript
var PrototypeEmitter = require('prototype-emitter')
var CustomEmitter = PrototypeEmitter()CustomEmitter.once('data', function() {
console.log('data recieved: ')
})CustomEmitter.on('data', function(data) {
console.log('>>', data)
})var emitter = new CustomEmitter
emitter.on('data', function(data) {
console.log(' >', data)
})emitter.emit('data', 1)
emitter.emit('data', 2)
emitter.emit('data', 3)// data recieved:
// >> 1
// > 1
// >> 2
// > 2
// >> 3
// > 3
```## License ##
MIT. See [LICENSE.md](http://github.com/hughsk/prototype-emitter/blob/master/LICENSE.md) for details.