Giter Site home page Giter Site logo

sparkles's Introduction

sparkles

NPM version Downloads Build Status Coveralls Status

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.

Note: If you put an event handler in a file in your application, that file must be loaded in via an import somewhere in your application, even if it's not directly being used. Otherwise, it will not be loaded into memory.

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 ๐Ÿ˜„

License

MIT

sparkles's People

Contributors

github-actions[bot] avatar phated avatar rohanbhatia avatar sttk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

sparkles's Issues

Initialization race condition.

Given modules a

var i = require('sparkles')()
var b = require('./b')

i.on('foo', function () { console.log('a') })

i.emit('foo', 'a')

and b:

var i = require('sparkles')()

i.on('foo', function () { console.log('b') })

You end up with references to two different event emitters, and only 'a' gets logged. If in a you add your listener before requiring b, they are the same event emitter and work as expected.

Proliferation of global variables.

By adding each name space to global as it's own variable they all become available in every module by that name. Pretty unexpected behavior, and can return unexpected objects from the global name space.

Better to put them all on a single global object with a hard to mistakenly interact with name space. Something like:

var globalStore = global['sparkles@store']
if (!globalStore) globalStore =  global['sparkles@store'] = {}
// store all event emitters on globalStore

npm publish

hi,
i noticed your module on github has been cleaned up a bit.
any chance you could npm publish and bump the version?
thx

Consider using a symbol

We use a string to attach our global emitter, but it might make more sense to use a symbol if node 10+ supports them

Avoiding global variables

What about just having a singleton store inside this module? This would only be a problem if multiple copies of sparkles are installed in an app.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.