Skip to content

Commit

Permalink
fix: use enums as socket io events
Browse files Browse the repository at this point in the history
  • Loading branch information
sirily11 committed Feb 12, 2022
1 parent 5218d90 commit 3a405ad
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 39 deletions.
68 changes: 35 additions & 33 deletions packages/etd-services/src/socket-io/services/app_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,13 @@ export class APpService extends BaseSocketIOAuthService {
* @param socket
*/
joinRoomHandler: SocketHandler = (socket) => {
socket.on("join-room", async (roomId: string) => {
socket.on(enums.SocketIOEvents.joinRoom, async (roomId: string) => {
if (socket.rooms.size > 2) {
socket.emit("join-room-error", {
err: "You have already joined another room. You need to leave first!",
});
return;
}

Logger.info("Joining room " + roomId);
socket.join(roomId);
});
Expand All @@ -84,7 +83,7 @@ export class APpService extends BaseSocketIOAuthService {
* @param socket
*/
leaveRoomHandler: SocketHandler = (socket) => {
socket.on("leave-room", (roomId: string) => {
socket.on(enums.SocketIOEvents.leaveRoom, (roomId: string) => {
socket.leave(roomId);
});
};
Expand All @@ -95,7 +94,7 @@ export class APpService extends BaseSocketIOAuthService {
*/
rpcCommandHandler: SocketHandler = (socket) => {
socket.on(
"rpc-command",
enums.SocketIOEvents.rpcCommand,
async (command: enums.Web3ValueType, uuid: number | undefined) => {
const pendingJobPlugin = new PendingJobService();
const selectedRoom = this.canSubmitJob(socket);
Expand All @@ -120,7 +119,7 @@ export class APpService extends BaseSocketIOAuthService {
await pendingJobPlugin.create(job, {});
} else {
Logger.error("Cannot run rpc-command, not in any room!");
socket.emit("rpc-error", {
socket.emit(enums.SocketIOEvents.rpcError, {
err: "Cannot join the room. Not in any room!",
});
}
Expand All @@ -133,37 +132,40 @@ export class APpService extends BaseSocketIOAuthService {
* @param socket
*/
dockerCommandHandler: SocketHandler = (socket) => {
socket.on("docker-command", async (value: any, uuid: string) => {
console.log("Getting docker command", value, uuid);
const pendingJobPlugin = new PendingJobService();
// eslint-disable-next-line no-invalid-this
const selectedRoom = this.canSubmitJob(socket);
if (selectedRoom) {
const job = {
id: uuid,
targetDeviceId: selectedRoom,
from: socket.id,
time: new Date(),
task: {
type: enums.JobTaskType.Docker,
value: value,
},
};
/// Add id if user defined
if (uuid) {
socket.on(
enums.SocketIOEvents.dockerCommand,
async (value: any, uuid: string) => {
console.log("Getting docker command", value, uuid);
const pendingJobPlugin = new PendingJobService();
// eslint-disable-next-line no-invalid-this
const selectedRoom = this.canSubmitJob(socket);
if (selectedRoom) {
const job = {
id: uuid,
targetDeviceId: selectedRoom,
from: socket.id,
time: new Date(),
task: {
type: enums.JobTaskType.Docker,
value: value,
},
};
/// Add id if user defined
if (uuid) {
//@ts-ignore
// eslint-disable-next-line new-cap
job._id = mongoose.mongo.ObjectId(uuid);
}
//@ts-ignore
// eslint-disable-next-line new-cap
job._id = mongoose.mongo.ObjectId(uuid);
await pendingJobPlugin.create(job, {});
} else {
Logger.error("Cannot run docker-command, not in any room!");
socket.emit(enums.SocketIOEvents.dockerError, {
err: "Cannot join the room. Not in any room!",
});
}
//@ts-ignore
await pendingJobPlugin.create(job, {});
} else {
Logger.error("Cannot run docker-command, not in any room!");
socket.emit("docker-error", {
err: "Cannot join the room. Not in any room!",
});
}
});
);
};

handlePushUpdates: SocketHandler = (socket) => {
Expand Down
13 changes: 7 additions & 6 deletions packages/etd-services/src/socket-io/services/db_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,14 @@ export class DBChangeService extends BaseSocketIOService {
const clientService = this.findService<ClientService>(
enums.SocketIOServiceName.client
);
const executionPlanService = new ExecutionPlanService();
if (data.operationType === "insert" || data.operationType === "delete") {
const updateTemplateId = data.fullDocument!.updateTemplate;
Logger.info(`Execution plan ${updateTemplateId} has a update`);
clientService?.server
?.in(updateTemplateId)
.emit(enums.SocketIOEvents.executionPlan, "update");
const updateTemplateId = data.fullDocument?.updateTemplate;
if (updateTemplateId) {
Logger.info(`Execution plan ${updateTemplateId} has a update`);
clientService?.server
?.in(updateTemplateId)
.emit(enums.SocketIOEvents.executionPlan, "update");
}
}
});
}
Expand Down

0 comments on commit 3a405ad

Please sign in to comment.