Skip to content

Commit

Permalink
refactor(join): clearify code
Browse files Browse the repository at this point in the history
  • Loading branch information
kalitine committed Feb 1, 2017
1 parent b825f91 commit d546298
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 24 deletions.
19 changes: 7 additions & 12 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,6 @@ const openers = new Map();

class WSServer {

constructor () {
this.server = null;
}

static start (options, cb = () => {}, extraOptions) {
let WebSocket = {};
try {
Expand Down Expand Up @@ -228,7 +224,11 @@ class WSServer {
if (ioMsg.isToOpen()) {
open(socket, ioMsg);
} else if (ioMsg.isToJoin()) {
join(socket, ioMsg);
if (openers.has(ioMsg.key)) {
join(socket, ioMsg);
} else {
open(socket, ioMsg);
}
} else if (ioMsg.isToTransmitToOpener()) {
transmitToOpener(socket, ioMsg);
} else if (ioMsg.isToTransmitToJoining()) {
Expand All @@ -250,7 +250,6 @@ class WSServer {
console.log('Server has stopped successfully');
this.server.close(cb);
}

}

function errorOnSendCB (err) {
Expand Down Expand Up @@ -279,12 +278,8 @@ function open (socket, ioMsg) {
}

function join (socket, ioMsg) {
if (openers.has(ioMsg.key)) {
openers.get(ioMsg.key).values().next().value.addJoining(socket);
socket.send(IOJsonString.msgOpened(false), errorOnSendCB);
} else {
open(socket, ioMsg);
}
openers.get(ioMsg.key).values().next().value.addJoining(socket);
socket.send(IOJsonString.msgOpened(false), errorOnSendCB);
}

function transmitToJoining (socket, ioMsg) {
Expand Down
19 changes: 7 additions & 12 deletions src/WSServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ const openers = new Map()

export default class WSServer {

constructor () {
this.server = null
}

static start (options, cb = () => {}, extraOptions) {
let WebSocket = {}
try {
Expand Down Expand Up @@ -38,7 +34,11 @@ export default class WSServer {
if (ioMsg.isToOpen()) {
open(socket, ioMsg)
} else if (ioMsg.isToJoin()) {
join(socket, ioMsg)
if (openers.has(ioMsg.key)) {
join(socket, ioMsg)
} else {
open(socket, ioMsg)
}
} else if (ioMsg.isToTransmitToOpener()) {
transmitToOpener(socket, ioMsg)
} else if (ioMsg.isToTransmitToJoining()) {
Expand All @@ -60,7 +60,6 @@ export default class WSServer {
console.log('Server has stopped successfully')
this.server.close(cb)
}

}

function errorOnSendCB (err) {
Expand Down Expand Up @@ -89,12 +88,8 @@ function open (socket, ioMsg) {
}

function join (socket, ioMsg) {
if (openers.has(ioMsg.key)) {
openers.get(ioMsg.key).values().next().value.addJoining(socket)
socket.send(IOJsonString.msgOpened(false), errorOnSendCB)
} else {
open(socket, ioMsg)
}
openers.get(ioMsg.key).values().next().value.addJoining(socket)
socket.send(IOJsonString.msgOpened(false), errorOnSendCB)
}

function transmitToJoining (socket, ioMsg) {
Expand Down

0 comments on commit d546298

Please sign in to comment.