|
Jared K. Smith |
6cd1e85 |
# Ultron
|
|
Jared K. Smith |
6cd1e85 |
|
|
Jared K. Smith |
6cd1e85 |
[![Made by unshift](https://img.shields.io/badge/made%20by-unshift-00ffcc.svg?style=flat-square)](http://unshift.io)[![Version npm](http://img.shields.io/npm/v/ultron.svg?style=flat-square)](http://browsenpm.org/package/ultron)[![Build Status](http://img.shields.io/travis/unshiftio/ultron/master.svg?style=flat-square)](https://travis-ci.org/unshiftio/ultron)[![Dependencies](https://img.shields.io/david/unshiftio/ultron.svg?style=flat-square)](https://david-dm.org/unshiftio/ultron)[![Coverage Status](http://img.shields.io/coveralls/unshiftio/ultron/master.svg?style=flat-square)](https://coveralls.io/r/unshiftio/ultron?branch=master)[![IRC channel](http://img.shields.io/badge/IRC-irc.freenode.net%23unshift-00a8ff.svg?style=flat-square)](http://webchat.freenode.net/?channels=unshift)
|
|
Jared K. Smith |
6cd1e85 |
|
|
Jared K. Smith |
6cd1e85 |
Ultron is a high-intelligence robot. It gathers intelligence so it can start
|
|
Jared K. Smith |
6cd1e85 |
improving upon his rudimentary design. It will learn your event emitting
|
|
Jared K. Smith |
6cd1e85 |
patterns and find ways to exterminate them. Allowing you to remove only the
|
|
Jared K. Smith |
6cd1e85 |
event emitters that **you** assigned and not the ones that your users or
|
|
Jared K. Smith |
6cd1e85 |
developers assigned. This can prevent race conditions, memory leaks and even file
|
|
Jared K. Smith |
6cd1e85 |
descriptor leaks from ever happening as you won't remove clean up processes.
|
|
Jared K. Smith |
6cd1e85 |
|
|
Jared K. Smith |
6cd1e85 |
## Installation
|
|
Jared K. Smith |
6cd1e85 |
|
|
Jared K. Smith |
6cd1e85 |
The module is designed to be used in browsers using browserify and in Node.js.
|
|
Jared K. Smith |
6cd1e85 |
You can install the module through the public npm registry by running the
|
|
Jared K. Smith |
6cd1e85 |
following command in CLI:
|
|
Jared K. Smith |
6cd1e85 |
|
|
Jared K. Smith |
6cd1e85 |
```
|
|
Jared K. Smith |
6cd1e85 |
npm install --save ultron
|
|
Jared K. Smith |
6cd1e85 |
```
|
|
Jared K. Smith |
6cd1e85 |
|
|
Jared K. Smith |
6cd1e85 |
## Usage
|
|
Jared K. Smith |
6cd1e85 |
|
|
Jared K. Smith |
6cd1e85 |
In all examples we assume that you've required the library as following:
|
|
Jared K. Smith |
6cd1e85 |
|
|
Jared K. Smith |
6cd1e85 |
```js
|
|
Jared K. Smith |
6cd1e85 |
'use strict';
|
|
Jared K. Smith |
6cd1e85 |
|
|
Jared K. Smith |
6cd1e85 |
var Ultron = require('ultron');
|
|
Jared K. Smith |
6cd1e85 |
```
|
|
Jared K. Smith |
6cd1e85 |
|
|
Jared K. Smith |
6cd1e85 |
Now that we've required the library we can construct our first `Ultron` instance.
|
|
Jared K. Smith |
6cd1e85 |
The constructor requires one argument which should be the `EventEmitter`
|
|
Jared K. Smith |
6cd1e85 |
instance that we need to operate upon. This can be the `EventEmitter` module
|
|
Jared K. Smith |
6cd1e85 |
that ships with Node.js or `EventEmitter3` or anything else as long as it
|
|
Jared K. Smith |
6cd1e85 |
follow the same API and internal structure as these 2. So with that in mind we
|
|
Jared K. Smith |
6cd1e85 |
can create the instance:
|
|
Jared K. Smith |
6cd1e85 |
|
|
Jared K. Smith |
6cd1e85 |
```js
|
|
Jared K. Smith |
6cd1e85 |
//
|
|
Jared K. Smith |
6cd1e85 |
// For the sake of this example we're going to construct an empty EventEmitter
|
|
Jared K. Smith |
6cd1e85 |
//
|
|
Jared K. Smith |
6cd1e85 |
var EventEmitter = require('events').EventEmitter; // or require('eventmitter3');
|
|
Jared K. Smith |
6cd1e85 |
var events = new EventEmitter();
|
|
Jared K. Smith |
6cd1e85 |
|
|
Jared K. Smith |
6cd1e85 |
var ultron = new Ultron(events);
|
|
Jared K. Smith |
6cd1e85 |
```
|
|
Jared K. Smith |
6cd1e85 |
|
|
Jared K. Smith |
6cd1e85 |
You can now use the following API's from the Ultron instance:
|
|
Jared K. Smith |
6cd1e85 |
|
|
Jared K. Smith |
6cd1e85 |
### Ultron.on
|
|
Jared K. Smith |
6cd1e85 |
|
|
Jared K. Smith |
6cd1e85 |
Register a new event listener for the given event. It follows the exact same API
|
|
Jared K. Smith |
6cd1e85 |
as `EventEmitter.on` but it will return itself instead of returning the
|
|
Jared K. Smith |
6cd1e85 |
EventEmitter instance. If you are using EventEmitter3 it also supports the
|
|
Jared K. Smith |
6cd1e85 |
context param:
|
|
Jared K. Smith |
6cd1e85 |
|
|
Jared K. Smith |
6cd1e85 |
```js
|
|
Jared K. Smith |
6cd1e85 |
ultron.on('event-name', handler, { custom: 'function context' });
|
|
Jared K. Smith |
6cd1e85 |
```
|
|
Jared K. Smith |
6cd1e85 |
|
|
Jared K. Smith |
6cd1e85 |
Just like you would expect, it can also be chained together.
|
|
Jared K. Smith |
6cd1e85 |
|
|
Jared K. Smith |
6cd1e85 |
```js
|
|
Jared K. Smith |
6cd1e85 |
ultron
|
|
Jared K. Smith |
6cd1e85 |
.on('event-name', handler)
|
|
Jared K. Smith |
6cd1e85 |
.on('another event', handler);
|
|
Jared K. Smith |
6cd1e85 |
```
|
|
Jared K. Smith |
6cd1e85 |
|
|
Jared K. Smith |
6cd1e85 |
### Ultron.once
|
|
Jared K. Smith |
6cd1e85 |
|
|
Jared K. Smith |
6cd1e85 |
Exactly the same as the [Ultron.on](#ultronon) but it only allows the execution
|
|
Jared K. Smith |
6cd1e85 |
once.
|
|
Jared K. Smith |
6cd1e85 |
|
|
Jared K. Smith |
6cd1e85 |
Just like you would expect, it can also be chained together.
|
|
Jared K. Smith |
6cd1e85 |
|
|
Jared K. Smith |
6cd1e85 |
```js
|
|
Jared K. Smith |
6cd1e85 |
ultron
|
|
Jared K. Smith |
6cd1e85 |
.once('event-name', handler, { custom: 'this value' })
|
|
Jared K. Smith |
6cd1e85 |
.once('another event', handler);
|
|
Jared K. Smith |
6cd1e85 |
```
|
|
Jared K. Smith |
6cd1e85 |
|
|
Jared K. Smith |
6cd1e85 |
### Ultron.remove
|
|
Jared K. Smith |
6cd1e85 |
|
|
Jared K. Smith |
6cd1e85 |
This is where all the magic happens and the safe removal starts. This function
|
|
Jared K. Smith |
6cd1e85 |
accepts different argument styles:
|
|
Jared K. Smith |
6cd1e85 |
|
|
Jared K. Smith |
6cd1e85 |
- No arguments, assume that all events need to be removed so it will work as
|
|
Jared K. Smith |
6cd1e85 |
`removeAllListeners()` API.
|
|
Jared K. Smith |
6cd1e85 |
- 1 argument, when it's a string it will be split on ` ` and `,` to create a
|
|
Jared K. Smith |
6cd1e85 |
list of events that need to be cleared.
|
|
Jared K. Smith |
6cd1e85 |
- Multiple arguments, we assume that they are all names of events that need to
|
|
Jared K. Smith |
6cd1e85 |
be cleared.
|
|
Jared K. Smith |
6cd1e85 |
|
|
Jared K. Smith |
6cd1e85 |
```js
|
|
Jared K. Smith |
6cd1e85 |
ultron.remove('foo, bar baz'); // Removes foo, bar and baz.
|
|
Jared K. Smith |
6cd1e85 |
ultron.remove('foo', 'bar', 'baz'); // Removes foo, bar and baz.
|
|
Jared K. Smith |
6cd1e85 |
ultron.remove(); // Removes everything.
|
|
Jared K. Smith |
6cd1e85 |
```
|
|
Jared K. Smith |
6cd1e85 |
|
|
Jared K. Smith |
6cd1e85 |
If you just want to remove a single event listener using a function reference
|
|
Jared K. Smith |
6cd1e85 |
you can still use the EventEmitter's `removeListener(event, fn)` API:
|
|
Jared K. Smith |
6cd1e85 |
|
|
Jared K. Smith |
6cd1e85 |
```js
|
|
Jared K. Smith |
6cd1e85 |
function foo() {}
|
|
Jared K. Smith |
6cd1e85 |
|
|
Jared K. Smith |
6cd1e85 |
ultron.on('foo', foo);
|
|
Jared K. Smith |
6cd1e85 |
events.removeListener('foo', foo);
|
|
Jared K. Smith |
6cd1e85 |
```
|
|
Jared K. Smith |
6cd1e85 |
|
|
Jared K. Smith |
6cd1e85 |
## License
|
|
Jared K. Smith |
6cd1e85 |
|
|
Jared K. Smith |
6cd1e85 |
MIT
|