8000 fix: Emitter should not extend EventEmitter · aarne/sdk-javascript@1af3d43 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1af3d43

Browse files
committed
fix: Emitter should not extend EventEmitter
This change modifies Emitter so that it does not directly extend the Node.js EventEmitter class. Instead, it holds a singleton instance of an EventEmitter but is not an instance of EventEmitter itself. This commit also updates the typescript example to use a modern version of @types/node and typescript. Finally there are a few minor formatting changes picked up by eslint. Fixes: cloudevents#385 Signed-off-by: Lance Ball <lball@redhat.com>
1 parent 6be3b27 commit 1af3d43

File tree

5 files changed

+22
-31
lines changed

5 files changed

+22
-31
lines changed

examples/typescript-ex/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
"posttest": "npm run check"
2424
},
2525
"devDependencies": {
26-
"@types/node": "^8.9.0",
26+
"@types/node": "^14.14.10",
2727
"gts": "^1.1.0",
28-
"typescript": "~3.9.5"
28+
"typescript": "~4.1.3"
2929
},
3030
"dependencies": {
3131
"cloudevents": "~4.0.0"

examples/typescript-ex/src/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import { CloudEvent, CloudEventV1, HTTP } from "cloudevents";
1+
/* eslint-disable no-console */
2+
import { CloudEvent, HTTP } from "cloudevents";
23

34
export function doSomeStuff(): void {
4-
const myevent: CloudEventV1 = new CloudEvent({
5+
const myevent: CloudEvent = new CloudEvent({
56
source: "/source",
67
type: "type",
78
datacontenttype: "text/plain",
89
dataschema: "https://d.schema.com/my.json",
910
subject: "cha.json",
1011
data: "my-data",
11-
extension1: "some extension data"
12+
extension1: "some extension data",
1213
});
1314

1415
console.log("My structured event:", myevent);
@@ -21,7 +22,7 @@ export function doSomeStuff(): void {
2122

2223
// Typically used with an incoming HTTP request where myevent.format() is the actual
2324
// body of the HTTP
24-
console.log("Received structured event:", HTTP.toEvent({headers, body: myevent}));
25+
console.log("Received structured event:", HTTP.toEvent({ headers, body: myevent }));
2526

2627
// ------ receiver binary
2728
const data = {
@@ -38,9 +39,8 @@ export function doSomeStuff(): void {
3839
"ce-extension1": "extension1",
3940
};
4041

41-
console.log("My binary event:", HTTP.toEvent({headers: attributes, body: data}));
42-
console.log("My binary event extensions:", HTTP.toEvent({headers: attributes, body: data}));
43-
42+
console.log("My binary event:", HTTP.toEvent({ headers: attributes, body: data }));
43+
console.log("My binary event extensions:", HTTP.toEvent({ headers: attributes, body: data }));
4444
}
4545

4646
doSomeStuff();

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
"@types/cucumber": "^6.0.1",
109109
"@types/got": "^9.6.11",
110110
"@types/mocha": "^7.0.2",
111-
"@types/node": "^13.13.9",
111+
"@types/node": "^14.14.10",
112112
"@types/superagent": "^4.1.10",
113113
"@types/uuid": "^8.0.0",
114114
"@typescript-eslint/eslint-plugin": "^3.4.0",

src/transport/emitter.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ export function emitterFor(fn: TransportFunction, options = { binding: HTTP, mod
4646
throw new TypeError("A TransportFunction is required");
4747
}
4848
const { binding, mode } = options;
49-
return function emit(event: CloudEvent, options?: Options): Promise<unknown> {
50-
options = options || {};
49+
return function emit(event: CloudEvent, opts?: Options): Promise<unknown> {
50+
opts = opts || {};
5151

5252
switch (mode) {
5353
case Mode.BINARY:
54-
return fn(binding.binary(event), options);
54+
return fn(binding.binary(event), opts);
5555
case Mode.STRUCTURED:
56-
return fn(binding.structured(event), options);
56+
return fn(binding.structured(event), opts);
5757
default:
5858
throw new TypeError(`Unexpected transport mode: ${mode}`);
5959
}
@@ -63,29 +63,20 @@ export function emitterFor(fn: TransportFunction, options = { binding: HTTP, mod
6363
/**
6464
* A static class to emit CloudEvents within an application
6565
*/
66-
export class Emitter extends EventEmitter {
66+
export class Emitter {
6767
/**
6868
* Singleton store
6969
*/
70-
static instance: Emitter | undefined = undefined;
71-
72-
/**
73-
* Create an Emitter
74-
* On v4.0.0 this class will only remains as Singleton to allow using the
75-
* EventEmitter of NodeJS
76-
*/
77-
private constructor() {
78-
super();
79-
}
70+
static instance: EventEmitter | undefined = undefined;
8071

8172
/**
8273
* Return or create the Emitter singleton
8374
*
8475
* @return {Emitter} return Emitter singleton
8576
*/
86-
static getInstance(): Emitter {
77+
static getInstance(): EventEmitter {
8778
if (!Emitter.instance) {
88-
Emitter.instance = new Emitter();
79+
Emitter.instance = new EventEmitter();
8980
}
9081
return Emitter.instance;
9182
}
@@ -97,7 +88,7 @@ export class Emitter extends EventEmitter {
9788
* @param {Function} listener to call on event
9889
* @return {void}
9990
*/
100-
static on(event: "cloudevent" | "newListener" | "removeListener", listener: (...args: any[]) => void): void {
91+
static on(event: string, listener: (...args: any[]) => void): void {
10192
this.getInstance().on(event, listener);
10293
}
10394

0 commit comments

Comments
 (0)
0