root / HServer / 00.Server / 00.Program / node_modules / ultron / README.md
이력 | 보기 | 이력해설 | 다운로드 (3.92 KB)
1 |
# Ultron |
---|---|
2 |
|
3 |
[](http://unshift.io)[](http://browsenpm.org/package/ultron)[](https://travis-ci.org/unshiftio/ultron)[](https://david-dm.org/unshiftio/ultron)[](https://coveralls.io/r/unshiftio/ultron?branch=master)[](http://webchat.freenode.net/?channels=unshift) |
4 |
|
5 |
Ultron is a high-intelligence robot. It gathers intelligence so it can start |
6 |
improving upon his rudimentary design. It will learn your event emitting |
7 |
patterns and find ways to exterminate them. Allowing you to remove only the |
8 |
event emitters that **you** assigned and not the ones that your users or |
9 |
developers assigned. This can prevent race conditions, memory leaks and even file |
10 |
descriptor leaks from ever happening as you won't remove clean up processes. |
11 |
|
12 |
## Installation |
13 |
|
14 |
The module is designed to be used in browsers using browserify and in Node.js. |
15 |
You can install the module through the public npm registry by running the |
16 |
following command in CLI: |
17 |
|
18 |
``` |
19 |
npm install --save ultron |
20 |
``` |
21 |
|
22 |
## Usage |
23 |
|
24 |
In all examples we assume that you've required the library as following: |
25 |
|
26 |
```js |
27 |
'use strict'; |
28 |
|
29 |
var Ultron = require('ultron'); |
30 |
``` |
31 |
|
32 |
Now that we've required the library we can construct our first `Ultron` instance. |
33 |
The constructor requires one argument which should be the `EventEmitter` |
34 |
instance that we need to operate upon. This can be the `EventEmitter` module |
35 |
that ships with Node.js or `EventEmitter3` or anything else as long as it |
36 |
follow the same API and internal structure as these 2. So with that in mind we |
37 |
can create the instance: |
38 |
|
39 |
```js |
40 |
// |
41 |
// For the sake of this example we're going to construct an empty EventEmitter |
42 |
// |
43 |
var EventEmitter = require('events').EventEmitter; // or require('eventmitter3'); |
44 |
var events = new EventEmitter(); |
45 |
|
46 |
var ultron = new Ultron(events); |
47 |
``` |
48 |
|
49 |
You can now use the following API's from the Ultron instance: |
50 |
|
51 |
### Ultron.on |
52 |
|
53 |
Register a new event listener for the given event. It follows the exact same API |
54 |
as `EventEmitter.on` but it will return itself instead of returning the |
55 |
EventEmitter instance. If you are using EventEmitter3 it also supports the |
56 |
context param: |
57 |
|
58 |
```js |
59 |
ultron.on('event-name', handler, { custom: 'function context' }); |
60 |
``` |
61 |
|
62 |
Just like you would expect, it can also be chained together. |
63 |
|
64 |
```js |
65 |
ultron |
66 |
.on('event-name', handler) |
67 |
.on('another event', handler); |
68 |
``` |
69 |
|
70 |
### Ultron.once |
71 |
|
72 |
Exactly the same as the [Ultron.on](#ultronon) but it only allows the execution |
73 |
once. |
74 |
|
75 |
Just like you would expect, it can also be chained together. |
76 |
|
77 |
```js |
78 |
ultron |
79 |
.once('event-name', handler, { custom: 'this value' }) |
80 |
.once('another event', handler); |
81 |
``` |
82 |
|
83 |
### Ultron.remove |
84 |
|
85 |
This is where all the magic happens and the safe removal starts. This function |
86 |
accepts different argument styles: |
87 |
|
88 |
- No arguments, assume that all events need to be removed so it will work as |
89 |
`removeAllListeners()` API. |
90 |
- 1 argument, when it's a string it will be split on ` ` and `,` to create a |
91 |
list of events that need to be cleared. |
92 |
- Multiple arguments, we assume that they are all names of events that need to |
93 |
be cleared. |
94 |
|
95 |
```js |
96 |
ultron.remove('foo, bar baz'); // Removes foo, bar and baz. |
97 |
ultron.remove('foo', 'bar', 'baz'); // Removes foo, bar and baz. |
98 |
ultron.remove(); // Removes everything. |
99 |
``` |
100 |
|
101 |
If you just want to remove a single event listener using a function reference |
102 |
you can still use the EventEmitter's `removeListener(event, fn)` API: |
103 |
|
104 |
```js |
105 |
function foo() {} |
106 |
|
107 |
ultron.on('foo', foo); |
108 |
events.removeListener('foo', foo); |
109 |
``` |
110 |
|
111 |
## License |
112 |
|
113 |
MIT |