https://github.com/degjs/eventaggregator
Publish and subscribe to events across modules.
https://github.com/degjs/eventaggregator
events
Last synced: 9 months ago
JSON representation
Publish and subscribe to events across modules.
- Host: GitHub
- URL: https://github.com/degjs/eventaggregator
- Owner: DEGJS
- Created: 2015-08-21T20:36:33.000Z (almost 11 years ago)
- Default Branch: main
- Last Pushed: 2022-12-07T22:04:51.000Z (over 3 years ago)
- Last Synced: 2025-09-07T08:59:38.145Z (9 months ago)
- Topics: events
- Language: JavaScript
- Homepage:
- Size: 941 KB
- Stars: 0
- Watchers: 12
- Forks: 0
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# eventAggregator

EventAggregator is a module that acts as a single source of events for objects that wish to publish and/or subscribe to events. This module was heavily inspired by Eric Elliot and his excellent book [Programming JavaScript Applications](http://chimera.labs.oreilly.com/books/1234000000262/). Go read it.
## Install
Install eventAggregator from NPM with command:
```
$ npm install @degjs/event-aggregator
```
## Usage
EventAggregator is a singleton, so it does not need to be instantiated.
```js
import eventAggregator from "@degjs/event-aggregator";
function onSomeEvent(e) {
console.log(e);
}
/* Subscribe to 'someEvent' event */
eventAggregator.subscribe('someEvent', onSomeEvent);
/* Unsubscribe from 'someEvent' event */
eventAggregator.unsubscribe('someEvent', onSomeEvent);
/* Publish 'yetAnotherEvent' event */
eventAggregator.publish({
/* type property is required */
type: 'yetAnotherEvent',
data: {...}
});
```
## Methods
### .subscribe(eventType, listener)
Subscribe to an event.
#### eventType
Type: `String`
The name of the event to subscribe to.
#### listener
Type: `Function`
The listener function that will be called when the event is fired.
### .unsubscribe(eventType, listener)
Unsubscribe from an event.
#### eventType
Type: `String`
The name of the event to unsubscribe from.
#### listener
Type: `Function`
The listener function that was subscribed to the event.
### .publish(evt)
Publish an event.
#### evt
Type: `Object` or `String`
The event to publish. If the parameter is a `String`, it should be the name of the event. If the parameter is an `Object`, it must have a `type` property with a value that is the name of the event. Any other properties on the object will be passed along to all subscribers of the event.
## Browser Support
Breakpoints depends on the following browser APIs:
+ [Array.indexOf](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf)
+ [Array.forEach](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach)
+ [Array.isArray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray)
To support legacy browsers, you'll need to include polyfills for the above APIs.