Skip to content

Commit

Permalink
refactor: extract handle message from WsAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Oct 17, 2018
1 parent 3e9bfff commit a6b0016
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions packages/websockets/adapters/ws-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import { MessageMappingProperties } from '../gateway-metadata-explorer';
let wsPackage: any = {};

enum READY_STATE {
CONNECTING_STATE = 0,
OPEN_STATE = 1,
CLOSING_STATE = 2,
CLOSED_STATE = 3,
CONNECTING_STATE = 0,
OPEN_STATE = 1,
CLOSING_STATE = 2,
CLOSED_STATE = 3,
}

export class WsAdapter implements WebSocketAdapter {
Expand Down Expand Up @@ -79,11 +79,13 @@ export class WsAdapter implements WebSocketAdapter {
),
takeUntil(close$),
);
source$.subscribe(response => {
if (client.readyState === READY_STATE.OPEN_STATE) {
client.send(JSON.stringify(response));
const handleMessage = response => {
if (client.readyState !== READY_STATE.OPEN_STATE) {
return;
}
});
client.send(JSON.stringify(response));
};
source$.subscribe(handleMessage);
}

public bindMessageHandler(
Expand All @@ -108,7 +110,9 @@ export class WsAdapter implements WebSocketAdapter {
}

public bindErrorHandler(server) {
server.on(CONNECTION_EVENT, ws => ws.on(ERROR_EVENT, err => this.logger.error(err)));
server.on(CONNECTION_EVENT, ws =>
ws.on(ERROR_EVENT, err => this.logger.error(err)),
);
server.on(ERROR_EVENT, err => this.logger.error(err));
return server;
}
Expand Down

0 comments on commit a6b0016

Please sign in to comment.