Skip to content

Commit 6ebe2c1

Browse files
committed
Initial commit
0 parents  commit 6ebe2c1

File tree

8 files changed

+327
-0
lines changed

8 files changed

+327
-0
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*.log
2+
3+
node_modules/
4+
local/
5+
/tmp
6+
/public
7+
.Trash-1000/
8+
.DS_Store
9+
Thumbs.db

.jshintrc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"node": true,
3+
"browser": true,
4+
"esnext": true,
5+
"bitwise": true,
6+
"camelcase": false,
7+
"curly": true,
8+
"immed": true,
9+
"indent": 2,
10+
"latedef": true,
11+
"newcap": true,
12+
"noarg": true,
13+
"quotmark": "single",
14+
"regexp": true,
15+
"undef": true,
16+
"unused": true,
17+
"strict": true,
18+
"trailing": true,
19+
"smarttabs": true,
20+
"proto": true
21+
}

LICENSE

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
noder-eventemitter2 - https://github.com/noder-io/noder-eventemitter2
2+
3+
The MIT License (MIT)
4+
5+
Copyright (c) 2014 Nicolas Tallefourtane <dev@nicolab.net>
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# noder-eventemitter2
2+
3+
[![Actual version published on NPM](https://badge.fury.io/js/noder-eventemitter2.png)](https://www.npmjs.org/package/noder-eventemitter2)
4+
5+
Plugin for [Noder.io](http://noder.io) that provides the module [eventemitter2](https://github.com/asyncly/EventEmitter2) in the API (`noder` object) and/or `$di` container.
6+
7+
8+
## Install
9+
10+
```sh
11+
npm install --save noder-eventemitter2
12+
```
13+
14+
15+
## Usage
16+
17+
```js
18+
noder.use('noder-eventemitter2')
19+
```
20+
21+
To add `eventemitter2` in your API
22+
```js
23+
noder.$di.get('EventEmitterFactory');
24+
25+
// now, your API is an event emitter
26+
noder.emit('event')
27+
```
28+
29+
To add `eventemitter2` in the `$di` container
30+
```js
31+
noder.$di.get('EventEmitterDiFactory');
32+
33+
// now, your $di container is an event emitter
34+
noder.$di.emit('event')
35+
```
36+
37+
See the doc of [eventemitter2](https://github.com/asyncly/EventEmitter2) :blue_book:
38+
39+
40+
## Unit Tests
41+
42+
This plugin [is fully tested](https://github.com/noder-io/noder-eventemitter2/tree/master/test/src) with [Unit.js](http://unitjs.com) and Mocha.
43+
44+
45+
## License
46+
47+
[MIT](https://github.com/noder-io/noder-eventemitter2/blob/master/LICENSE) (c) 2013, Nicolas Tallefourtane.
48+
49+
50+
## Author
51+
52+
| [![Nicolas Tallefourtane - Nicolab.net](http://www.gravatar.com/avatar/d7dd0f4769f3aa48a3ecb308f0b457fc?s=64)](http://nicolab.net) |
53+
|---|
54+
| [Nicolas Talle](http://nicolab.net) |
55+
| [![Make a donation via Paypal](https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=PGRH4ZXP36GUC) |
56+

package.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "noder-eventemitter2",
3+
"version": "1.0.2",
4+
"description": "Plugin for Noder.io that provides the module 'eventemitter2' in the API and/or $di container.",
5+
"main": "src/index.js",
6+
"keywords": [
7+
"noder-plugin",
8+
"event",
9+
"eventemitter"
10+
],
11+
"author": {
12+
"name": "Nicolas Tallefourtane",
13+
"url": "http://nicolab.net"
14+
},
15+
"licenses": [
16+
{
17+
"type": "MIT",
18+
"url": "https://github.com/noder-io/noder-eventemitter2/blob/master/LICENSE"
19+
}
20+
],
21+
"repository": {
22+
"type": "git",
23+
"url": "git://github.com/noder-io/noder-eventemitter2.git"
24+
},
25+
"bugs": "https://github.com/noder-io/noder-eventemitter2/issues",
26+
"dependencies": {
27+
"eventemitter2": "^0.4.14"
28+
},
29+
"devDependencies": {
30+
"noder.io": "^1.0.2",
31+
"mocha": "^1.21.5",
32+
"unit.js": ">= 1.0.2 <= 2.0.0"
33+
},
34+
"scripts": {
35+
"test": "mocha test"
36+
}
37+
}

src/index.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* This file is part of `noder-eventemitter2`.
3+
*
4+
* (c) Nicolas Tallefourtane <dev@nicolab.net>
5+
*
6+
* For the full copyright and license information, please view
7+
* the LICENSE file that was distributed with this source code
8+
* or visit https://github.com/noder-io/noder-eventemitter2.
9+
*
10+
* @author Nicolas Tallefourtane <dev@nicolab.net>
11+
*/
12+
13+
'use strict';
14+
15+
var EventEmitter = require('eventemitter2').EventEmitter2;
16+
var utils = require('noder.io/src/utils');
17+
var api;
18+
19+
20+
/**
21+
* Init config
22+
*/
23+
function initConfig() {
24+
25+
api.$di.merge({
26+
config: {
27+
eventEmitter: {
28+
29+
// use wildcards.
30+
wildcard: true,
31+
32+
// the delimiter used to segment namespaces, defaults to `.`.
33+
delimiter: '.',
34+
35+
// if you want to emit the newListener event set to true.
36+
newListener: false,
37+
38+
// max listeners that can be assigned to an event, default 10.
39+
maxListeners: 10
40+
}
41+
}
42+
});
43+
}
44+
45+
/**
46+
* Init `noder-eventemitter2` plugin.
47+
* @param {Noder} noder `Noder` instance
48+
* @return {Noder} Current `Noder` instance
49+
*/
50+
module.exports.__noder = function EventEmitter2Plugin(noder) {
51+
52+
var ev;
53+
54+
api = noder;
55+
initConfig();
56+
57+
var getEv = function getEv() {
58+
59+
if(!ev) {
60+
ev = new EventEmitter(api.$di.get('config').eventEmitter);
61+
}
62+
63+
return ev;
64+
};
65+
66+
// add in `noder` object
67+
api.$factory('EventEmitterFactory', function() {
68+
utils.merge(api, getEv());
69+
});
70+
71+
// add in `noder.$di` object
72+
api.$factory('EventEmitterDiFactory', function() {
73+
utils.merge(api.$di, getEv());
74+
});
75+
76+
return api;
77+
};

test/mocha.opts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--reporter spec
2+
--recursive
3+
--growl

test/src/index.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* This file is part of `noder-eventemitter2`.
3+
*
4+
* (c) Nicolas Tallefourtane <dev@nicolab.net>
5+
*
6+
* For the full copyright and license information, please view
7+
* the LICENSE file that was distributed with this source code
8+
* or visit https://github.com/noder-io/noder-eventemitter2.
9+
*
10+
* @author Nicolas Tallefourtane <dev@nicolab.net>
11+
*/
12+
13+
'use strict';
14+
15+
var test = require('unit.js');
16+
var plugin = require('../../src/index');
17+
var EventEmitter2 = require('eventemitter2').EventEmitter2;
18+
19+
20+
describe('noder-eventemitter2', function() {
21+
22+
it('should be added in `noder` with `EventEmitterFactory`', function() {
23+
24+
var noder = require('noder.io').createNoder();
25+
26+
test
27+
.when('add the plugin', function() {
28+
29+
test.object(noder.use(plugin))
30+
.isInstanceOf(noder.Noder);
31+
})
32+
33+
.then('handle the plugin', function() {
34+
35+
test
36+
.bool(noder.$di.has('EventEmitterFactory'))
37+
.isTrue()
38+
39+
.object(noder)
40+
.notContains(new EventEmitter2(noder.$di.get('config').eventEmitter))
41+
42+
.given('Load in noder', noder.$di.get('EventEmitterFactory'))
43+
44+
.case('use the plugin', function() {
45+
46+
var spy = test.spy();
47+
48+
test.object(noder)
49+
.contains(new EventEmitter2(noder.$di.get('config').eventEmitter));
50+
51+
noder.on('test', spy);
52+
noder.emit('test', 'a');
53+
54+
test.assert(spy.calledOnce);
55+
test.assert(spy.calledWith('a'));
56+
})
57+
;
58+
})
59+
;
60+
});
61+
62+
it('should be added in `$di` with `EventEmitterDiFactory`', function() {
63+
64+
var noder = require('noder.io').createNoder();
65+
66+
test
67+
.when('add the plugin', function() {
68+
69+
test.object(noder.use(plugin))
70+
.isInstanceOf(noder.Noder);
71+
})
72+
73+
.then('handle the plugin', function() {
74+
75+
test
76+
.bool(noder.$di.has('EventEmitterDiFactory'))
77+
.isTrue()
78+
79+
.object(noder.$di)
80+
.notContains(new EventEmitter2(noder.$di.get('config').eventEmitter))
81+
82+
.given('Load in noder', noder.$di.get('EventEmitterDiFactory'))
83+
84+
.case('use the plugin', function() {
85+
86+
var spy = test.spy();
87+
88+
test.object(noder.$di)
89+
.contains(new EventEmitter2(noder.$di.get('config').eventEmitter));
90+
91+
noder.$di.on('test', spy);
92+
noder.$di.emit('test', 'a');
93+
94+
test.assert(spy.calledOnce);
95+
test.assert(spy.calledWith('a'));
96+
})
97+
;
98+
})
99+
;
100+
});
101+
});

0 commit comments

Comments
 (0)