hookified 1.12.1 → 1.13.0
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 +55 -12
- package/dist/browser/index.global.js +615 -1
- package/dist/browser/index.global.js.map +1 -1
- package/dist/browser/index.js +616 -1
- package/dist/browser/index.js.map +1 -1
- package/dist/node/index.cjs +640 -1
- package/dist/node/index.d.cts +35 -1
- package/dist/node/index.d.ts +35 -1
- package/dist/node/index.js +612 -1
- package/package.json +11 -11
package/README.md
CHANGED
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
- [Usage](#usage)
|
|
29
29
|
- [Using it in the Browser](#using-it-in-the-browser)
|
|
30
30
|
- [API - Hooks](#api---hooks)
|
|
31
|
-
- [.
|
|
31
|
+
- [.throwOnHookError](#throwhookerror)
|
|
32
32
|
- [.logger](#logger)
|
|
33
33
|
- [.enforceBeforeAfter](#enforcebeforeafter)
|
|
34
34
|
- [.deprecatedHooks](#deprecatedhooks)
|
|
@@ -51,6 +51,7 @@
|
|
|
51
51
|
- [.clearHooks(eventName)](#clearhookeventname)
|
|
52
52
|
- [API - Events](#api---events)
|
|
53
53
|
- [.throwOnEmitError](#throwonemitterror)
|
|
54
|
+
- [.throwOnEmptyListeners](#throwonemptylisteners)
|
|
54
55
|
- [.on(eventName, handler)](#oneventname-handler)
|
|
55
56
|
- [.off(eventName, handler)](#offeventname-handler)
|
|
56
57
|
- [.emit(eventName, ...args)](#emiteventname-args)
|
|
@@ -174,7 +175,7 @@ if you are not using ESM modules, you can use the following:
|
|
|
174
175
|
|
|
175
176
|
# API - Hooks
|
|
176
177
|
|
|
177
|
-
## .
|
|
178
|
+
## .throwOnHookError
|
|
178
179
|
|
|
179
180
|
If set to true, errors thrown in hooks will be thrown. If set to false, errors will be only emitted.
|
|
180
181
|
|
|
@@ -183,13 +184,13 @@ import { Hookified } from 'hookified';
|
|
|
183
184
|
|
|
184
185
|
class MyClass extends Hookified {
|
|
185
186
|
constructor() {
|
|
186
|
-
super({
|
|
187
|
+
super({ throwOnHookError: true });
|
|
187
188
|
}
|
|
188
189
|
}
|
|
189
190
|
|
|
190
191
|
const myClass = new MyClass();
|
|
191
192
|
|
|
192
|
-
console.log(myClass.
|
|
193
|
+
console.log(myClass.throwOnHookError); // true. because it is set in super
|
|
193
194
|
|
|
194
195
|
try {
|
|
195
196
|
myClass.onHook('error-event', async () => {
|
|
@@ -201,8 +202,8 @@ try {
|
|
|
201
202
|
console.log(error.message); // error
|
|
202
203
|
}
|
|
203
204
|
|
|
204
|
-
myClass.
|
|
205
|
-
console.log(myClass.
|
|
205
|
+
myClass.throwOnHookError = false;
|
|
206
|
+
console.log(myClass.throwOnHookError); // false
|
|
206
207
|
```
|
|
207
208
|
|
|
208
209
|
## .logger
|
|
@@ -911,6 +912,48 @@ class MyClass extends Hookified {
|
|
|
911
912
|
}
|
|
912
913
|
```
|
|
913
914
|
|
|
915
|
+
## .throwOnEmptyListeners
|
|
916
|
+
|
|
917
|
+
If set to true, errors will be thrown when emitting an `error` event with no listeners. This follows the standard Node.js EventEmitter behavior. Default is false. In version 2, this will be set to true by default.
|
|
918
|
+
|
|
919
|
+
```javascript
|
|
920
|
+
import { Hookified } from 'hookified';
|
|
921
|
+
|
|
922
|
+
class MyClass extends Hookified {
|
|
923
|
+
constructor() {
|
|
924
|
+
super({ throwOnEmptyListeners: true });
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
const myClass = new MyClass();
|
|
929
|
+
|
|
930
|
+
console.log(myClass.throwOnEmptyListeners); // true
|
|
931
|
+
|
|
932
|
+
// This will throw because there are no error listeners
|
|
933
|
+
try {
|
|
934
|
+
myClass.emit('error', new Error('Something went wrong'));
|
|
935
|
+
} catch (error) {
|
|
936
|
+
console.log(error.message); // Something went wrong
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
// Add an error listener - now it won't throw
|
|
940
|
+
myClass.on('error', (error) => {
|
|
941
|
+
console.log('Error caught:', error.message);
|
|
942
|
+
});
|
|
943
|
+
|
|
944
|
+
myClass.emit('error', new Error('This will be caught')); // No throw, listener handles it
|
|
945
|
+
|
|
946
|
+
// You can also change it dynamically
|
|
947
|
+
myClass.throwOnEmptyListeners = false;
|
|
948
|
+
console.log(myClass.throwOnEmptyListeners); // false
|
|
949
|
+
```
|
|
950
|
+
|
|
951
|
+
**Difference between `throwOnEmitError` and `throwOnEmptyListeners`:**
|
|
952
|
+
- `throwOnEmitError`: Throws when emitting 'error' event every time.
|
|
953
|
+
- `throwOnEmptyListeners`: Throws only when there are NO error listeners registered
|
|
954
|
+
|
|
955
|
+
When both are set to `true`, `throwOnEmitError` takes precedence.
|
|
956
|
+
|
|
914
957
|
## .on(eventName, handler)
|
|
915
958
|
|
|
916
959
|
Subscribe to an event.
|
|
@@ -1204,8 +1247,8 @@ We are doing very simple benchmarking to see how this compares to other librarie
|
|
|
1204
1247
|
|
|
1205
1248
|
| name | summary | ops/sec | time/op | margin | samples |
|
|
1206
1249
|
|-----------------------|:---------:|----------:|----------:|:--------:|----------:|
|
|
1207
|
-
| Hookified (v1.
|
|
1208
|
-
| Hookable (v5.5.3) | -
|
|
1250
|
+
| Hookified (v1.13.0) | 🥇 | 5M | 238ns | ±1.06% | 4M |
|
|
1251
|
+
| Hookable (v5.5.3) | -68% | 1M | 826ns | ±2.25% | 1M |
|
|
1209
1252
|
|
|
1210
1253
|
## Emits
|
|
1211
1254
|
|
|
@@ -1213,10 +1256,10 @@ This shows how on par `hookified` is to the native `EventEmitter` and popular `e
|
|
|
1213
1256
|
|
|
1214
1257
|
| name | summary | ops/sec | time/op | margin | samples |
|
|
1215
1258
|
|---------------------------|:---------:|----------:|----------:|:--------:|----------:|
|
|
1216
|
-
| Hookified (v1.
|
|
1217
|
-
| EventEmitter3 (v5.0.1) |
|
|
1218
|
-
| EventEmitter (v20.17.0) |
|
|
1219
|
-
| Emittery (v1.2.0) | -91% | 1M | 1µs | ±
|
|
1259
|
+
| Hookified (v1.13.0) | 🥇 | 12M | 90ns | ±3.17% | 11M |
|
|
1260
|
+
| EventEmitter3 (v5.0.1) | -0.52% | 12M | 89ns | ±1.66% | 11M |
|
|
1261
|
+
| EventEmitter (v20.17.0) | -3.5% | 12M | 91ns | ±0.42% | 11M |
|
|
1262
|
+
| Emittery (v1.2.0) | -91% | 1M | 1µs | ±3.33% | 959K |
|
|
1220
1263
|
|
|
1221
1264
|
_Note: the `EventEmitter` version is Nodejs versioning._
|
|
1222
1265
|
|