@adonisjs/events 7.0.2 → 7.1.3

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/LICENSE.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # The MIT License
2
2
 
3
- Copyright 2021 Harminder Virk, contributors
3
+ Copyright 2022 Harminder Virk, contributors
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
6
 
@@ -17,6 +17,10 @@ declare module '@ioc:Adonis/Core/Event' {
17
17
  * Shape of event trap handler
18
18
  */
19
19
  export type TrapHandler<T extends any = any> = EventHandler<T>;
20
+ /**
21
+ * The error handler for handling events when an emit call fails
22
+ */
23
+ export type ErrorHandler = (event: string, error: any, data: any) => void;
20
24
  /**
21
25
  * Shape of trap all events handler
22
26
  */
@@ -41,6 +45,10 @@ declare module '@ioc:Adonis/Core/Event' {
41
45
  */
42
46
  export interface EmitterContract {
43
47
  transport: EmitterTransportContract;
48
+ /**
49
+ * Define a custom error handler
50
+ */
51
+ onError(handler: ErrorHandler): this;
44
52
  /**
45
53
  * Define a custom IoC Container base namespace for resolving
46
54
  * the listener bindings.
@@ -84,6 +92,10 @@ declare module '@ioc:Adonis/Core/Event' {
84
92
  */
85
93
  clearListeners<K extends keyof EventsList>(event: K): void;
86
94
  clearListeners<K extends string>(event: K): void;
95
+ /**
96
+ * Clear all listeners for all events
97
+ */
98
+ clearAllListeners(): void;
87
99
  /**
88
100
  * Returns count of listeners listening for a given event
89
101
  */
@@ -1,6 +1,6 @@
1
1
  /// <reference path="../../adonis-typings/events.d.ts" />
2
2
  import { ApplicationContract } from '@ioc:Adonis/Core/Application';
3
- import { AnyHandler, EventsList, TrapHandler, EventHandler, DataForEvent, TrapAllHandler, EmitterContract, EmitterTransportContract } from '@ioc:Adonis/Core/Event';
3
+ import { AnyHandler, EventsList, TrapHandler, EventHandler, DataForEvent, ErrorHandler, TrapAllHandler, EmitterContract, EmitterTransportContract } from '@ioc:Adonis/Core/Event';
4
4
  /**
5
5
  * Emitter class exposes the API for async event emitter built on top of
6
6
  * Emittery. It also exposes an API to pre-define the Typescript types
@@ -12,12 +12,17 @@ export declare class Emitter implements EmitterContract {
12
12
  private trappingEvents;
13
13
  private traps;
14
14
  private trapAllHandler?;
15
+ private errorHandler?;
15
16
  constructor(app?: ApplicationContract);
16
17
  /**
17
18
  * Returns reference to the IoC resolver. Do not call this method until
18
19
  * handler is not a string
19
20
  */
20
21
  private getResolver;
22
+ /**
23
+ * Define a custom error handler
24
+ */
25
+ onError(handler: ErrorHandler): this;
21
26
  /**
22
27
  * Define event handler for a given event
23
28
  */
@@ -39,6 +39,13 @@ class Emitter {
39
39
  }
40
40
  return this.iocResolver;
41
41
  }
42
+ /**
43
+ * Define a custom error handler
44
+ */
45
+ onError(handler) {
46
+ this.errorHandler = handler;
47
+ return this;
48
+ }
42
49
  /**
43
50
  * Define event handler for a given event
44
51
  */
@@ -79,21 +86,29 @@ class Emitter {
79
86
  * Emit event
80
87
  */
81
88
  async emit(event, data) {
82
- if (this.trappingEvents) {
83
- /**
84
- * Give preference to the handler for a specific event
85
- */
86
- if (this.traps.has(event)) {
87
- return this.traps.get(event)(data);
89
+ try {
90
+ if (this.trappingEvents) {
91
+ /**
92
+ * Give preference to the handler for a specific event
93
+ */
94
+ if (this.traps.has(event)) {
95
+ return await this.traps.get(event)(data);
96
+ }
97
+ /**
98
+ * Invoke catch all (if defined)
99
+ */
100
+ if (this.trapAllHandler) {
101
+ return await this.trapAllHandler(event, data);
102
+ }
88
103
  }
89
- /**
90
- * Invoke catch all (if defined)
91
- */
92
- if (this.trapAllHandler) {
93
- return this.trapAllHandler(event, data);
104
+ return await this.transport.emit(event, data);
105
+ }
106
+ catch (error) {
107
+ if (this.errorHandler) {
108
+ return this.errorHandler(event, error, data);
94
109
  }
110
+ throw error;
95
111
  }
96
- return this.transport.emit(event, data);
97
112
  }
98
113
  /**
99
114
  * Remove existing event listener
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adonisjs/events",
3
- "version": "7.0.2",
3
+ "version": "7.1.3",
4
4
  "description": "Event emitter with asynchronous events",
5
5
  "main": "build/providers/EventProvider.js",
6
6
  "files": [
@@ -13,12 +13,12 @@
13
13
  "scripts": {
14
14
  "mrm": "mrm --preset=@adonisjs/mrm-preset",
15
15
  "pretest": "npm run lint",
16
- "test": "node japaFile.js",
16
+ "test": "node .bin/test.js",
17
17
  "clean": "del build",
18
18
  "compile": "npm run lint && npm run clean && tsc",
19
19
  "build": "npm run compile",
20
20
  "commit": "git-cz",
21
- "release": "np",
21
+ "release": "np --message=\"chore(release): %s\"",
22
22
  "version": "npm run build",
23
23
  "format": "prettier --write .",
24
24
  "prepublishOnly": "npm run build",
@@ -32,23 +32,25 @@
32
32
  "author": "virk,adonisjs",
33
33
  "license": "MIT",
34
34
  "devDependencies": {
35
- "@adonisjs/application": "^5.1.3",
36
- "@adonisjs/mrm-preset": "^4.1.0",
37
- "@adonisjs/require-ts": "^2.0.4",
38
- "@poppinss/dev-utils": "^1.1.4",
39
- "@types/node": "^15.6.1",
40
- "del-cli": "^3.0.1",
41
- "eslint": "^7.27.0",
35
+ "@adonisjs/application": "^5.1.5",
36
+ "@adonisjs/mrm-preset": "^5.0.2",
37
+ "@adonisjs/require-ts": "^2.0.7",
38
+ "@poppinss/dev-utils": "^2.0.1",
39
+ "@types/node": "^17.0.8",
40
+ "commitizen": "^4.2.4",
41
+ "cz-conventional-changelog": "^3.3.0",
42
+ "del-cli": "^4.0.1",
43
+ "eslint": "^8.7.0",
42
44
  "eslint-config-prettier": "^8.3.0",
43
- "eslint-plugin-adonis": "^1.3.1",
44
- "eslint-plugin-prettier": "^3.4.0",
45
- "github-label-sync": "^2.0.0",
46
- "husky": "^6.0.0",
47
- "japa": "^3.1.1",
45
+ "eslint-plugin-adonis": "^2.1.0",
46
+ "eslint-plugin-prettier": "^4.0.0",
47
+ "github-label-sync": "^2.0.1",
48
+ "husky": "^7.0.1",
49
+ "japa": "^4.0.0",
48
50
  "mrm": "^3.0.2",
49
51
  "np": "^7.5.0",
50
- "prettier": "^2.3.0",
51
- "typescript": "^4.3.2"
52
+ "prettier": "^2.3.2",
53
+ "typescript": "^4.3.5"
52
54
  },
53
55
  "nyc": {
54
56
  "exclude": [
@@ -68,7 +70,7 @@
68
70
  "anyBranch": false
69
71
  },
70
72
  "dependencies": {
71
- "emittery": "^0.9.0"
73
+ "emittery": "^0.10.0"
72
74
  },
73
75
  "peerDependencies": {
74
76
  "@adonisjs/application": "^5.0.0"
@@ -88,5 +90,48 @@
88
90
  "publishConfig": {
89
91
  "access": "public",
90
92
  "tag": "latest"
93
+ },
94
+ "mrmConfig": {
95
+ "core": true,
96
+ "license": "MIT",
97
+ "services": [
98
+ "github-actions"
99
+ ],
100
+ "minNodeVersion": "14.15.4",
101
+ "probotApps": [
102
+ "stale",
103
+ "lock"
104
+ ],
105
+ "runGhActionsOnWindows": false
106
+ },
107
+ "eslintConfig": {
108
+ "extends": [
109
+ "plugin:adonis/typescriptPackage",
110
+ "prettier"
111
+ ],
112
+ "plugins": [
113
+ "prettier"
114
+ ],
115
+ "rules": {
116
+ "prettier/prettier": [
117
+ "error",
118
+ {
119
+ "endOfLine": "auto"
120
+ }
121
+ ]
122
+ }
123
+ },
124
+ "eslintIgnore": [
125
+ "build"
126
+ ],
127
+ "prettier": {
128
+ "trailingComma": "es5",
129
+ "semi": false,
130
+ "singleQuote": true,
131
+ "useTabs": false,
132
+ "quoteProps": "consistent",
133
+ "bracketSpacing": true,
134
+ "arrowParens": "always",
135
+ "printWidth": 100
91
136
  }
92
137
  }