Skip to content

Commit bb67651

Browse files
committed
feat: allow ensureDelivery to be able to ensure delivery on emit
1 parent 1671f66 commit bb67651

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function sendWithAxios(message) {
107107

108108
const emit = emitterFor(sendWithAxios, { mode: Mode.BINARY });
109109
// Set the emit
110-
Emitter.getSingleton().on("event", emit);
110+
Emitter.on("cloudevent", emit);
111111

112112
...
113113
// In any part of the code will send the event

src/event/cloudevent.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,11 @@ export class CloudEvent implements CloudEventV1, CloudEventV03 {
171171
/**
172172
* Emit this CloudEvent through the application
173173
*
174-
* @return {CloudEvent} current CloudEvent object
174+
* @param {boolean} ensureDelivery fail the promise if one listener fail
175+
* @return {Promise<CloudEvent>} current CloudEvent object
175176
*/
176-
public emit(): this {
177-
Emitter.emitEvent(this);
177+
public async emit(ensureDelivery = false): Promise<this> {
178+
await Emitter.emitEvent(this, ensureDelivery);
178179
return this;
179180
}
180181

src/transport/emitter.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export class Emitter extends EventEmitter {
9898
/**
9999
* Singleton store
100100
*/
101-
static singleton: Emitter | undefined = undefined;
101+
static instance: Emitter | undefined = undefined;
102102
url?: string;
103103
protocol: Protocol;
104104
binaryEmitter: EmitterFunction;
@@ -153,20 +153,42 @@ export class Emitter extends EventEmitter {
153153
*
154154
* @return {Emitter} return Emitter singleton
155155
*/
156-
static getSingleton(): Emitter {
157-
if (!Emitter.singleton) {
158-
Emitter.singleton = new Emitter();
156+
static getInstance(): Emitter {
157+
if (!Emitter.instance) {
158+
Emitter.instance = new Emitter();
159159
}
160-
return Emitter.singleton;
160+
return Emitter.instance;
161+
}
162+
163+
/**
164+
* Add a listener for eventing
165+
*
166+
* @param {string} event type to listen to
167+
* @param {Function} listener to call on event
168+
* @return {void}
169+
*/
170+
static on(event: "cloudevent" | "newListener" | "removeListener", listener: (...args: any[]) => void): void {
171+
this.getInstance().on(event, listener);
161172
}
162173

163174
/**
164175
* Emit an event inside this application
165176
*
166177
* @param {CloudEvent} event to emit
178+
* @param {boolean} ensureDelivery fail the promise if one listener fail
167179
* @return {void}
168180
*/
169-
static emitEvent(event: CloudEvent): void {
170-
this.getSingleton().emit("event", event);
181+
static async emitEvent(event: CloudEvent, ensureDelivery = false): Promise<void> {
182+
if (!ensureDelivery) {
183+
// Ensure delivery is disable so we do wait for Promise
184+
Emitter.getInstance().emit("cloudevent", event);
185+
} else {
186+
// Execute all listeners and wrap them in a Promise
187+
await Promise.all(
188+
Emitter.getInstance()
189+
.listeners("cloudevent")
190+
.map((l) => Promise.resolve(l(event))),
191+
);
192+
}
171193
}
172194
}

0 commit comments

Comments
 (0)