What is sparkles?
The sparkles npm package is used for creating and managing a global event namespace for your application, allowing different parts of your application to communicate with each other using events. It is a lightweight event emitter.
What are sparkles's main functionalities?
Event Emission
This feature allows you to emit events globally within your application. Any part of your application that has required sparkles and called it as a function will be able to listen for 'my-event' and react accordingly.
const sparkles = require('sparkles')();
sparkles.emit('my-event', { some: 'data' });
Event Listening
This feature allows you to listen for events that have been emitted globally. In this example, when 'my-event' is emitted, the callback function will be executed, and the passed data will be logged to the console.
const sparkles = require('sparkles')();
sparkles.on('my-event', (data) => {
console.log('Received data:', data);
});
Event Namespacing
Sparkles allows you to create separate event namespaces. This can be useful if you want to avoid event name collisions between different parts of your application.
const sparkles = require('sparkles')('my-namespace');
sparkles.emit('my-event', { some: 'data' });
Other packages similar to sparkles
eventemitter3
EventEmitter3 is a high-performance event emitter. It offers similar functionality to sparkles but with a focus on performance and being a drop-in replacement for existing event emitter implementations.
mitt
Mitt is a tiny functional event emitter / pubsub. It provides a similar event-driven communication approach as sparkles but with a smaller footprint and functional API.
wolfy87-eventemitter
Wolfy87's EventEmitter is an implementation of the EventEmitter module found in Node.js. It provides similar event handling capabilities as sparkles but includes additional features such as wildcard event listeners.
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.
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')();
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