@supercat1337/event-emitter 1.0.0 → 1.0.2
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/dist/{event-emitter.esm.js → event-emitter.1.0.2.esm.js} +8 -17
- package/dist/event-emitter.1.0.2.min.esm.js +1 -0
- package/jsconfig.json +3 -2
- package/package.json +9 -7
- package/scripts/build.js +105 -0
- package/src/EventEmitter.js +3 -22
- package/tests/test.js +34 -2
- package/dist/event-emitter.min.esm.js +0 -1
|
@@ -1,15 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
// src/EventEmitter.js
|
|
2
|
+
var EventEmitter = class {
|
|
3
|
+
/** @type {Object.<string, TListener[]>} */
|
|
4
|
+
events = {};
|
|
2
5
|
/**
|
|
3
|
-
* Creates an instance of EventEmitter.
|
|
4
|
-
*
|
|
5
|
-
* @constructor
|
|
6
|
-
*/
|
|
7
|
-
constructor() {
|
|
8
|
-
this.events = {};
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
*
|
|
12
|
-
* @method
|
|
13
6
|
* @param {string} event
|
|
14
7
|
* @param {TListener} listener
|
|
15
8
|
* @returns {Unsubscriber}
|
|
@@ -26,7 +19,6 @@ class EventEmitter {
|
|
|
26
19
|
return unsubscriber;
|
|
27
20
|
}
|
|
28
21
|
/**
|
|
29
|
-
*
|
|
30
22
|
* @param {string} event
|
|
31
23
|
* @param {TListener} listener
|
|
32
24
|
*/
|
|
@@ -40,7 +32,6 @@ class EventEmitter {
|
|
|
40
32
|
}
|
|
41
33
|
}
|
|
42
34
|
/**
|
|
43
|
-
*
|
|
44
35
|
* @param {string} event
|
|
45
36
|
*/
|
|
46
37
|
emit(event) {
|
|
@@ -59,8 +50,6 @@ class EventEmitter {
|
|
|
59
50
|
}
|
|
60
51
|
}
|
|
61
52
|
/**
|
|
62
|
-
*
|
|
63
|
-
* @method
|
|
64
53
|
* @param {string} event
|
|
65
54
|
* @param {TListener} listener
|
|
66
55
|
* @returns {Unsubscriber}
|
|
@@ -71,5 +60,7 @@ class EventEmitter {
|
|
|
71
60
|
listener.apply(this, arguments);
|
|
72
61
|
});
|
|
73
62
|
}
|
|
74
|
-
}
|
|
75
|
-
export {
|
|
63
|
+
};
|
|
64
|
+
export {
|
|
65
|
+
EventEmitter
|
|
66
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
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/jsconfig.json
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
"compilerOptions": {
|
|
3
3
|
"baseUrl": ".",
|
|
4
4
|
"paths": {
|
|
5
|
-
"src/*": ["./src/*",
|
|
5
|
+
"src/*": ["./src/*"],
|
|
6
|
+
"@/*": ["./node_modules/*"]
|
|
6
7
|
},
|
|
7
8
|
"target": "ESNext",
|
|
8
9
|
"module": "ES6",
|
|
@@ -13,5 +14,5 @@
|
|
|
13
14
|
],
|
|
14
15
|
},
|
|
15
16
|
//Add any build/compiled folders here to stop vscode searching those
|
|
16
|
-
"exclude": ["node_modules", "dist"]
|
|
17
|
+
"exclude": ["node_modules", "dist", "**/node_modules"]
|
|
17
18
|
}
|
package/package.json
CHANGED
|
@@ -1,23 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@supercat1337/event-emitter",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Event Emitter",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "ava",
|
|
8
|
-
"build": "
|
|
9
|
-
"build_esm": "esbuild src/EventEmitter.js --outfile=dist/event-emitter.esm.js",
|
|
10
|
-
"build_min_esm": "esbuild src/EventEmitter.js --minify --outfile=dist/event-emitter.min.esm.js"
|
|
7
|
+
"test": "c8 ava",
|
|
8
|
+
"build": "node scripts/build.js"
|
|
11
9
|
},
|
|
12
10
|
"author": "Supercat",
|
|
13
11
|
"license": "MIT",
|
|
14
12
|
"devDependencies": {
|
|
15
13
|
"ava": "^6.1.2",
|
|
14
|
+
"c8": "^9.1.0",
|
|
16
15
|
"esbuild": "^0.20.2"
|
|
17
16
|
},
|
|
18
17
|
"type": "module",
|
|
19
|
-
"moduleResolution": "
|
|
20
|
-
"keywords": [
|
|
18
|
+
"moduleResolution": "nodenext",
|
|
19
|
+
"keywords": [
|
|
20
|
+
"event emitter",
|
|
21
|
+
"event bus"
|
|
22
|
+
],
|
|
21
23
|
"publishConfig": {
|
|
22
24
|
"registry": "https://registry.npmjs.org"
|
|
23
25
|
},
|
package/scripts/build.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
//import * as esbuild from "@/esbuild/lib/main.js";
|
|
4
|
+
|
|
5
|
+
// @ts-ignore
|
|
6
|
+
import * as esbuild from 'esbuild';
|
|
7
|
+
|
|
8
|
+
import fs from 'fs';
|
|
9
|
+
|
|
10
|
+
import path from "path";
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
var packageInfo = (() => {
|
|
14
|
+
var packageJson = fs.readFileSync("package.json", { encoding: "utf-8" });
|
|
15
|
+
var result = JSON.parse(packageJson);
|
|
16
|
+
return result;
|
|
17
|
+
})();
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Format bytes as human-readable text.
|
|
21
|
+
*
|
|
22
|
+
* @param {number} bytes Number of bytes.
|
|
23
|
+
* @param si True to use metric (SI) units, aka powers of 1000. False to use
|
|
24
|
+
* binary (IEC), aka powers of 1024.
|
|
25
|
+
* @param dp Number of decimal places to display.
|
|
26
|
+
*
|
|
27
|
+
* @return Formatted string.
|
|
28
|
+
*/
|
|
29
|
+
function humanFileSize(bytes, si = false, dp = 1) {
|
|
30
|
+
//const thresh = si ? 1000 : 1024;
|
|
31
|
+
const thresh = 1024;
|
|
32
|
+
|
|
33
|
+
if (Math.abs(bytes) < thresh) {
|
|
34
|
+
return bytes + ' B';
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/*
|
|
38
|
+
const units = si
|
|
39
|
+
? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
|
|
40
|
+
: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
|
|
41
|
+
*/
|
|
42
|
+
const units = ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
|
43
|
+
|
|
44
|
+
let u = -1;
|
|
45
|
+
const r = 10 ** dp;
|
|
46
|
+
|
|
47
|
+
do {
|
|
48
|
+
bytes /= thresh;
|
|
49
|
+
++u;
|
|
50
|
+
} while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1);
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
return bytes.toFixed(dp) + ' ' + units[u];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function main() {
|
|
57
|
+
|
|
58
|
+
const dist_directory = "dist";
|
|
59
|
+
|
|
60
|
+
for (const file of fs.readdirSync(dist_directory)) {
|
|
61
|
+
fs.unlinkSync(path.join(dist_directory, file));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
var filename_1 = `${dist_directory}/event-emitter.${packageInfo.version}.esm.js`;
|
|
65
|
+
|
|
66
|
+
await esbuild.build({
|
|
67
|
+
entryPoints: ['./index.js'],
|
|
68
|
+
bundle: true,
|
|
69
|
+
minify: false,
|
|
70
|
+
outfile: filename_1,
|
|
71
|
+
platform: `neutral`,
|
|
72
|
+
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
var filesize_1 = fs.statSync(filename_1).size;
|
|
76
|
+
|
|
77
|
+
var filename_2 = `${dist_directory}/event-emitter.${packageInfo.version}.min.esm.js`;
|
|
78
|
+
|
|
79
|
+
await esbuild.build({
|
|
80
|
+
entryPoints: ['./index.js'],
|
|
81
|
+
bundle: true,
|
|
82
|
+
minify: true,
|
|
83
|
+
outfile: filename_2,
|
|
84
|
+
platform: `neutral`
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
var filesize_2 = fs.statSync(filename_2).size;
|
|
89
|
+
|
|
90
|
+
console.table({
|
|
91
|
+
1: {
|
|
92
|
+
file: filename_1,
|
|
93
|
+
size: humanFileSize(filesize_1, false, 2)
|
|
94
|
+
},
|
|
95
|
+
2: {
|
|
96
|
+
file: filename_2,
|
|
97
|
+
size: humanFileSize(filesize_2, false, 2)
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
["file", "size"]);
|
|
101
|
+
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
main();
|
|
105
|
+
|
package/src/EventEmitter.js
CHANGED
|
@@ -1,34 +1,19 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
|
|
3
|
-
/** @module EventEmitter */
|
|
4
|
-
|
|
5
3
|
/**
|
|
6
4
|
* @typedef {Function} TListener
|
|
5
|
+
* @typedef {()=>void} Unsubscriber
|
|
7
6
|
*/
|
|
8
7
|
|
|
9
|
-
/** @typedef {()=>void} Unsubscriber */
|
|
10
|
-
|
|
11
8
|
/**
|
|
12
9
|
*
|
|
13
10
|
* @class
|
|
14
11
|
* */
|
|
15
12
|
class EventEmitter {
|
|
16
|
-
/**
|
|
17
|
-
|
|
18
|
-
*
|
|
19
|
-
* @constructor
|
|
20
|
-
*/
|
|
21
|
-
constructor() {
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* @type {Object.<string, TListener[]>}
|
|
25
|
-
*/
|
|
26
|
-
this.events = {};
|
|
27
|
-
}
|
|
13
|
+
/** @type {Object.<string, TListener[]>} */
|
|
14
|
+
events = {};
|
|
28
15
|
|
|
29
16
|
/**
|
|
30
|
-
*
|
|
31
|
-
* @method
|
|
32
17
|
* @param {string} event
|
|
33
18
|
* @param {TListener} listener
|
|
34
19
|
* @returns {Unsubscriber}
|
|
@@ -50,7 +35,6 @@ class EventEmitter {
|
|
|
50
35
|
return unsubscriber;
|
|
51
36
|
}
|
|
52
37
|
/**
|
|
53
|
-
*
|
|
54
38
|
* @param {string} event
|
|
55
39
|
* @param {TListener} listener
|
|
56
40
|
*/
|
|
@@ -67,7 +51,6 @@ class EventEmitter {
|
|
|
67
51
|
|
|
68
52
|
}
|
|
69
53
|
/**
|
|
70
|
-
*
|
|
71
54
|
* @param {string} event
|
|
72
55
|
*/
|
|
73
56
|
emit(event) {
|
|
@@ -92,8 +75,6 @@ class EventEmitter {
|
|
|
92
75
|
}
|
|
93
76
|
|
|
94
77
|
/**
|
|
95
|
-
*
|
|
96
|
-
* @method
|
|
97
78
|
* @param {string} event
|
|
98
79
|
* @param {TListener} listener
|
|
99
80
|
* @returns {Unsubscriber}
|
package/tests/test.js
CHANGED
|
@@ -5,7 +5,6 @@ import { EventEmitter } from "./../index.js";
|
|
|
5
5
|
import test from "./../node_modules/ava/entrypoints/main.mjs";
|
|
6
6
|
|
|
7
7
|
|
|
8
|
-
|
|
9
8
|
test("on(), emit()", t => {
|
|
10
9
|
var ev = new EventEmitter;
|
|
11
10
|
ev.on("foo", () => {
|
|
@@ -61,7 +60,7 @@ test("Call unsubscriber", t => {
|
|
|
61
60
|
var action = () => {
|
|
62
61
|
foo++;
|
|
63
62
|
};
|
|
64
|
-
|
|
63
|
+
|
|
65
64
|
var unsubscriber = ev.on("foo", action);
|
|
66
65
|
|
|
67
66
|
|
|
@@ -75,4 +74,37 @@ test("Call unsubscriber", t => {
|
|
|
75
74
|
} else {
|
|
76
75
|
t.fail()
|
|
77
76
|
}
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
test("on(), emit() with error", t => {
|
|
81
|
+
var ev = new EventEmitter;
|
|
82
|
+
var foo = 0;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
*
|
|
86
|
+
* @param {number} bar
|
|
87
|
+
*/
|
|
88
|
+
function func(bar) {
|
|
89
|
+
if (bar % 2) {
|
|
90
|
+
throw new Error("Custom error")
|
|
91
|
+
} else {
|
|
92
|
+
foo++;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
ev.on("foo", () => {
|
|
97
|
+
func(foo);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
ev.emit("foo");
|
|
101
|
+
ev.emit("foo");
|
|
102
|
+
ev.emit("foo");
|
|
103
|
+
|
|
104
|
+
if (foo == 1) {
|
|
105
|
+
t.pass();
|
|
106
|
+
} else {
|
|
107
|
+
t.fail()
|
|
108
|
+
}
|
|
109
|
+
|
|
78
110
|
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
class o{constructor(){this.events={}}on(t,e){typeof this.events[t]!="object"&&(this.events[t]=[]),this.events[t].push(e);let s=this;return function(){s.removeListener(t,e)}}removeListener(t,e){var s;typeof this.events[t]=="object"&&(s=this.events[t].indexOf(e),s>-1&&this.events[t].splice(s,1))}emit(t){if(typeof this.events[t]=="object"){var e,s,i,r=[].slice.call(arguments,1);for(s=this.events[t].slice(),i=s.length,e=0;e<i;e++)try{s[e].apply(this,r)}catch(n){console.error(t,r),console.error(n)}}}once(t,e){return this.on(t,function s(){this.removeListener(t,s),e.apply(this,arguments)})}}export{o as EventEmitter};
|