https://github.com/scriptnull/dead-simple-event-bus
Dead simple implementation of an event bus
https://github.com/scriptnull/dead-simple-event-bus
Last synced: 10 months ago
JSON representation
Dead simple implementation of an event bus
- Host: GitHub
- URL: https://github.com/scriptnull/dead-simple-event-bus
- Owner: scriptnull
- Created: 2015-09-18T12:16:24.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-09-18T17:11:33.000Z (over 10 years ago)
- Last Synced: 2025-03-10T05:35:10.590Z (11 months ago)
- Language: JavaScript
- Size: 137 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Dead simple event bus that can be used in Browsers and Node.js
## Installation
Available via npm
```bash
npm install dead-simple-event-bus
```
## Public API
It as 3 methods.
- add()
- remove()
- trigger()
API can be used in 3 ways , namely
- Single event
- Multiple events with args style
- Multiple events with array style
## Browser Setup
```html
var ebSingle = new window.EventBus();
var ebArgsStyle = new window.EventBus();
var ebArrayStyle = new window.EventBus();
```
## node module
```javascript
var EventBus = require('dead-simple-event-bus').EventBus;
var ebSingle = new EventBus();
var ebArgsStyle = new EventBus();
var ebArrayStyle = new EventBus();
```
### Single event
```javascript
//event Bus with single event
ebSingle.add('a' , function(a , b ){ console.log(a + ' ' + b ); });
ebSingle.trigger('a' , [ 1 , 2 ]);
ebSingle.remove('a' , function(a , b ){ console.log(a + ' ' + b ); } );
```
### Multiple events with args style
```javascript
ebArgsStyle.add('a' , function(a , b ){ console.log(a + ' ' + b ); } , function(a , b ){ console.log( b + ' ' + a ); });
ebArgsStyle.trigger('a' , [ 1 , 2 ]);
ebArgsStyle.remove('a' , function(a , b ){ console.log(a + ' ' + b ); } , function(a , b ){ console.log( b + ' ' + a ); });
```
### Multiple events with array style
```javascript
ebArrayStyle.add('a' ,[ function(a , b ){ console.log(a + ' ' + b ); } , function(a , b ){ console.log( b + ' ' + a ); } ] );
ebArrayStyle.trigger('a' , [ 1 , 2 ]);
ebArrayStyle.remove('a' , [ function(a , b ){ console.log(a + ' ' + b ); } , function(a , b ){ console.log( b + ' ' + a ); }]);
```
### Duplicate functions
__dead-simple-event-bus__ optimizes the event bus by not allowing more than one copy of function to be added.
```javascript
ebSingle.add('a' , function(a , b ){ console.log(a + ' ' + b ); }); // assigns event
ebSingle.add('a' , function(a , b ){ console.log(a + ' ' + b ); }); // avoids assigning and prints message
```
By default , duplicate functions print a message to console and doesn't get addedup again on the event bus. If you want to handle this manually , you could dig a bit into the source and insert a callback.
### Contribution
More than welcomed !
### License
