event-listener-service
Advanced tools
Comparing version 0.0.3 to 1.0.0
@@ -12,3 +12,2 @@ export interface EventListenerImplementation { | ||
private static registered; | ||
private static emitToLisener(registered); | ||
private static findIndexOf(wanted); | ||
@@ -15,0 +14,0 @@ static useWithoutImplementation(): void; |
14
index.js
@@ -11,8 +11,2 @@ "use strict"; | ||
} | ||
EventListenerService.emitToLisener = function (registered) { | ||
var emit = this; | ||
if (emit.eventName === registered.eventName) { | ||
emit.emitted ? registered.listener(emit.emitted) : registered.listener(); | ||
} | ||
}; | ||
EventListenerService.findIndexOf = function (wanted) { | ||
@@ -56,4 +50,8 @@ var registered = EventListenerService.registered; | ||
EventListenerService.emit = function (eventName, emitted) { | ||
var registered = EventListenerService.registered, emitToLisener = EventListenerService.emitToLisener; | ||
registered.forEach(emitToLisener, { eventName: eventName, emitted: emitted }); | ||
for (var _i = 0, _a = EventListenerService.registered; _i < _a.length; _i++) { | ||
var listener = _a[_i]; | ||
if (listener.eventName === eventName) { | ||
emitted ? listener.listener(emitted) : listener.listener(); | ||
} | ||
} | ||
}; | ||
@@ -60,0 +58,0 @@ EventListenerService.removeListener = function (eventName, listener) { |
{ | ||
"name": "event-listener-service", | ||
"version": "0.0.3", | ||
"description": "Simple helper class to help you stay DRY (Don't repeat yourself)", | ||
"version": "1.0.0", | ||
"description": "Universal Event Litener with testability built in", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "typings": "index.d.ts", |
116
README.md
@@ -10,1 +10,117 @@ # event-listener-service | ||
[![Issue Count](https://codeclimate.com/github/NoHomey/event-listener-service/badges/issue_count.svg)](https://codeclimate.com/github/NoHomey/event-listener-service) | ||
# Install | ||
[![NPM](https://nodei.co/npm/event-listener-service.png?downloads=true&stars=true)](https://nodei.co/npm/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 run typings` | ||
3. `npm test` | ||
# Contributing | ||
1. `npm install` | ||
2. `npm run typings` | ||
3. Make changes | ||
4. If necessary add some tests to `__tests__` | ||
5. `npm test` | ||
6. Make a Pull Request |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
10828
0
126
94