Skip to content

Commit cafdcc7

Browse files
author
Guille Paz
committed
Initial commit.
1 parent 2653624 commit cafdcc7

File tree

8 files changed

+440
-1
lines changed

8 files changed

+440
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.DS_Store
2+
node_modules
3+
dist

.jshintrc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"bitwise": false,
3+
"curly": true,
4+
"eqeqeq": true,
5+
"expr": true,
6+
"forin": true,
7+
"freeze": true,
8+
"funcscope": true,
9+
"globalstrict": true,
10+
"nonbsp": true,
11+
"unused": true,
12+
"debug": true,
13+
"evil": true,
14+
"lastsemic": true,
15+
"loopfunc": true,
16+
"proto": true,
17+
"scripturl": true,
18+
"strict": true,
19+
"browser": true,
20+
"browserify": true,
21+
"node": true,
22+
"esnext": true,
23+
"validthis": true
24+
}

CONTRIBUTING.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Contributing
2+
3+
## Code style
4+
Regarding code style like indentation and whitespace, **follow the conventions you see used in the source already.**
5+
6+
## Modifying the code
7+
8+
Please don't edit `/dist/emitter.js` and `/dist/emitter.min.js` files. You'll find source code in the `index.js` file.
9+
10+
1. Fork and clone the repo.
11+
2. Run `npm install` to install all dependencies.
12+
3. Create a new branch, please don't work in your `master` branch directly.
13+
4. Code!
14+
15+
### Running the tests
16+
17+
- Run `npm test` from your command line and check the console.
18+
19+
## Pull requests
20+
21+
1. Create a new branch, **please don't work in your `master` branch directly**.
22+
2. Code!
23+
3. Update the tests and run `npm test` to see the tests.
24+
4. Run `npm run hint`.
25+
5. Run `npm run dist` to build a new version.
26+
6. Update the documentation and package to reflect any changes.
27+
7. Push to your fork and submit a pull request.

