@@ -91,15 +91,29 @@ export function emitterFor(fn: TransportFunction, options = { binding: HTTP, mod
91
91
*
92
92
* @see https://github.com/cloudevents/spec/blob/v1.0/http-protocol-binding.md
93
93
* @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
94
*
96
95
*/
97
- export class Emitter {
96
+ export class Emitter extends EventEmitter {
97
+ /**
98
+ * Singleton store
99
+ */
100
+ static singleton : Emitter = undefined ;
98
101
url ?: string ;
99
102
protocol : Protocol ;
100
103
binaryEmitter : EmitterFunction ;
101
104
structuredEmitter : EmitterFunction ;
102
105
106
+ /**
107
+ * Create an Emitter
108
+ * On v4.0.0 this class will only remains as Singleton to allow using the
109
+ * EventEmitter of NodeJS
110
+ *
111
+ *
112
+ * @param {Object } [options] The configuration options for this event. Options
113
+ * provided will be passed along to Node.js `http.request()`.
114
+ * https://nodejs.org/api/http.html#http_http_request_options_callback
115
+ * @deprecated Will be removed in 4.0.0. Consider using the emitterFactory
116
+ */
103
117
constructor ( options : TransportOptions = { protocol : Protocol . HTTPBinary } ) {
104
118
this . protocol = options . protocol as Protocol ;
105
119
this . url = options . url ;
@@ -131,4 +145,23 @@ export class Emitter {
131
145
}
132
146
return this . binaryEmitter ( event , options ) ;
133
147
}
148
+
149
+ /**
150
+ * Return or create the Emitter singleton
151
+ */
152
+ static getSingleton ( ) {
153
+ if ( ! Emitter . singleton ) {
154
+ Emitter . singleton = new Emitter ( ) ;
155
+ }
156
+ return Emitter . singleton ;
157
+ }
158
+
159
+ /**
160
+ * Emit an event inside this application
161
+ *
162
+ * @param event to emit
163
+ */
164
+ static emit ( event : CloudEvent ) {
165
+ this . getSingleton ( ) . emit ( "event" , event ) ;
166
+ }
134
167
}
0 commit comments