@@ -3,6 +3,7 @@ import { axiosEmitter } from "./http";
3
3
import { Protocol } from "./protocols" ;
4
4
import { Agent } from "http" ;
5
5
import { HTTP , Message , Mode } from "../message" ;
6
+ import { EventEmitter } from "events" ;
6
7
7
8
/**
8
9
* Options supplied to the Emitter when sending an event.
@@ -91,16 +92,31 @@ export function emitterFor(fn: TransportFunction, options = { binding: HTTP, mod
91
92
*
92
93
* @see https://github.com/cloudevents/spec/blob/v1.0/http-protocol-binding.md
93
94
* @see https://github.com/cloudevents/spec/blob/v1.0/http-protocol-binding.md#13-content-modes
94
- * @deprecated Will be removed in 4.0.0. Consider using the emitterFactory
95
95
*
96
96
*/
97
- export class Emitter {
97
+ export class Emitter extends EventEmitter {
98
+ /**
99
+ * Singleton store
100
+ */
101
+ static singleton : Emitter | undefined = undefined ;
98
102
url ?: string ;
99
103
protocol : Protocol ;
100
104
binaryEmitter : EmitterFunction ;
101
105
structuredEmitter : EmitterFunction ;
102
106
107
+ /**
108
+ * Create an Emitter
109
+ * On v4.0.0 this class will only remains as Singleton to allow using the
110
+ * EventEmitter of NodeJS
111
+ *
112
+ *
113
+ * @param {Object } [options] The configuration options for this event. Options
114
+ * provided will be passed along to Node.js `http.request()`.
115
+ * https://nodejs.org/api/http.html#http_http_request_options_callback
116
+ * @deprecated Will be removed in 4.0.0. Consider using the emitterFactory
117
+ */
103
118
constructor ( options : TransportOptions = { protocol : Protocol . HTTPBinary } ) {
119
+ super ( ) ;
104
120
this . protocol = options . protocol as Protocol ;
105
121
this . url = options . url ;
106
122
@@ -131,4 +147,26 @@ export class Emitter {
131
147
}
132
148
return this . binaryEmitter ( event , options ) ;
133
149
}
150
+
151
+ /**
152
+ * Return or create the Emitter singleton
153
+ *
154
+ * @return {Emitter } return Emitter singleton
155
+ */
156
+ static getSingleton ( ) : Emitter {
157
+ if ( ! Emitter . singleton ) {
158
+ Emitter . singleton = new Emitter ( ) ;
159
+ }
160
+ return Emitter . singleton ;
161
+ }
162
+
163
+ /**
164
+ * Emit an event inside this application
165
+ *
166
+ * @param {CloudEvent } event to emit
167
+ * @return {void }
168
+ */
169
+ static emitEvent ( event : CloudEvent ) : void {
170
+ this . getSingleton ( ) . emit ( "event" , event ) ;
171
+ }
134
172
}
0 commit comments