pax_global_header00006660000000000000000000000064125734225020014514gustar00rootroot0000000000000052 comment=66eed55eeac9f3ba641d4643c5ad2ed598bc6a72 sparkles-1.0.0/000077500000000000000000000000001257342250200133365ustar00rootroot00000000000000sparkles-1.0.0/.editorconfig000066400000000000000000000003051257342250200160110ustar00rootroot00000000000000# http://editorconfig.org root = true [*] indent_style = space indent_size = 2 charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.md] trim_trailing_whitespace = false sparkles-1.0.0/.eslintrc000066400000000000000000000000471257342250200151630ustar00rootroot00000000000000{ "extends": "@phated/iceddev/es5" } sparkles-1.0.0/.gitignore000066400000000000000000000011461257342250200153300ustar00rootroot00000000000000# Logs logs *.log # Runtime data pids *.pid *.seed # Directory for instrumented libs generated by jscoverage/JSCover lib-cov # Coverage directory used by tools like istanbul coverage # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) .grunt # Compiled binary addons (http://nodejs.org/api/addons.html) build/Release # Dependency directory # Commenting this out is preferred by some people, see # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- node_modules # Users Environment Variables .lock-wscript # Garbage files .DS_Store sparkles-1.0.0/.travis.yml000066400000000000000000000002301257342250200154420ustar00rootroot00000000000000sudo: false language: node_js node_js: - 'iojs' - '0.12' - '0.10' before_install: - 'npm install -g npm' # update npm before install for scopes sparkles-1.0.0/LICENSE000066400000000000000000000020721257342250200143440ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2014 Blaine Bublitz Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. sparkles-1.0.0/README.md000066400000000000000000000016741257342250200146250ustar00rootroot00000000000000sparkles ======== [![Build Status](https://travis-ci.org/phated/sparkles.svg?branch=master)](https://travis-ci.org/phated/sparkles) Namespaced global event emitter ## Usage Sparkles exports a function that returns a singleton `EventEmitter`. This EE can be shared across your application, whether or not node loads multiple copies. ```js var sparkles = require('sparkles')(); // make sure to call the function sparkles.on('my-event', function(evt){ console.log('my-event handled', evt); }); sparkles.emit('my-event', { my: 'event' }); ``` ## API ### sparkles(namespace) Returns an EventEmitter that is shared amongst the provided namespace. If no namespace is provided, returns a default EventEmitter. ### sparkles.exists(namespace); Checks whether a namespace exists and returns true or false. ## Why the name? This is a "global emitter"; shortened: "glitter" but it was already taken; so we got sparkles instead :smile: ## License MIT sparkles-1.0.0/index.js000066400000000000000000000014221257342250200150020ustar00rootroot00000000000000'use strict'; var EventEmitter = require('events').EventEmitter; var sparklesNamespace = 'store@sparkles'; var defaultNamespace = 'default'; function getStore(){ var store = global[sparklesNamespace]; if(!store){ 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; sparkles-1.0.0/package.json000066400000000000000000000013651257342250200156310ustar00rootroot00000000000000{ "name": "sparkles", "version": "1.0.0", "description": "Namespaced global event emitter", "author": "Blaine Bublitz (http://iceddev.com/)", "contributors": [], "repository": "phated/sparkles", "license": "MIT", "engines": { "node": ">= 0.10" }, "main": "index.js", "files": [ "LICENSE", "index.js" ], "scripts": { "test": "lab -cvL --ignore store@sparkles" }, "dependencies": {}, "devDependencies": { "@phated/eslint-config-iceddev": "^0.2.1", "code": "^1.5.0", "eslint": "^1.3.1", "eslint-plugin-mocha": "^0.5.1", "eslint-plugin-react": "^3.3.1", "lab": "^5.16.0" }, "keywords": [ "ee", "emitter", "events", "global", "namespaced" ] } sparkles-1.0.0/test/000077500000000000000000000000001257342250200143155ustar00rootroot00000000000000sparkles-1.0.0/test/exists.js000066400000000000000000000007741257342250200162020ustar00rootroot00000000000000'use strict'; var lab = exports.lab = require('lab').script(); var describe = lab.describe; var it = lab.it; var expect = require('code').expect; var sparkles = require('../'); describe('sparkles.exists()', function(){ it('checks if a namespace has been defined', function(done){ expect(sparkles.exists('test')).to.be.false(); var ee = sparkles('test'); expect(sparkles.exists('test')).to.be.true(); ee.remove(); expect(sparkles.exists('test')).to.be.false(); done(); }); }); sparkles-1.0.0/test/index.js000066400000000000000000000017151257342250200157660ustar00rootroot00000000000000'use strict'; var lab = exports.lab = require('lab').script(); var describe = lab.describe; var it = lab.it; var beforeEach = lab.beforeEach; var afterEach = lab.afterEach; var expect = require('code').expect; var sparkles = require('../'); function noop(){} describe('sparkles()', function(){ var ee; 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['store@sparkles']).to.exist(); done(); }); it('will attach an event emitter to the sparkles store default namespace', function(done){ expect(global['store@sparkles']).to.include('default'); 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']).to.not.include('default'); done(); }); }); sparkles-1.0.0/test/namespace.js000066400000000000000000000017731257342250200166170ustar00rootroot00000000000000'use strict'; var lab = exports.lab = require('lab').script(); var describe = lab.describe; var it = lab.it; var beforeEach = lab.beforeEach; var afterEach = lab.afterEach; var expect = require('code').expect; var EventEmitter = require('events').EventEmitter; describe('namespace', function(){ beforeEach(function(done){ global['store@sparkles'] = {}; done(); }); afterEach(function(done){ delete global['store@sparkles']; done(); }); it('should use an EE from sparkles namespace if it already exists', function(done){ var ee = global['store@sparkles'].default = new EventEmitter(); ee.custom = 'ee'; var sparkles = require('../')(); expect(sparkles.custom).to.equal('ee'); done(); }); it('should allow custom namespaces', function(done){ var ee = global['store@sparkles'].customNamespace = new EventEmitter(); ee.custom = true; var sparkles = require('../')('customNamespace'); expect(sparkles.custom).to.equal(true); done(); }); });