-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e5a798a
Showing
4 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Event Dispatch | ||
|
||
[![Software Version](https://img.shields.io/badge/event--dispatch-v1.0.0-green)](https://github.com/SaboSuke/event-dispatch) | ||
|
||
A simple library to emit custom events. | ||
|
||
## Methods | ||
|
||
Name | Returns | Explanation | ||
---- | ------- | ----------- | ||
`dispatch` | `self` | Dispatch a custom event - (name, event). | ||
`on` | `self` | Listen to a specific event - (name, callback). | ||
|
||
# Usage | ||
|
||
```javascript | ||
|
||
let EVENT = new EventDispatch(); | ||
EVENT.dispatch('myEvent', 'my data'); | ||
|
||
EVENT.on('myEvent', function(event){ | ||
console.log(event) // 'my data' | ||
}) | ||
|
||
``` | ||
|
||
## Contributing | ||
|
||
If you have anything to contribute, or functionality that you lack - you are more than welcome to participate in this! | ||
If anyone wishes to contribute unit tests that also would be great. | ||
|
||
## License | ||
|
||
The MIT License (MIT) | ||
|
||
Copyright (c) 2021 Essam Abed (abedissam95@gmail.com) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* @desc Emits a custom events of your chosing | ||
* | ||
* @return {EventDispatch} | ||
*/ | ||
var EventDispatch = function () { | ||
|
||
/** | ||
* @desc Dispatch a custom event | ||
* @param {string} name - event name | ||
* @param {Callback Function} event - contains the data passed to the event | ||
*/ | ||
this.dispatch = function (name, event) { | ||
let callbacks = _this[name]; | ||
if (callbacks) callbacks.forEach(callback => callback(event)); | ||
|
||
return this; | ||
}; | ||
|
||
/** | ||
* @desc Listen to a specific event | ||
* @param {string} name - event name | ||
* @param {function} callback - callback function | ||
*/ | ||
this.on = function (name, callback) { | ||
let callbacks = this[name]; | ||
if (!callbacks) this[name] = [callback]; | ||
else callbacks.push(callback); | ||
|
||
return this; | ||
}; | ||
} | ||
|
||
exports.EventDispatch = EventDispatch; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
export default class EventDispatch { | ||
/** | ||
* @desc Emits a custom events of your chosing | ||
* | ||
* @return {EventDispatch} | ||
*/ | ||
constructor() { } | ||
|
||
dispatch(name, event) { | ||
let callbacks = this[name]; | ||
if (callbacks) callbacks.forEach(callback => callback(event)); | ||
|
||
return this; | ||
}; | ||
|
||
on(name, callback) { | ||
let callbacks = this[name]; | ||
if (!callbacks) this[name] = [callback]; | ||
else callbacks.push(callback); | ||
|
||
return this; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "event-dispatch", | ||
"version": "1.0.0", | ||
"description": "A simple library to emit custom events", | ||
"main": "event-dispatch.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/SaboSuke/event-dispatch.git" | ||
}, | ||
"keywords": [ | ||
"event", | ||
"event-emitter", | ||
"emitter", | ||
"custom-events" | ||
], | ||
"author": "Essam Abed", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/SaboSuke/event-dispatch/issues" | ||
}, | ||
"homepage": "https://github.com/SaboSuke/event-dispatch#readme" | ||
} |