Skip to content

Commit 4e4a429

Browse files
committed
feat: add EventEmitter to Emitter and singleton paradigm
Signed-off-by: Remi Cattiau <[email protected]>
1 parent 765b81c commit 4e4a429

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/event/cloudevent.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { v4 as uuidv4 } from "uuid";
2+
import { Emitter } from "..";
23

34
import {
45
CloudEventV03,
@@ -167,6 +168,16 @@ export class CloudEvent implements CloudEventV1, CloudEventV03 {
167168
}
168169
}
169170

171+
/**
172+
* Emit this CloudEvent through the application
173+
*
174+
* @return {CloudEvent} current CloudEvent object
175+
*/
176+
public emit(): this {
177+
Emitter.emitEvent(this);
178+
return this;
179+
}
180+
170181
/**
171182
* Clone a CloudEvent with new/update attributes
172183
* @param {object} options attributes to augment the CloudEvent with

src/transport/emitter.ts

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { axiosEmitter } from "./http";
33
import { Protocol } from "./protocols";
44
import { Agent } from "http";
55
import { HTTP, Message, Mode } from "../message";
6+
import { EventEmitter } from "events";
67

78
/**
89
* Options supplied to the Emitter when sending an event.
@@ -91,16 +92,31 @@ export function emitterFor(fn: TransportFunction, options = { binding: HTTP, mod
9192
*
9293
* @see https://github.com/cloudevents/spec/blob/v1.0/http-protocol-binding.md
9394
* @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
9595
*
9696
*/
97-
export class Emitter {
97+
export class Emitter extends EventEmitter {
98+
/**
99+
* Singleton store
100+
*/
101+
static singleton: Emitter | undefined = undefined;
98102
url?: string;
99103
protocol: Protocol;
100104
binaryEmitter: EmitterFunction;
101105
structuredEmitter: EmitterFunction;
102106

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+
*/
103118
constructor(options: TransportOptions = { protocol: Protocol.HTTPBinary }) {
119+
super();
104120
this.protocol = options.protocol as Protocol;
105121
this.url = options.url;
106122

@@ -131,4 +147,26 @@ export class Emitter {
131147
}
132148
return this.binaryEmitter(event, options);
133149
}
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+
}
134172
}

0 commit comments

Comments
 (0)