README.md

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,103 @@
11
# emitter
2-
A JavaScript event emitter written in ES6.
2+
3+
> A JavaScript Emitter written in ES6.
4+
5+
## Usage
6+
```js
7+
import Emitter from 'Emitter';
8+
9+
let bus = new Emitter();
10+
11+
bus.emit('finish');
12+
13+
```
14+
15+
```js
16+
import Emitter from 'Emitter';
17+
18+
class Foo extends Emitter {
19+
constructor() {
20+
// Code here!
21+
}
22+
23+
...
24+
};
25+
26+
let foo = new Foo();
27+
28+
foo.emit('finish');
29+
```
30+
31+
## API
32+
33+
### Emitter#on(event, listener)
34+
Adds a `listener` to the collection for a specified `event`.
35+
- `event` - The name of the event you want to add.
36+
- `listener` - Listener you want to add from given event.
37+
38+
```js
39+
emitter.on('live', listener);
40+
```
41+
42+
### Emitter#once(event, listener)
43+
Adds a one time `listener` to the collection for a specified `event`. It will execute only once.
44+
- `event` - The name of the event.
45+
- `listener` - Listener you want to add from the given event.
46+
47+
```js
48+
emitter.once('live', listener);
49+
```
50+
51+
### Emitter#off(event, listener)
52+
Removes a `listener` from the collection for a specified `event`.
53+
- `event` - The name of the event.
54+
- `listener` - Listener you want to remove from the given event.
55+
56+
```js
57+
emitter.off('live', listener);
58+
```
59+
60+
### Emitter#removeAllListeners(event)
61+
Removes all `listeners` from the collection for a specified `event`.
62+
- `event` - The name of the event you want to remove.
63+
64+
```js
65+
emitter.removeAllListeners('live');
66+
```
67+
68+
### Emitter#listeners(event)
69+
Returns all `listeners` from the collection for a specified `event`.
70+
- `event` - The name of the event.
71+
72+
```js
73+
emitter.listeners('live');
74+
```
75+
76+
### Emitter#emit(event, [arg1], [arg2], [...])
77+
Execute each of the `listeners` collection in order with the given parameters.
78+
- `event` - The name of the event you want to emit.
79+
- `[args, ...]` - The given arguments.
80+
81+
```js
82+
emitter.emit('live', 'data1', 'data2');
83+
```
84+
85+
## npm-scripts
86+
```
87+
$ npm run build
88+
```
89+
90+
```
91+
$ npm run dist
92+
```
93+
94+
```
95+
$ npm test
96+
```
97+
98+
```
99+
$ npm run hint
100+
```
101+
102+
## License
103+
MIT license. Copyright © 2015 [Mango](http://getmango.com).

browserify.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
var fs = require('fs');
4+
var browserify = require('browserify');
5+
var babelify = require('babelify');
6+
7+
if (!fs.existsSync('./dist')) {
8+
fs.mkdirSync('./dist');
9+
}
10+
11+
browserify({'debug': true, 'standalone': 'Emitter'})
12+
.transform(babelify)
13+
.require('./index.js', {'entry': true})
14+
.bundle()
15+
.on('error', function (err) { console.log('Error : ' + err.message); })
16+
.pipe(fs.createWriteStream('dist/build.js'));

index.js

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
'use strict';
2+
3+
/**
4+
* Creates a new instance of Emitter.
5+
* @class
6+
* @returns {Object} Returns a new instance of Emitter.
7+
* @example
8+
* // Creates a new instance of Emitter.
9+
* var Emitter = require('emitter');
10+
*
11+
* var emitter = new Emitter();
12+
*/
13+
class Emitter {
14+
15+
/**
16+
* Adds a listener to the collection for the specified event.
17+
* @memberof! Emitter.prototype
18+
* @function
19+
* @param {String} event - The event name.
20+
* @param {Function} listener - A listener function to add.
21+
* @returns {Object} Returns an instance of Emitter.
22+
* @example
23+
* // Add an event listener to "foo" event.
24+
* emitter.on('foo', listener);
25+
*/
26+
on(event, listener) {
27+
// Use the current collection or create it.
28+
this._eventCollection = this._eventCollection || {};
29+
30+
// Use the current collection of an event or create it.
31+
this._eventCollection[event] = this._eventCollection[event] || [];
32+
33+
// Appends the listener into the collection of the given event
34+
this._eventCollection[event].push(listener);
35+
36+
return this;
37+
}
38+
39+
/**
40+
* Adds a listener to the collection for the specified event that will be called only once.
41+
* @memberof! Emitter.prototype
42+
* @function
43+
* @param {String} event - The event name.
44+
* @param {Function} listener - A listener function to add.
45+
* @returns {Object} Returns an instance of Emitter.
46+
* @example
47+
* // Will add an event handler to "foo" event once.
48+
* emitter.once('foo', listener);
49+
*/
50+
once(event, listener) {
51+
const self = this;
52+
53+
function fn() {
54+
self.off(event, fn);
55+
listener.apply(this, arguments);
56+
}
57+
58+
fn.listener = listener;
59+
60+
this.on(event, fn);
61+
62+
return this;
63+
}
64+
65+
/**
66+
* Removes a listener from the collection for the specified event.
67+
* @memberof! Emitter.prototype
68+
* @function
69+
* @param {String} event - The event name.
70+
* @param {Function} listener - A listener function to remove.
71+
* @returns {Object} Returns an instance of Emitter.
72+
* @example
73+
* // Remove a given listener.
74+
* emitter.off('foo', listener);
75+
*/
76+
off(event, listener) {
77+
78+
let listeners;
79+
80+
// Defines listeners value.
81+
if (!this._eventCollection || !(listeners = this._eventCollection[event])) {
82+
return this;
83+
}
84+
85+
listeners.forEach(function(fn, i) {
86+
if (fn === listener || fn.listener === listener) {
87+
// Removes the given listener.
88+
listeners.splice(i, 1);
89+
return;
90+
}
91+
});
92+
93+
// Removes an empty event collection.
94+
if (listeners.length === 0) {
95+
delete this._eventCollection[event];
96+
}
97+
98+
return this;
99+
}
100+
101+
/**
102+
* Execute each item in the listener collection in order with the specified data.
103+
* @memberof! Emitter.prototype
104+
* @function
105+
* @param {String} event - The name of the event you want to emit.
106+
* @param {...Object} data - Data to pass to the listeners.
107+
* @returns {Object} Returns an instance of Emitter.
108+
* @example
109+
* // Emits the "foo" event with 'param1' and 'param2' as arguments.
110+
* emitter.emit('foo', 'param1', 'param2');
111+
*/
112+
emit(event, ...args) {
113+
let listeners;
114+
115+
// Defines listeners value.
116+
if (!this._eventCollection || !(listeners = this._eventCollection[event])) {
117+
return this;
118+
}
119+
120+
// Clone listeners
121+
listeners = listeners.slice(0);
122+
123+
listeners.forEach(fn => fn.apply(this, args));
124+
125+
return this;
126+
}
127+
128+
}
129+
130+
/**
131+
* Exports Emitter
132+
*/
133+
export default Emitter;

package.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "emitter",
3+
"repository": "git@github.com:mango/emitter.git",
4+
"description": "A JavaScript event emitter written in ES6.",
5+
"author": "Guille Paz <guille87paz@gmail.com>",
6+
"version": "0.0.1",
7+
"scripts": {
8+
"build": "node browserify.js",
9+
"test": "npm run build && node_modules/.bin/_mocha",
10+
"dist": "node browserify.js && uglifyjs ./dist/emitter.js -m -o ./dist/emitter.min.js",
11+
"hint": "jshint index.js"
12+
},
13+
"dependencies": {},
14+
"devDependencies": {
15+
"better-assert": "1.0.2",
16+
"browserify": "7.0.3",
17+
"mocha": "2.1.0",
18+
"uglify-js": "2.4.15",
19+
"jshint": "2.6.3",
20+
"babelify": "^5.0.4",
21+
"babel": "^4.7.13"
22+
},
23+
"main": "index.js",
24+
"keywords": [
25+
"emitter"
26+
],
27+
"license": "MIT"
28+
}

0 commit comments

Comments
 (0)