Skip to content

Commit e5a798a

Browse files
committed
initial commit
0 parents  commit e5a798a

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Event Dispatch
2+
3+
[![Software Version](https://img.shields.io/badge/event--dispatch-v1.0.0-green)](https://github.com/SaboSuke/event-dispatch)
4+
5+
A simple library to emit custom events.
6+
7+
## Methods
8+
9+
Name | Returns | Explanation
10+
---- | ------- | -----------
11+
`dispatch` | `self` | Dispatch a custom event - (name, event).
12+
`on` | `self` | Listen to a specific event - (name, callback).
13+
14+
# Usage
15+
16+
```javascript
17+
18+
let EVENT = new EventDispatch();
19+
EVENT.dispatch('myEvent', 'my data');
20+
21+
EVENT.on('myEvent', function(event){
22+
console.log(event) // 'my data'
23+
})
24+
25+
```
26+
27+
## Contributing
28+
29+
If you have anything to contribute, or functionality that you lack - you are more than welcome to participate in this!
30+
If anyone wishes to contribute unit tests that also would be great.
31+
32+
## License
33+
34+
The MIT License (MIT)
35+
36+
Copyright (c) 2021 Essam Abed (abedissam95@gmail.com)
37+
38+
Permission is hereby granted, free of charge, to any person obtaining a copy
39+
of this software and associated documentation files (the "Software"), to deal
40+
in the Software without restriction, including without limitation the rights
41+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
42+
copies of the Software, and to permit persons to whom the Software is
43+
furnished to do so, subject to the following conditions:
44+
45+
The above copyright notice and this permission notice shall be included in all
46+
copies or substantial portions of the Software.

event-dispatch.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @desc Emits a custom events of your chosing
3+
*
4+
* @return {EventDispatch}
5+
*/
6+
var EventDispatch = function () {
7+
8+
/**
9+
* @desc Dispatch a custom event
10+
* @param {string} name - event name
11+
* @param {Callback Function} event - contains the data passed to the event
12+
*/
13+
this.dispatch = function (name, event) {
14+
let callbacks = _this[name];
15+
if (callbacks) callbacks.forEach(callback => callback(event));
16+
17+
return this;
18+
};
19+
20+
/**
21+
* @desc Listen to a specific event
22+
* @param {string} name - event name
23+
* @param {function} callback - callback function
24+
*/
25+
this.on = function (name, callback) {
26+
let callbacks = this[name];
27+
if (!callbacks) this[name] = [callback];
28+
else callbacks.push(callback);
29+
30+
return this;
31+
};
32+
}
33+
34+
exports.EventDispatch = EventDispatch;

event-dispatch.module.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export default class EventDispatch {
2+
/**
3+
* @desc Emits a custom events of your chosing
4+
*
5+
* @return {EventDispatch}
6+
*/
7+
constructor() { }
8+
9+
dispatch(name, event) {
10+
let callbacks = this[name];
11+
if (callbacks) callbacks.forEach(callback => callback(event));
12+
13+
return this;
14+
};
15+
16+
on(name, callback) {
17+
let callbacks = this[name];
18+
if (!callbacks) this[name] = [callback];
19+
else callbacks.push(callback);
20+
21+
return this;
22+
};
23+
}

package.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "event-dispatch",
3+
"version": "1.0.0",
4+
"description": "A simple library to emit custom events",
5+
"main": "event-dispatch.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/SaboSuke/event-dispatch.git"
12+
},
13+
"keywords": [
14+
"event",
15+
"event-emitter",
16+
"emitter",
17+
"custom-events"
18+
],
19+
"author": "Essam Abed",
20+
"license": "MIT",
21+
"bugs": {
22+
"url": "https://github.com/SaboSuke/event-dispatch/issues"
23+
},
24+
"homepage": "https://github.com/SaboSuke/event-dispatch#readme"
25+
}

0 commit comments

Comments
 (0)