Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nohomey/event-listener-service
Universal Event Listener with testability built in
https://github.com/nohomey/event-listener-service
event-emiting eventlistener javascript nodejs test-driven-development testability
Last synced: 28 days ago
JSON representation
Universal Event Listener with testability built in
- Host: GitHub
- URL: https://github.com/nohomey/event-listener-service
- Owner: NoHomey
- License: mit
- Created: 2016-09-14T06:56:41.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-11-17T15:53:45.000Z (over 4 years ago)
- Last Synced: 2024-04-25T04:02:34.564Z (10 months ago)
- Topics: event-emiting, eventlistener, javascript, nodejs, test-driven-development, testability
- Language: TypeScript
- Homepage:
- Size: 13.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# event-listener-service
Universal Event Listener service with testability built in[](https://badge.fury.io/js/event-listener-service)
[](https://github.com/NoHomey/event-listener-service)
[](https://semaphoreci.com/nohomey/event-listener-service)
[](https://codeclimate.com/github/NoHomey/event-listener-service)
[](https://codeclimate.com/github/NoHomey/event-listener-service/coverage)
[](https://codeclimate.com/github/NoHomey/event-listener-service)

# Install
Install with npm:
```bash
$ npm install event-listener-service
```# Api
## public static useWithoutImplementation(): void
Declare usage without internal event registering implementation
## public static setImplementation(implementation: EventListenerImplementation): void
Set internal event registering implementation
## public static addListener(eventName: string, listener: (event?: any) => void, ...additional: any[]): void
Add EventListener
## public static emit(eventName: string, emitted?: any): void
Emit event using built in event emiting
## public static removeListener(eventName: string, listener: (event?: any) => void, ...additional: any[]): void
Remove EventListener
# Usage
## As stand alone EventListener
```javascript
import EventListenerService from 'event-listener-service';// Declare usage without internal event registering implementation
EventListenerService.useWithoutImplementation();function onSomeEvent(event) {
console.log('some-event', event);
}// Adding EventLisner
EventListenerService.addListener('some-event', onSomeEvent);// Emiting Events
EventListenerService.emit('some-event', { rand: 9 });// Removing EventLisnter
EventListenerService.removeListener('some-event', onSomeEvent);
```## For listening events on DOM Nodes in the Browser
```javascript
import EventListenerService from 'event-listener-service';// Seting internal event registering implementation
EventListenerService.setImplementation({
addListener: window.addEventListener.bind(window),
removeListener: window.removeEventListener.bind(window)
});function onResize() {
/* ... */
}// Adding EventLisner
EventListenerService.addListener('resize', onResize, false);// Removing EventLisnter
EventListenerService.removeListener('resize', onResize, false);// Optionaly you can manually trigger event listeners in your tests or when you need to fully simulate event
EventListenerService.emit('resize', /* [event object] */);
```## As universal EventListener API
```javascript
// Set internal implementation for registering event listeners
EventListenerService.setImplementation({
addListener: /* (eventName: string, listener: (event?: any) => void, ...additional: any[]) => void */
removeListener: /* (eventName: string, listener: (event?: any) => void, ...additional: any[]) => void */
});// And than use addListener and removeListner
EventListnerService.addListener(eventName: string, listener: (event?: any) => void, ...additional: any[]);
EventListnerService.removeListener(eventName: string, listener: (event?: any) => void, ...additional: any[]);
```# Testing
1. `npm install`
2. `npm test`
# Contributing
1. `npm install`
2. Make changes
3. If necessary add some tests to `__tests__`
4. `npm test`
5. Make a Pull Request