Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
SaboSuke committed Sep 6, 2021
0 parents commit e5a798a
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
46 changes: 46 additions & 0 deletions README.md
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.
34 changes: 34 additions & 0 deletions event-dispatch.js
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;
23 changes: 23 additions & 0 deletions event-dispatch.module.js
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;
};
}
25 changes: 25 additions & 0 deletions package.json
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"
}

0 comments on commit e5a798a

Please sign in to comment.