@irrelon/emitter 5.0.6 → 6.0.1
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/.eslintignore +2 -1
- package/.prettierignore +2 -1
- package/.prettierrc.yml +18 -1
- package/dist/cjs/src/Emitter.d.ts +71 -20
- package/dist/cjs/src/Emitter.js +128 -78
- package/dist/cjs/tests/Emitter.test.js +125 -52
- package/dist/cjs/tsconfig-cjs.tsbuildinfo +1 -1
- package/dist/{mjs → esm}/src/Emitter.d.ts +71 -20
- package/dist/{mjs → esm}/src/Emitter.js +128 -67
- package/dist/{mjs → esm}/tests/Emitter.test.js +123 -50
- package/dist/esm/tsconfig-esm.tsbuildinfo +1 -0
- package/fixup.sh +1 -1
- package/package.json +8 -7
- package/readme.md +101 -20
- package/src/Emitter.ts +287 -114
- package/tests/Emitter.test.ts +249 -82
- package/tsconfig-cjs.json +1 -1
- package/tsconfig-esm.json +8 -0
- package/tsconfig.json +33 -5
- package/fixPaths.cjs +0 -183
- package/tsconfig-base.json +0 -36
- /package/dist/{mjs → esm}/package.json +0 -0
- /package/dist/{mjs → esm}/tests/Emitter.test.d.ts +0 -0
- /package/dist/{mjs → esm}/tsconfig.tsbuildinfo +0 -0
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"author": "Rob Evans - Irrelon Software Limited",
|
|
3
3
|
"name": "@irrelon/emitter",
|
|
4
|
-
"description": "Simple JavaScript event emitter.",
|
|
5
|
-
"version": "
|
|
4
|
+
"description": "Simple JavaScript event emitter with some powerful features.",
|
|
5
|
+
"version": "6.0.1",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"javascript",
|
|
8
8
|
"browser",
|
|
@@ -39,15 +39,16 @@
|
|
|
39
39
|
"minimatch": "^7.4.2",
|
|
40
40
|
"prettier": "^2.7.1",
|
|
41
41
|
"ts-jest": "^29.0.2",
|
|
42
|
-
"typescript": "^
|
|
42
|
+
"typescript": "^5.3.2",
|
|
43
|
+
"@trivago/prettier-plugin-sort-imports": "^4.3.0"
|
|
43
44
|
},
|
|
44
45
|
"engines": {
|
|
45
46
|
"node": "*"
|
|
46
47
|
},
|
|
47
48
|
"scripts": {
|
|
48
|
-
"build": "tsc -p tsconfig.json && tsc -p tsconfig-cjs.json && sh ./fixup.sh",
|
|
49
|
+
"build": "tsc -p tsconfig-esm.json && tsc -p tsconfig-cjs.json && sh ./fixup.sh",
|
|
49
50
|
"build-watch": "tsc --watch",
|
|
50
|
-
"fixPaths": "
|
|
51
|
+
"fixPaths": "npx @irrelon/fixPaths --write",
|
|
51
52
|
"test": "NODE_OPTIONS=--experimental-vm-modules npx jest --testMatch \"**/*.test.js\"",
|
|
52
53
|
"test-watch": "NODE_OPTIONS=--experimental-vm-modules npx jest --testMatch \"**/*.test.js\" --watchAll"
|
|
53
54
|
},
|
|
@@ -56,10 +57,10 @@
|
|
|
56
57
|
"url": "git://github.com/irrelon/emitter.git"
|
|
57
58
|
},
|
|
58
59
|
"main": "dist/cjs/src/Emitter.js",
|
|
59
|
-
"module": "dist/
|
|
60
|
+
"module": "dist/esm/src/Emitter.js",
|
|
60
61
|
"exports": {
|
|
61
62
|
".": {
|
|
62
|
-
"import": "./dist/
|
|
63
|
+
"import": "./dist/esm/src/Emitter.js",
|
|
63
64
|
"require": "./dist/cjs/src/Emitter.js"
|
|
64
65
|
}
|
|
65
66
|
},
|
package/readme.md
CHANGED
|
@@ -2,35 +2,88 @@
|
|
|
2
2
|
|
|
3
3
|
## Usage
|
|
4
4
|
|
|
5
|
-
###
|
|
6
|
-
|
|
5
|
+
### JavaScript
|
|
6
|
+
```javascript
|
|
7
|
+
import { Emitter } from "@irrelon/emitter";
|
|
8
|
+
const emitter = new Emitter();
|
|
7
9
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
// When the emit() call is made at the end of this
|
|
11
|
+
// example this listener will be called with
|
|
12
|
+
// `isEnabled = true` and `id = "1234"`. You can pass
|
|
13
|
+
// any number of arguments when calling emit() and
|
|
14
|
+
// they will be recieved in the same order by your
|
|
15
|
+
// event listeners
|
|
16
|
+
emitter.on("someEvent", (isEnabled, id) => {
|
|
17
|
+
return "someReturnValue1";
|
|
18
|
+
});
|
|
11
19
|
|
|
12
|
-
|
|
13
|
-
|
|
20
|
+
// Lets register another listener on the same event
|
|
21
|
+
// that returns a slightly different value
|
|
22
|
+
emitter.on("someEvent", (isEnabled, id) => {
|
|
23
|
+
return "someReturnValue2";
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// The `results` will contain the return values from
|
|
27
|
+
// all the event listeners registered for the event
|
|
28
|
+
// so in this case ["someReturnValue1", "someReturnValue2"]
|
|
29
|
+
const results = emitter.emit("someEvent", true, "1234");
|
|
14
30
|
```
|
|
15
31
|
|
|
16
|
-
|
|
32
|
+
### TypeScript Compatibility
|
|
33
|
+
> TypeScript based projects can benefit from type safety if you declare the event listener
|
|
34
|
+
> function signatures via an interface as shown below
|
|
17
35
|
|
|
18
|
-
```
|
|
19
|
-
|
|
36
|
+
```typescript
|
|
37
|
+
import { Emitter } from "@irrelon/emitter";
|
|
38
|
+
|
|
39
|
+
interface MyEvents {
|
|
40
|
+
event1: (name: string) => number;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Pass your interface to the Emitter instantiation
|
|
44
|
+
const emitter = new Emitter<MyEvents>();
|
|
45
|
+
|
|
46
|
+
// This will show a typescript error because the
|
|
47
|
+
// first argument of the event1 listener should be
|
|
48
|
+
// a string, and the listener should return a number
|
|
49
|
+
// instead of void.
|
|
50
|
+
emitter.on("event1", (isEnabled: boolean) => {
|
|
51
|
+
return;
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// This will not error as it satisfies the
|
|
55
|
+
// MyEvents.event1 event listener signature
|
|
56
|
+
emitter.on("event1", (name: string) => {
|
|
57
|
+
return 18;
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// This will error because a string argument is
|
|
61
|
+
// expected and none is provided to the call
|
|
62
|
+
emitter.emit("event1");
|
|
63
|
+
|
|
64
|
+
// This will not error as you are passing the
|
|
65
|
+
// expected string argument
|
|
66
|
+
emitter.emit("event1", "John Smith");
|
|
67
|
+
|
|
68
|
+
// This will correctly infer the type of `result`
|
|
69
|
+
// as a number since the return type was defined
|
|
70
|
+
// in the MyEvents.event1 interface
|
|
71
|
+
const result = emitter.emit("event1", "John Smith");
|
|
20
72
|
```
|
|
21
73
|
|
|
22
|
-
|
|
74
|
+
### Extending The Emitter Class
|
|
23
75
|
|
|
24
76
|
```js
|
|
25
|
-
import {Emitter} from "@irrelon/emitter";
|
|
77
|
+
import { Emitter } from "@irrelon/emitter";
|
|
78
|
+
|
|
26
79
|
class MyClass extends Emitter {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
80
|
+
async someAsyncFunc () {
|
|
81
|
+
await this.emit('myEvent', myData, myOtherData);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
someFunc () {
|
|
85
|
+
this.emit('myEvent', myData, myOtherData);
|
|
86
|
+
}
|
|
34
87
|
};
|
|
35
88
|
```
|
|
36
89
|
|
|
@@ -46,10 +99,38 @@ Your class now inherits the emitter methods:
|
|
|
46
99
|
* cancelStatic
|
|
47
100
|
* deferEmit
|
|
48
101
|
* willEmit
|
|
102
|
+
* rpc
|
|
103
|
+
* rpcId
|
|
104
|
+
|
|
105
|
+
### EcmaScript Modules and CommonJS Modules
|
|
106
|
+
> The package includes both ESM and CJS modules for ease of use.
|
|
107
|
+
> Use `import` to get the ESM version, and `require()` to get
|
|
108
|
+
> the CJS version.
|
|
109
|
+
|
|
110
|
+
### Install via NPM / Yarn:
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
npm i @irrelon/emitter
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
yarn add @irrelon/emitter
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Include in Your Application
|
|
121
|
+
|
|
122
|
+
```js
|
|
123
|
+
import {Emitter} from "@irrelon/emitter";
|
|
124
|
+
```
|
|
125
|
+
or
|
|
126
|
+
```js
|
|
127
|
+
var Emitter = require("@irrelon/emitter");
|
|
128
|
+
```
|
|
49
129
|
|
|
50
130
|
### Browser
|
|
131
|
+
|
|
51
132
|
Include the Emitter.js file in your HTML (the path depends on where you've put the file)
|
|
52
133
|
|
|
53
134
|
```html
|
|
54
|
-
<script src="./src/Emitter.js" type="module"></script>
|
|
135
|
+
<script src="./dist/esm/src/Emitter.js" type="module"></script>
|
|
55
136
|
```
|