Skip to content

Commit

Permalink
fix(transports): handle multiple onData callback
Browse files Browse the repository at this point in the history
fixes #53
  • Loading branch information
shanejonas committed Aug 22, 2019
1 parent 7bb9563 commit b125962
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.
23 changes: 20 additions & 3 deletions src/transports/EventEmitterTransport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,30 @@ describe("EventEmitterTransport", () => {
it("can send and receive data", (done) => {
const emitter = new EventEmitter();
const eventEmitterTransport = new EventEmitterTransport(emitter, "from1", "to1");
eventEmitterTransport.connect().then(() => {
const eventEmitterServerTransport = new EventEmitterTransport(emitter, "to1", "from1");
eventEmitterServerTransport.sendData(JSON.stringify({ foo: "bar" }));
});
eventEmitterTransport.onData((data: any) => {
const d = JSON.parse(data);
expect(d.foo).toEqual("bar");
done();
});
});
it("can handle multiple calls to onData", (done) => {
const emitter = new EventEmitter();
const eventEmitterTransport = new EventEmitterTransport(emitter, "from1", "to1");
eventEmitterTransport.connect().then(() => {
const eventEmitterServerTransport = new EventEmitterTransport(emitter, "to1", "from1");
eventEmitterServerTransport.sendData(JSON.stringify({ foo: "bar" }));
});
eventEmitterTransport.onData(() => {
// noop
});
eventEmitterTransport.onData((data: any) => {
const d = JSON.parse(data);
expect(d.foo).toEqual("bar");
done();
});

const eventEmitterServerTransport = new EventEmitterTransport(emitter, "to1", "from1");
eventEmitterServerTransport.sendData(JSON.stringify({foo: "bar"}));
});
});
13 changes: 9 additions & 4 deletions src/transports/EventEmitterTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,25 @@ class EventEmitterTransport implements ITransport {
public connection: EventEmitter;
private reqUri: string;
private resUri: string;
private onDataCallbacks: any[];
constructor(emitter: EventEmitter, reqUri: string, resUri: string) {
this.onDataCallbacks = [];
this.connection = emitter;
this.reqUri = reqUri;
this.resUri = resUri;
}

public connect(): Promise<any> {
this.connection.on(this.reqUri, (data: any) => {
this.onDataCallbacks.map((callback: (data: string) => void) => {
callback(data);
});
});
return Promise.resolve();
}

public onData(callback: (data: string) => any) {
this.connection.on(this.reqUri, (data: any) => {
callback(data);
});
public onData(callback: (data: string) => void) {
this.onDataCallbacks.push(callback);
}

public sendData(data: string) {
Expand Down
18 changes: 17 additions & 1 deletion src/transports/WebSocketTransport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,27 @@ describe("WebSocketTransport", () => {
});
it("can send and receive data", (done) => {
const wst = new WebSocketTransport("http://localhost:8545");
wst.connect().then(() => {
wst.sendData(JSON.stringify({ foo: "bar" }));
});
wst.onData((data: any) => {
const d = JSON.parse(data);
expect(d.foo).toEqual("bar");
done();
});
});
it("can handle multiple onData callbacks", (done) => {
const wst = new WebSocketTransport("http://localhost:8545");
wst.connect().then(() => {
wst.sendData(JSON.stringify({ foo: "bar" }));
});
wst.onData(() => {
// noop
});
wst.onData((data: any) => {
const d = JSON.parse(data);
expect(d.foo).toEqual("bar");
done();
});
wst.sendData(JSON.stringify({foo: "bar"}));
});
});
13 changes: 9 additions & 4 deletions src/transports/WebSocketTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import ITransport from "./Transport";

class WebSocketTransport implements ITransport {
public connection: WS;
private onDataCallbacks: any[];
constructor(uri: string) {
this.connection = new WS(uri);
this.onDataCallbacks = [];
}
public connect(): Promise<any> {
return new Promise((resolve, reject) => {
Expand All @@ -13,12 +15,15 @@ class WebSocketTransport implements ITransport {
resolve();
};
this.connection.addEventListener("open", cb);
this.connection.addEventListener("message", (ev: { data: string }) => {
this.onDataCallbacks.map((callback: (data: string) => void) => {
callback(ev.data);
});
});
});
}
public onData(callback: (data: string) => any) {
this.connection.addEventListener("message", (ev: {data: string}) => {
callback(ev.data);
});
public onData(callback: (data: string) => void) {
this.onDataCallbacks.push(callback);
}
public sendData(data: any) {
this.connection.send(data);
Expand Down

0 comments on commit b125962

Please sign in to comment.