Remove plural forms of enums and use TS enum rather than JS const

This commit is contained in:
Andrew Kingston 2023-05-31 17:03:14 +01:00
parent 8a2d4b7052
commit 680e609b52
7 changed files with 46 additions and 53 deletions

View File

@ -1,14 +1,14 @@
import { createWebsocket } from "@budibase/frontend-core"
import { userStore } from "builderStore"
import { datasources, tables } from "stores/backend"
import { SocketEvents, BuilderSocketEvents } from "@budibase/shared-core"
import { SocketEvent, BuilderSocketEvent } from "@budibase/shared-core"
export const createBuilderWebsocket = appId => {
const socket = createWebsocket("/socket/builder")
// Built-in events
socket.on("connect", () => {
socket.emit(BuilderSocketEvents.SelectApp, appId, response => {
socket.emit(BuilderSocketEvent.SelectApp, appId, response => {
userStore.actions.init(response.users)
})
})
@ -17,16 +17,16 @@ export const createBuilderWebsocket = appId => {
})
// User events
socket.on(SocketEvents.UserUpdate, userStore.actions.updateUser)
socket.on(SocketEvents.UserDisconnect, userStore.actions.removeUser)
socket.on(SocketEvent.UserUpdate, userStore.actions.updateUser)
socket.on(SocketEvent.UserDisconnect, userStore.actions.removeUser)
// Table events
socket.on(BuilderSocketEvents.TableChange, ({ id, table }) => {
socket.on(BuilderSocketEvent.TableChange, ({ id, table }) => {
tables.replaceTable(id, table)
})
// Datasource events
socket.on(BuilderSocketEvents.DatasourceChange, ({ id, datasource }) => {
socket.on(BuilderSocketEvent.DatasourceChange, ({ id, datasource }) => {
datasources.replaceDatasource(id, datasource)
})

View File

@ -1,6 +1,6 @@
import { get } from "svelte/store"
import { createWebsocket } from "../../../utils"
import { SocketEvents, GridSocketEvents } from "@budibase/shared-core"
import { SocketEvent, GridSocketEvent } from "@budibase/shared-core"
export const createGridWebsocket = context => {
const { rows, tableId, users, focusedCellId, table } = context
@ -11,7 +11,7 @@ export const createGridWebsocket = context => {
return
}
// Identify which table we are editing
socket.emit(GridSocketEvents.SelectTable, tableId, response => {
socket.emit(GridSocketEvent.SelectTable, tableId, response => {
// handle initial connection info
users.set(response.users)
})
@ -26,15 +26,15 @@ export const createGridWebsocket = context => {
})
// User events
socket.on(SocketEvents.UserUpdate, user => {
socket.on(SocketEvent.UserUpdate, user => {
users.actions.updateUser(user)
})
socket.on(SocketEvents.UserDisconnect, user => {
socket.on(SocketEvent.UserDisconnect, user => {
users.actions.removeUser(user)
})
// Row events
socket.on(GridSocketEvents.RowChange, async data => {
socket.on(GridSocketEvent.RowChange, async data => {
if (data.id) {
rows.actions.replaceRow(data.id, data.row)
} else if (data.row.id) {
@ -44,7 +44,7 @@ export const createGridWebsocket = context => {
})
// Table events
socket.on(GridSocketEvents.TableChange, data => {
socket.on(GridSocketEvent.TableChange, data => {
// Only update table if one exists. If the table was deleted then we don't
// want to know - let the builder navigate away
if (data.table) {
@ -57,7 +57,7 @@ export const createGridWebsocket = context => {
// Notify selected cell changes
focusedCellId.subscribe($focusedCellId => {
socket.emit(GridSocketEvents.SelectCell, $focusedCellId)
socket.emit(GridSocketEvent.SelectCell, $focusedCellId)
})
return () => socket?.disconnect()

View File

@ -1,5 +1,5 @@
import { io } from "socket.io-client"
import { SocketEvents, SocketSessionTTL } from "@budibase/shared-core"
import { SocketEvent, SocketSessionTTL } from "@budibase/shared-core"
export const createWebsocket = (path, heartbeat = true) => {
if (!path) {
@ -29,7 +29,7 @@ export const createWebsocket = (path, heartbeat = true) => {
let interval
if (heartbeat) {
interval = setInterval(() => {
socket.emit(SocketEvents.Heartbeat)
socket.emit(SocketEvent.Heartbeat)
}, SocketSessionTTL * 500)
}

View File

@ -7,7 +7,7 @@ import { Datasource, Table, SocketSession, ContextUser } from "@budibase/types"
import { gridSocket } from "./index"
import { clearLock } from "../utilities/redis"
import { Socket } from "socket.io"
import { BuilderSocketEvents } from "@budibase/shared-core"
import { BuilderSocketEvent } from "@budibase/shared-core"
export default class BuilderSocket extends BaseSocket {
constructor(app: Koa, server: http.Server) {
@ -16,7 +16,7 @@ export default class BuilderSocket extends BaseSocket {
async onConnect(socket: Socket) {
// Initial identification of selected app
socket.on(BuilderSocketEvents.SelectApp, async (appId, callback) => {
socket.on(BuilderSocketEvent.SelectApp, async (appId, callback) => {
await this.joinRoom(socket, appId)
// Reply with all users in current room
@ -48,19 +48,19 @@ export default class BuilderSocket extends BaseSocket {
emitTableUpdate(ctx: any, table: Table) {
this.io
.in(ctx.appId)
.emit(BuilderSocketEvents.TableChange, { id: table._id, table })
.emit(BuilderSocketEvent.TableChange, { id: table._id, table })
gridSocket.emitTableUpdate(table)
}
emitTableDeletion(ctx: any, id: string) {
this.io
.in(ctx.appId)
.emit(BuilderSocketEvents.TableChange, { id, table: null })
.emit(BuilderSocketEvent.TableChange, { id, table: null })
gridSocket.emitTableDeletion(id)
}
emitDatasourceUpdate(ctx: any, datasource: Datasource) {
this.io.in(ctx.appId).emit(BuilderSocketEvents.DatasourceChange, {
this.io.in(ctx.appId).emit(BuilderSocketEvent.DatasourceChange, {
id: datasource._id,
datasource,
})
@ -69,6 +69,6 @@ export default class BuilderSocket extends BaseSocket {
emitDatasourceDeletion(ctx: any, id: string) {
this.io
.in(ctx.appId)
.emit(BuilderSocketEvents.DatasourceChange, { id, datasource: null })
.emit(BuilderSocketEvent.DatasourceChange, { id, datasource: null })
}
}

View File

@ -6,7 +6,7 @@ import Koa from "koa"
import { getTableId } from "../api/controllers/row/utils"
import { Row, Table } from "@budibase/types"
import { Socket } from "socket.io"
import { GridSocketEvents } from "@budibase/shared-core"
import { GridSocketEvent } from "@budibase/shared-core"
export default class GridSocket extends BaseSocket {
constructor(app: Koa, server: http.Server) {
@ -15,7 +15,7 @@ export default class GridSocket extends BaseSocket {
async onConnect(socket: Socket) {
// Initial identification of connected spreadsheet
socket.on(GridSocketEvents.SelectTable, async (tableId, callback) => {
socket.on(GridSocketEvent.SelectTable, async (tableId, callback) => {
await this.joinRoom(socket, tableId)
// Reply with all users in current room
@ -24,28 +24,28 @@ export default class GridSocket extends BaseSocket {
})
// Handle users selecting a new cell
socket.on(GridSocketEvents.SelectCell, cellId => {
socket.on(GridSocketEvent.SelectCell, cellId => {
this.updateUser(socket, { focusedCellId: cellId })
})
}
emitRowUpdate(ctx: any, row: Row) {
const tableId = getTableId(ctx)
this.io.in(tableId).emit(GridSocketEvents.RowChange, { id: row._id, row })
this.io.in(tableId).emit(GridSocketEvent.RowChange, { id: row._id, row })
}
emitRowDeletion(ctx: any, id: string) {
const tableId = getTableId(ctx)
this.io.in(tableId).emit(GridSocketEvents.RowChange, { id, row: null })
this.io.in(tableId).emit(GridSocketEvent.RowChange, { id, row: null })
}
emitTableUpdate(table: Table) {
this.io
.in(table._id!)
.emit(GridSocketEvents.TableChange, { id: table._id, table })
.emit(GridSocketEvent.TableChange, { id: table._id, table })
}
emitTableDeletion(id: string) {
this.io.in(id).emit(GridSocketEvents.TableChange, { id, table: null })
this.io.in(id).emit(GridSocketEvent.TableChange, { id, table: null })
}
}

View File

@ -8,7 +8,7 @@ import currentApp from "../middleware/currentapp"
import { createAdapter } from "@socket.io/redis-adapter"
import { Socket } from "socket.io"
import { getSocketPubSubClients } from "../utilities/redis"
import { SocketEvents, SocketSessionTTL } from "@budibase/shared-core"
import { SocketEvent, SocketSessionTTL } from "@budibase/shared-core"
import { SocketSession } from "@budibase/types"
export class BaseSocket {
@ -90,14 +90,8 @@ export class BaseSocket {
// Initialise redis before handling connections
this.initialise().then(() => {
this.io.on("connection", async socket => {
// Add built in handler to allow fetching all other users in this room
socket.on(SocketEvents.GetUsers, async (payload, callback) => {
const sessions = await this.getRoomSessions(socket.data.room)
callback({ users: sessions })
})
// Add built in handler for heartbeats
socket.on(SocketEvents.Heartbeat, async () => {
socket.on(SocketEvent.Heartbeat, async () => {
console.log(socket.data.email, "heartbeat received")
await this.extendSessionTTL(socket.data.sessionId)
})
@ -180,7 +174,7 @@ export class BaseSocket {
)
const prunedSessionIds = sessionIds.filter((id, idx) => {
if (!sessionsExist[idx]) {
this.io.to(room).emit(SocketEvents.UserDisconnect, sessionIds[idx])
this.io.to(room).emit(SocketEvent.UserDisconnect, sessionIds[idx])
return false
}
return true
@ -223,7 +217,7 @@ export class BaseSocket {
}
// Notify other users
socket.to(room).emit(SocketEvents.UserUpdate, user)
socket.to(room).emit(SocketEvent.UserUpdate, user)
}
// Disconnects a socket from its current room
@ -249,7 +243,7 @@ export class BaseSocket {
)
// Notify other users
socket.to(room).emit(SocketEvents.UserDisconnect, sessionId)
socket.to(room).emit(SocketEvent.UserDisconnect, sessionId)
}
// Updates a connected user's metadata, assuming a room change is not required.

View File

@ -68,24 +68,23 @@ export const SqlNumberTypeRangeMap = {
},
}
export const SocketEvents = {
UserUpdate: "UserUpdate",
UserDisconnect: "UserDisconnect",
GetUsers: "GetUsers",
Heartbeat: "Heartbeat",
export enum SocketEvent {
UserUpdate = "UserUpdate",
UserDisconnect = "UserDisconnect",
Heartbeat = "Heartbeat",
}
export const GridSocketEvents = {
RowChange: "RowChange",
TableChange: "TableChange",
SelectTable: "SelectTable",
SelectCell: "SelectCell",
export enum GridSocketEvent {
RowChange = "RowChange",
TableChange = "TableChange",
SelectTable = "SelectTable",
SelectCell = "SelectCell",
}
export const BuilderSocketEvents = {
SelectApp: "SelectApp",
TableChange: "TableChange",
DatasourceChange: "DatasourceChange",
export enum BuilderSocketEvent {
SelectApp = "SelectApp",
TableChange = "TableChange",
DatasourceChange = "DatasourceChange",
}
export const SocketSessionTTL = 60