From 7f8b6678fe3ec8449f47eb156b91ce5150ed9348 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Sat, 23 Mar 2024 14:32:09 -0700 Subject: [PATCH] feat: Add module for accessing legacy store (#10) chore: Combine tests into one file --- legacy.js | 48 ++++++++++++++++++ package.json | 3 +- test/exists.js | 16 ------ test/index.js | 46 +++++++++++++++++ test/legacy.js | 123 ++++++++++++++++++++++++++++++++++++++++++++++ test/namespace.js | 40 --------------- 6 files changed, 219 insertions(+), 57 deletions(-) create mode 100644 legacy.js delete mode 100644 test/exists.js create mode 100644 test/legacy.js delete mode 100644 test/namespace.js diff --git a/legacy.js b/legacy.js new file mode 100644 index 0000000..ba5359a --- /dev/null +++ b/legacy.js @@ -0,0 +1,48 @@ +'use strict'; + +var EventEmitter = require('events').EventEmitter; + +var sparklesNamespace = 'store@sparkles'; +var defaultNamespace = 'default' + +function getStore() { + var store = global[sparklesNamespace]; + + if (!store) { + Object.defineProperty(global, sparklesNamespace, { + value: {}, + enumerable: false, + }); + store = global[sparklesNamespace]; + } + + return store; +} + +function getEmitter(namespace) { + var store = getStore(); + + namespace = namespace || defaultNamespace; + + var ee = store[namespace]; + + if (!ee) { + ee = store[namespace] = new EventEmitter(); + ee.setMaxListeners(0); + ee.remove = function remove() { + ee.removeAllListeners(); + delete store[namespace]; + }; + } + + return ee; +} + +function exists(namespace) { + var store = getStore(); + + return !!store[namespace]; +} + +module.exports = getEmitter; +module.exports.exists = exists; diff --git a/package.json b/package.json index 6f4d01f..3ad8112 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ "main": "index.js", "files": [ "LICENSE", - "index.js" + "index.js", + "legacy.js" ], "scripts": { "lint": "eslint .", diff --git a/test/exists.js b/test/exists.js deleted file mode 100644 index 43ca070..0000000 --- a/test/exists.js +++ /dev/null @@ -1,16 +0,0 @@ -'use strict'; - -var expect = require('expect'); - -var sparkles = require('../'); - -describe('sparkles.exists()', function () { - it('checks if a namespace has been defined', function (done) { - expect(sparkles.exists('test')).toBe(false); - var ee = sparkles('test'); - expect(sparkles.exists('test')).toBe(true); - ee.remove(); - expect(sparkles.exists('test')).toBe(false); - done(); - }); -}); diff --git a/test/index.js b/test/index.js index f06c903..a69622a 100644 --- a/test/index.js +++ b/test/index.js @@ -75,3 +75,49 @@ describe('sparkles()', function () { done(); }); }); + +describe('sparkles.exists()', function () { + it('checks if a namespace has been defined', function (done) { + expect(sparkles.exists('test')).toBe(false); + var ee = sparkles('test'); + expect(sparkles.exists('test')).toBe(true); + ee.remove(); + expect(sparkles.exists('test')).toBe(false); + done(); + }); +}); + +describe('namespace', function () { + var storeSymbol = Symbol.for('sparkles:store'); + var namespaceSymbol = Symbol.for('sparkles:namespace'); + + beforeEach(function (done) { + global[storeSymbol] = {}; + done(); + }); + + afterEach(function (done) { + delete global[storeSymbol]; + done(); + }); + + it('should use an EE from sparkles namespace if it already exists', function (done) { + var ee = (global[storeSymbol][namespaceSymbol] = new EventEmitter()); + ee.custom = 'ee'; + + var sparkles = require('../')(); + + expect(sparkles.custom).toEqual('ee'); + done(); + }); + + it('should allow custom namespaces', function (done) { + var ee = (global[storeSymbol].customNamespace = new EventEmitter()); + ee.custom = true; + + var sparkles = require('../')('customNamespace'); + + expect(sparkles.custom).toEqual(true); + done(); + }); +}); diff --git a/test/legacy.js b/test/legacy.js new file mode 100644 index 0000000..85e3fe6 --- /dev/null +++ b/test/legacy.js @@ -0,0 +1,123 @@ +'use strict'; + +var expect = require('expect'); +var EventEmitter = require('events').EventEmitter; + +var sparkles = require('../legacy.js'); + +describe('legacy: sparkles()', function () { + describe('behavior on global', function () { + var ee; + var storeNamespace = 'store@sparkles'; + var defaultNamespace = 'default'; + + beforeEach(function (done) { + ee = sparkles(); + done(); + }); + + afterEach(function (done) { + ee.remove(); + done(); + }); + + it('will attach the sparkles store namespace to global', function (done) { + expect(global[storeNamespace]).toBeTruthy(); + done(); + }); + + it('will attach an event emitter to the sparkles store default namespace', function (done) { + expect(global[storeNamespace][defaultNamespace]).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[storeNamespace][defaultNamespace]).toBeUndefined(); + done(); + }); + + it('does not show up when enumerating the global object', function (done) { + expect(Object.keys(global)).not.toContain(storeNamespace); + done(); + }); + }); + + 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('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('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(); + }); +}); + +describe('legacy: sparkles.exists()', function () { + it('checks if a namespace has been defined', function (done) { + expect(sparkles.exists('test')).toBe(false); + var ee = sparkles('test'); + expect(sparkles.exists('test')).toBe(true); + ee.remove(); + expect(sparkles.exists('test')).toBe(false); + done(); + }); +}); + +describe('legacy: namespace', function () { + var storeNamespace = Symbol.for('sparkles:store'); + var defaultNamespace = Symbol.for('sparkles:namespace'); + + beforeEach(function (done) { + global[storeNamespace] = {}; + done(); + }); + + afterEach(function (done) { + delete global[storeNamespace]; + done(); + }); + + it('should use an EE from sparkles namespace if it already exists', function (done) { + var ee = (global[storeNamespace][defaultNamespace] = new EventEmitter()); + ee.custom = 'ee'; + + var sparkles = require('../')(); + + expect(sparkles.custom).toEqual('ee'); + done(); + }); + + it('should allow custom namespaces', function (done) { + var ee = (global[storeNamespace].customNamespace = new EventEmitter()); + ee.custom = true; + + var sparkles = require('../')('customNamespace'); + + expect(sparkles.custom).toEqual(true); + done(); + }); +}); diff --git a/test/namespace.js b/test/namespace.js deleted file mode 100644 index 3147cc0..0000000 --- a/test/namespace.js +++ /dev/null @@ -1,40 +0,0 @@ -'use strict'; - -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[storeSymbol] = {}; - done(); - }); - - afterEach(function (done) { - delete global[storeSymbol]; - done(); - }); - - it('should use an EE from sparkles namespace if it already exists', function (done) { - var ee = (global[storeSymbol][namespaceSymbol] = new EventEmitter()); - ee.custom = 'ee'; - - var sparkles = require('../')(); - - expect(sparkles.custom).toEqual('ee'); - done(); - }); - - it('should allow custom namespaces', function (done) { - var ee = (global[storeSymbol].customNamespace = new EventEmitter()); - ee.custom = true; - - var sparkles = require('../')('customNamespace'); - - expect(sparkles.custom).toEqual(true); - done(); - }); -});