Skip to content

Commit

Permalink
New: Allow custom namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed May 14, 2018
1 parent 2bfd37f commit ce23391
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

var EventEmitter = require('events').EventEmitter;

var namespace = '__sparklesEventEmitter';
var defaultNamespace = '__sparklesEventEmitter';

function getEmitter(namespace){

namespace = namespace || defaultNamespace;

function getEmitter(){
var ee;

if(global[namespace]){
Expand Down
22 changes: 14 additions & 8 deletions test/exists.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,25 @@ var EventEmitter = require('events').EventEmitter;

describe('namespace', function(){

var ee;
var sparkles;

before(function(done){
ee = global.__sparklesEventEmitter = new EventEmitter();
it('should use an EE from sparkles namespace if it already exists', function(done){
var ee = global.__sparklesEventEmitter = new EventEmitter();
ee.custom = 'ee';

sparkles = require('../')();
var sparkles = require('../')();

expect(sparkles.custom).to.equal('ee');
sparkles.removeAllListeners();
expect(global.__sparklesEventEmitter).to.not.exist();
done();
});

it('should use an EE from sparkles namespace if it already exists', function(done){
expect(sparkles.custom).to.equal('ee');
it('should allow custom namespaces', function(done){
var ee = global.__myCustomNamespace = new EventEmitter();
ee.custom = true;

var sparkles = require('../')('__myCustomNamespace');

expect(sparkles.custom).to.equal(true);
sparkles.removeAllListeners();
expect(global.__sparklesEventEmitter).to.not.exist();
done();
Expand Down

0 comments on commit ce23391

Please sign in to comment.