@supercat1337/event-emitter 1.0.3 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# event-emitter
|
|
2
|
+
|
|
3
|
+
## Environment agnostic event emitter
|
|
4
|
+
|
|
5
|
+
Installation
|
|
6
|
+
```
|
|
7
|
+
$ npm install @supercat1337/event-emitter
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
Usage
|
|
11
|
+
|
|
12
|
+
Basic example
|
|
13
|
+
```js
|
|
14
|
+
import { EventEmitter } from "@supercat1337/event-emitter/src/EventEmitter.js";
|
|
15
|
+
|
|
16
|
+
var ev = new EventEmitter();
|
|
17
|
+
ev.on("foo", () => {
|
|
18
|
+
console.log("Hello!");
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
ev.emit("bar");
|
|
22
|
+
ev.emit("foo");
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Example of unsubscribing from an event
|
|
26
|
+
```js
|
|
27
|
+
import { EventEmitter } from "@supercat1337/event-emitter/src/EventEmitter.js";
|
|
28
|
+
|
|
29
|
+
var ev = new EventEmitter;
|
|
30
|
+
var foo = 0
|
|
31
|
+
var action = () => {
|
|
32
|
+
foo++;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
var unsubscriber = ev.on("foo", action);
|
|
36
|
+
|
|
37
|
+
ev.emit("foo");
|
|
38
|
+
unsubscriber();
|
|
39
|
+
|
|
40
|
+
ev.emit("foo");
|
|
41
|
+
|
|
42
|
+
if (foo == 1) {
|
|
43
|
+
console.log("Success!");
|
|
44
|
+
} else {
|
|
45
|
+
console.log("Fail!");
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Example of using the Once method
|
|
50
|
+
```js
|
|
51
|
+
import { EventEmitter } from "@supercat1337/event-emitter/src/EventEmitter.js";
|
|
52
|
+
|
|
53
|
+
var ev = new EventEmitter;
|
|
54
|
+
var foo = 0
|
|
55
|
+
ev.once("foo", () => {
|
|
56
|
+
foo++;
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
ev.emit("foo");
|
|
60
|
+
ev.emit("foo");
|
|
61
|
+
ev.emit("foo");
|
|
62
|
+
|
|
63
|
+
if (foo == 1) {
|
|
64
|
+
console.log("Success!");
|
|
65
|
+
} else {
|
|
66
|
+
console.log("Fail!");
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
// version 1.0.
|
|
1
|
+
// version 1.0.5
|
|
2
2
|
|
|
3
3
|
// src/EventEmitter.js
|
|
4
4
|
var EventEmitter = class {
|
|
5
5
|
/** @type {Object.<string, Function[]>} */
|
|
6
6
|
events = {};
|
|
7
7
|
/**
|
|
8
|
+
* on is used to add a callback function that's going to be executed when the event is triggered
|
|
8
9
|
* @param {string} event
|
|
9
10
|
* @param {Function} listener
|
|
10
11
|
* @returns {()=>void}
|
|
@@ -21,6 +22,7 @@ var EventEmitter = class {
|
|
|
21
22
|
return unsubscriber;
|
|
22
23
|
}
|
|
23
24
|
/**
|
|
25
|
+
* Remove an event listener from an event
|
|
24
26
|
* @param {string} event
|
|
25
27
|
* @param {Function} listener
|
|
26
28
|
*/
|
|
@@ -34,6 +36,7 @@ var EventEmitter = class {
|
|
|
34
36
|
}
|
|
35
37
|
}
|
|
36
38
|
/**
|
|
39
|
+
* emit is used to trigger an event
|
|
37
40
|
* @param {string} event
|
|
38
41
|
*/
|
|
39
42
|
emit(event) {
|
|
@@ -52,6 +55,7 @@ var EventEmitter = class {
|
|
|
52
55
|
}
|
|
53
56
|
}
|
|
54
57
|
/**
|
|
58
|
+
* Add a one-time listener
|
|
55
59
|
* @param {string} event
|
|
56
60
|
* @param {Function} listener
|
|
57
61
|
* @returns {()=>void}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
// version 1.0.
|
|
1
|
+
// version 1.0.5
|
|
2
2
|
var i=class{events={};on(e,s){typeof this.events[e]!="object"&&(this.events[e]=[]),this.events[e].push(s);let t=this;return function(){t.removeListener(e,s)}}removeListener(e,s){var t;typeof this.events[e]=="object"&&(t=this.events[e].indexOf(s),t>-1&&this.events[e].splice(t,1))}emit(e){if(typeof this.events[e]=="object"){var s,t,r,o=[].slice.call(arguments,1);for(t=this.events[e].slice(),r=t.length,s=0;s<r;s++)try{t[s].apply(this,o)}catch(n){console.error(e,o),console.error(n)}}}once(e,s){return this.on(e,function t(){this.removeListener(e,t),s.apply(this,arguments)})}};export{i as EventEmitter};
|
package/package.json
CHANGED
package/src/EventEmitter.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
// @ts-check
|
|
2
|
+
/** @module EventEmitter */
|
|
2
3
|
|
|
3
4
|
class EventEmitter {
|
|
4
5
|
/** @type {Object.<string, Function[]>} */
|
|
5
6
|
events = {};
|
|
6
7
|
|
|
7
8
|
/**
|
|
9
|
+
* on is used to add a callback function that's going to be executed when the event is triggered
|
|
8
10
|
* @param {string} event
|
|
9
11
|
* @param {Function} listener
|
|
10
12
|
* @returns {()=>void}
|
|
@@ -26,6 +28,7 @@ class EventEmitter {
|
|
|
26
28
|
return unsubscriber;
|
|
27
29
|
}
|
|
28
30
|
/**
|
|
31
|
+
* Remove an event listener from an event
|
|
29
32
|
* @param {string} event
|
|
30
33
|
* @param {Function} listener
|
|
31
34
|
*/
|
|
@@ -42,6 +45,7 @@ class EventEmitter {
|
|
|
42
45
|
|
|
43
46
|
}
|
|
44
47
|
/**
|
|
48
|
+
* emit is used to trigger an event
|
|
45
49
|
* @param {string} event
|
|
46
50
|
*/
|
|
47
51
|
emit(event) {
|
|
@@ -66,6 +70,7 @@ class EventEmitter {
|
|
|
66
70
|
}
|
|
67
71
|
|
|
68
72
|
/**
|
|
73
|
+
* Add a one-time listener
|
|
69
74
|
* @param {string} event
|
|
70
75
|
* @param {Function} listener
|
|
71
76
|
* @returns {()=>void}
|