Skip to content

Commit 42cb621

Browse files
authored
Merge pull request #8 from shystruk/dev
initCustomEvent is deprecated
2 parents 7b66284 + fb3b0bb commit 42cb621

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
[![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://opensource.org/licenses/mit-license.php) [![npm version](https://badge.fury.io/js/custom-event-js.svg)](https://badge.fury.io/js/custom-event-js)
33
[![Known Vulnerabilities](https://snyk.io/test/github/shystruk/custom-event-js/badge.svg?targetFile=package.json)](https://snyk.io/test/github/shystruk/custom-event-js?targetFile=package.json)
44

5-
The Custom Event Dispatcher provides the ability to communicate inside your application by dispatching events and listening to them. It also runs polyfill for Internet Explorer 9 and higher. What is the CustomEvent interface you may find [here](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent).
5+
The Custom Event Dispatcher provides the ability to communicate inside your application by dispatching events and listening to them. What is the CustomEvent interface you may find [here](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent).
66

7-
> **Custom Event Dispatcher works in all popular browsers, including Internet Explorer 9 and higher.**
7+
> **Custom Event Dispatcher works in all popular browsers**
88
99
## Install ##
1010
#### npm
@@ -27,14 +27,19 @@ CustomEvent.dispatch('SHOW_NAME', { name: 'GitHub' })
2727

2828
// Remove event listener
2929
CustomEvent.off('SHOW_NAME')
30+
31+
// Remove a specific callback from event listener
32+
CustomEvent.off('SHOW_NAME', callback)
3033
```
3134

3235
## API
3336
- **on(eventName, callback)** add an appropriate event listener. When event gets fired callback will be called with **detail** argument
3437

3538
- **dispatch(eventName, detail)** dispatch event to all event listeners
3639

37-
- **off(eventName)** remove event listener
40+
- **off(eventName)** remove all event listeners for the event
41+
42+
- **off(eventName, callback)** remove a specific event listener for the event
3843

3944

4045
## Contributing

src/custom-event.js

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// Internet Explorer 9 and higher
2-
_CustomEventPolyfill();
3-
41
const TARGET = document;
52
const EVENTS = {};
63

@@ -16,24 +13,6 @@ function _dispatchEvent(eventName, detail) {
1613
TARGET.dispatchEvent(event);
1714
}
1815

19-
function _CustomEventPolyfill() {
20-
if (typeof window.CustomEvent === 'function') {
21-
return;
22-
}
23-
24-
function CustomEvent(event, params) {
25-
const evt = document.createEvent('CustomEvent');
26-
27-
params = params || { bubbles: false, cancelable: false, detail: undefined };
28-
evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail);
29-
30-
return evt;
31-
}
32-
33-
CustomEvent.prototype = window.Event.prototype;
34-
window.CustomEvent = CustomEvent;
35-
}
36-
3716
module.exports = {
3817
/**
3918
* @param {String} eventName
@@ -53,17 +32,33 @@ module.exports = {
5332

5433
/**
5534
* @param {String} eventName
35+
* @param {Function} callback
5636
*/
57-
off: function (eventName) {
37+
off: function (eventName, callback) {
5838
if (!EVENTS[eventName]) {
5939
return;
6040
}
6141

62-
EVENTS[eventName].callbacks.forEach(callback => {
63-
TARGET.removeEventListener(eventName, callback);
64-
});
42+
const callbacks = EVENTS[eventName].callbacks;
43+
44+
if (callback) {
45+
for (let i = callbacks.length - 1; i >= 0; i--) {
46+
if (callbacks[i] === callback) {
47+
TARGET.removeEventListener(eventName, callbacks[i]);
48+
callbacks.splice(i, 1);
49+
}
50+
}
6551

66-
delete EVENTS[eventName];
52+
if (callbacks.length === 0) {
53+
delete EVENTS[eventName];
54+
}
55+
} else {
56+
callbacks.forEach(callback => {
57+
TARGET.removeEventListener(eventName, callback);
58+
});
59+
60+
delete EVENTS[eventName];
61+
}
6762
},
6863

6964
/**

0 commit comments

Comments
 (0)