Skip to content

Commit

Permalink
feat!: Use a Symbol for attaching the store to the global (#9)
Browse files Browse the repository at this point in the history
feat!: Use a Symbol for attaching the default namespace to the store
feat: Use Symbol.for so other applications can create the same Symbol

Co-authored-by: Blaine Bublitz <blaine.bublitz@gmail.com>
  • Loading branch information
sttk and phated committed Feb 1, 2022
1 parent 7bbd64f commit 2196fb1
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 24 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

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

var sparklesNamespace = 'store@sparkles';
var defaultNamespace = 'default';
var sparklesNamespace = Symbol.for('sparkles:store');
var defaultNamespace = Symbol.for('sparkles:namespace');

function getStore() {
var store = global[sparklesNamespace];
Expand Down
75 changes: 57 additions & 18 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,77 @@
'use strict';

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

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

function noop() {}

describe('sparkles()', function () {
var ee;
describe('behavior on global', function () {
var ee;
var storeSymbol = Symbol.for('sparkles:store');
var namespaceSymbol = Symbol.for('sparkles:namespace');

beforeEach(function (done) {
ee = sparkles();
done();
});
beforeEach(function (done) {
ee = sparkles();
done();
});

afterEach(function (done) {
ee.remove();
done();
afterEach(function (done) {
ee.remove();
done();
});

it('will attach the sparkles store namespace to global', function (done) {
expect(global[storeSymbol]).toBeTruthy();
done();
});

it('will attach an event emitter to the sparkles store default namespace', function (done) {
expect(global[storeSymbol][namespaceSymbol]).toBeInstanceOf(EventEmitter);
done();
});

it('removes the event emitter from the store when remove is called', function (done) {
ee.on('test', function () { });
ee.remove();
expect(global[storeSymbol][namespaceSymbol]).toBeUndefined();
done();
});

it('does not show up when enumerating the global object', function (done) {
expect(Object.keys(global)).not.toContain(storeSymbol);
done();
});
});

it('will attach the sparkles store namespace to global', function (done) {
expect(global['store@sparkles']).toBeTruthy();
it('should get the default emitter if namespace is not specified', function (done) {
var ee = sparkles();
expect(ee).toBeInstanceOf(EventEmitter);

expect(sparkles()).toBe(ee);
done();
});

it('will attach an event emitter to the sparkles store default namespace', function (done) {
expect(global['store@sparkles']).toHaveProperty('default');
it('should get an emitter for a specified namespace', function (done) {
var ee = sparkles('ns1');
expect(ee).toBeInstanceOf(EventEmitter);

expect(sparkles()).not.toBe(ee);
expect(sparkles('ns1')).toBe(ee);
expect(sparkles('ns2')).not.toBe(ee);
done();
});

it('removes the event emitter from the store when remove is called', function (done) {
ee.on('test', noop);
ee.remove();
expect(global['store@sparkles']).not.toHaveProperty('default');
it('should remove and re-create emitter in the store', function (done) {
var ee0 = sparkles();
var ee1 = sparkles('ns1');

ee0.remove();
expect(sparkles()).not.toBe(ee0);
expect(sparkles('ns1')).toBe(ee1);

ee1.remove();
expect(sparkles('ns1')).not.toBe(ee1);
done();
});
});
11 changes: 7 additions & 4 deletions test/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ var expect = require('expect');
var EventEmitter = require('events').EventEmitter;

describe('namespace', function () {
var storeSymbol = Symbol.for('sparkles:store');
var namespaceSymbol = Symbol.for('sparkles:namespace');

beforeEach(function (done) {
global['store@sparkles'] = {};
global[storeSymbol] = {};
done();
});

afterEach(function (done) {
delete global['store@sparkles'];
delete global[storeSymbol];
done();
});

it('should use an EE from sparkles namespace if it already exists', function (done) {
var ee = (global['store@sparkles'].default = new EventEmitter());
var ee = (global[storeSymbol][namespaceSymbol] = new EventEmitter());
ee.custom = 'ee';

var sparkles = require('../')();
Expand All @@ -26,7 +29,7 @@ describe('namespace', function () {
});

it('should allow custom namespaces', function (done) {
var ee = (global['store@sparkles'].customNamespace = new EventEmitter());
var ee = (global[storeSymbol].customNamespace = new EventEmitter());
ee.custom = true;

var sparkles = require('../')('customNamespace');
Expand Down

0 comments on commit 2196fb1

Please sign in to comment.