Remove plural forms of enums and use TS enum rather than JS const
This commit is contained in:
parent
8a2d4b7052
commit
680e609b52
|
@ -1,14 +1,14 @@
|
||||||
import { createWebsocket } from "@budibase/frontend-core"
|
import { createWebsocket } from "@budibase/frontend-core"
|
||||||
import { userStore } from "builderStore"
|
import { userStore } from "builderStore"
|
||||||
import { datasources, tables } from "stores/backend"
|
import { datasources, tables } from "stores/backend"
|
||||||
import { SocketEvents, BuilderSocketEvents } from "@budibase/shared-core"
|
import { SocketEvent, BuilderSocketEvent } from "@budibase/shared-core"
|
||||||
|
|
||||||
export const createBuilderWebsocket = appId => {
|
export const createBuilderWebsocket = appId => {
|
||||||
const socket = createWebsocket("/socket/builder")
|
const socket = createWebsocket("/socket/builder")
|
||||||
|
|
||||||
// Built-in events
|
// Built-in events
|
||||||
socket.on("connect", () => {
|
socket.on("connect", () => {
|
||||||
socket.emit(BuilderSocketEvents.SelectApp, appId, response => {
|
socket.emit(BuilderSocketEvent.SelectApp, appId, response => {
|
||||||
userStore.actions.init(response.users)
|
userStore.actions.init(response.users)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -17,16 +17,16 @@ export const createBuilderWebsocket = appId => {
|
||||||
})
|
})
|
||||||
|
|
||||||
// User events
|
// User events
|
||||||
socket.on(SocketEvents.UserUpdate, userStore.actions.updateUser)
|
socket.on(SocketEvent.UserUpdate, userStore.actions.updateUser)
|
||||||
socket.on(SocketEvents.UserDisconnect, userStore.actions.removeUser)
|
socket.on(SocketEvent.UserDisconnect, userStore.actions.removeUser)
|
||||||
|
|
||||||
// Table events
|
// Table events
|
||||||
socket.on(BuilderSocketEvents.TableChange, ({ id, table }) => {
|
socket.on(BuilderSocketEvent.TableChange, ({ id, table }) => {
|
||||||
tables.replaceTable(id, table)
|
tables.replaceTable(id, table)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Datasource events
|
// Datasource events
|
||||||
socket.on(BuilderSocketEvents.DatasourceChange, ({ id, datasource }) => {
|
socket.on(BuilderSocketEvent.DatasourceChange, ({ id, datasource }) => {
|
||||||
datasources.replaceDatasource(id, datasource)
|
datasources.replaceDatasource(id, datasource)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { get } from "svelte/store"
|
import { get } from "svelte/store"
|
||||||
import { createWebsocket } from "../../../utils"
|
import { createWebsocket } from "../../../utils"
|
||||||
import { SocketEvents, GridSocketEvents } from "@budibase/shared-core"
|
import { SocketEvent, GridSocketEvent } from "@budibase/shared-core"
|
||||||
|
|
||||||
export const createGridWebsocket = context => {
|
export const createGridWebsocket = context => {
|
||||||
const { rows, tableId, users, focusedCellId, table } = context
|
const { rows, tableId, users, focusedCellId, table } = context
|
||||||
|
@ -11,7 +11,7 @@ export const createGridWebsocket = context => {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Identify which table we are editing
|
// Identify which table we are editing
|
||||||
socket.emit(GridSocketEvents.SelectTable, tableId, response => {
|
socket.emit(GridSocketEvent.SelectTable, tableId, response => {
|
||||||
// handle initial connection info
|
// handle initial connection info
|
||||||
users.set(response.users)
|
users.set(response.users)
|
||||||
})
|
})
|
||||||
|
@ -26,15 +26,15 @@ export const createGridWebsocket = context => {
|
||||||
})
|
})
|
||||||
|
|
||||||
// User events
|
// User events
|
||||||
socket.on(SocketEvents.UserUpdate, user => {
|
socket.on(SocketEvent.UserUpdate, user => {
|
||||||
users.actions.updateUser(user)
|
users.actions.updateUser(user)
|
||||||
})
|
})
|
||||||
socket.on(SocketEvents.UserDisconnect, user => {
|
socket.on(SocketEvent.UserDisconnect, user => {
|
||||||
users.actions.removeUser(user)
|
users.actions.removeUser(user)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Row events
|
// Row events
|
||||||
socket.on(GridSocketEvents.RowChange, async data => {
|
socket.on(GridSocketEvent.RowChange, async data => {
|
||||||
if (data.id) {
|
if (data.id) {
|
||||||
rows.actions.replaceRow(data.id, data.row)
|
rows.actions.replaceRow(data.id, data.row)
|
||||||
} else if (data.row.id) {
|
} else if (data.row.id) {
|
||||||
|
@ -44,7 +44,7 @@ export const createGridWebsocket = context => {
|
||||||
})
|
})
|
||||||
|
|
||||||
// Table events
|
// 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
|
// Only update table if one exists. If the table was deleted then we don't
|
||||||
// want to know - let the builder navigate away
|
// want to know - let the builder navigate away
|
||||||
if (data.table) {
|
if (data.table) {
|
||||||
|
@ -57,7 +57,7 @@ export const createGridWebsocket = context => {
|
||||||
|
|
||||||
// Notify selected cell changes
|
// Notify selected cell changes
|
||||||
focusedCellId.subscribe($focusedCellId => {
|
focusedCellId.subscribe($focusedCellId => {
|
||||||
socket.emit(GridSocketEvents.SelectCell, $focusedCellId)
|
socket.emit(GridSocketEvent.SelectCell, $focusedCellId)
|
||||||
})
|
})
|
||||||
|
|
||||||
return () => socket?.disconnect()
|
return () => socket?.disconnect()
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { io } from "socket.io-client"
|
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) => {
|
export const createWebsocket = (path, heartbeat = true) => {
|
||||||
if (!path) {
|
if (!path) {
|
||||||
|
@ -29,7 +29,7 @@ export const createWebsocket = (path, heartbeat = true) => {
|
||||||
let interval
|
let interval
|
||||||
if (heartbeat) {
|
if (heartbeat) {
|
||||||
interval = setInterval(() => {
|
interval = setInterval(() => {
|
||||||
socket.emit(SocketEvents.Heartbeat)
|
socket.emit(SocketEvent.Heartbeat)
|
||||||
}, SocketSessionTTL * 500)
|
}, SocketSessionTTL * 500)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ import { Datasource, Table, SocketSession, ContextUser } from "@budibase/types"
|
||||||
import { gridSocket } from "./index"
|
import { gridSocket } from "./index"
|
||||||
import { clearLock } from "../utilities/redis"
|
import { clearLock } from "../utilities/redis"
|
||||||
import { Socket } from "socket.io"
|
import { Socket } from "socket.io"
|
||||||
import { BuilderSocketEvents } from "@budibase/shared-core"
|
import { BuilderSocketEvent } from "@budibase/shared-core"
|
||||||
|
|
||||||
export default class BuilderSocket extends BaseSocket {
|
export default class BuilderSocket extends BaseSocket {
|
||||||
constructor(app: Koa, server: http.Server) {
|
constructor(app: Koa, server: http.Server) {
|
||||||
|
@ -16,7 +16,7 @@ export default class BuilderSocket extends BaseSocket {
|
||||||
|
|
||||||
async onConnect(socket: Socket) {
|
async onConnect(socket: Socket) {
|
||||||
// Initial identification of selected app
|
// Initial identification of selected app
|
||||||
socket.on(BuilderSocketEvents.SelectApp, async (appId, callback) => {
|
socket.on(BuilderSocketEvent.SelectApp, async (appId, callback) => {
|
||||||
await this.joinRoom(socket, appId)
|
await this.joinRoom(socket, appId)
|
||||||
|
|
||||||
// Reply with all users in current room
|
// Reply with all users in current room
|
||||||
|
@ -48,19 +48,19 @@ export default class BuilderSocket extends BaseSocket {
|
||||||
emitTableUpdate(ctx: any, table: Table) {
|
emitTableUpdate(ctx: any, table: Table) {
|
||||||
this.io
|
this.io
|
||||||
.in(ctx.appId)
|
.in(ctx.appId)
|
||||||
.emit(BuilderSocketEvents.TableChange, { id: table._id, table })
|
.emit(BuilderSocketEvent.TableChange, { id: table._id, table })
|
||||||
gridSocket.emitTableUpdate(table)
|
gridSocket.emitTableUpdate(table)
|
||||||
}
|
}
|
||||||
|
|
||||||
emitTableDeletion(ctx: any, id: string) {
|
emitTableDeletion(ctx: any, id: string) {
|
||||||
this.io
|
this.io
|
||||||
.in(ctx.appId)
|
.in(ctx.appId)
|
||||||
.emit(BuilderSocketEvents.TableChange, { id, table: null })
|
.emit(BuilderSocketEvent.TableChange, { id, table: null })
|
||||||
gridSocket.emitTableDeletion(id)
|
gridSocket.emitTableDeletion(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
emitDatasourceUpdate(ctx: any, datasource: Datasource) {
|
emitDatasourceUpdate(ctx: any, datasource: Datasource) {
|
||||||
this.io.in(ctx.appId).emit(BuilderSocketEvents.DatasourceChange, {
|
this.io.in(ctx.appId).emit(BuilderSocketEvent.DatasourceChange, {
|
||||||
id: datasource._id,
|
id: datasource._id,
|
||||||
datasource,
|
datasource,
|
||||||
})
|
})
|
||||||
|
@ -69,6 +69,6 @@ export default class BuilderSocket extends BaseSocket {
|
||||||
emitDatasourceDeletion(ctx: any, id: string) {
|
emitDatasourceDeletion(ctx: any, id: string) {
|
||||||
this.io
|
this.io
|
||||||
.in(ctx.appId)
|
.in(ctx.appId)
|
||||||
.emit(BuilderSocketEvents.DatasourceChange, { id, datasource: null })
|
.emit(BuilderSocketEvent.DatasourceChange, { id, datasource: null })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ import Koa from "koa"
|
||||||
import { getTableId } from "../api/controllers/row/utils"
|
import { getTableId } from "../api/controllers/row/utils"
|
||||||
import { Row, Table } from "@budibase/types"
|
import { Row, Table } from "@budibase/types"
|
||||||
import { Socket } from "socket.io"
|
import { Socket } from "socket.io"
|
||||||
import { GridSocketEvents } from "@budibase/shared-core"
|
import { GridSocketEvent } from "@budibase/shared-core"
|
||||||
|
|
||||||
export default class GridSocket extends BaseSocket {
|
export default class GridSocket extends BaseSocket {
|
||||||
constructor(app: Koa, server: http.Server) {
|
constructor(app: Koa, server: http.Server) {
|
||||||
|
@ -15,7 +15,7 @@ export default class GridSocket extends BaseSocket {
|
||||||
|
|
||||||
async onConnect(socket: Socket) {
|
async onConnect(socket: Socket) {
|
||||||
// Initial identification of connected spreadsheet
|
// Initial identification of connected spreadsheet
|
||||||
socket.on(GridSocketEvents.SelectTable, async (tableId, callback) => {
|
socket.on(GridSocketEvent.SelectTable, async (tableId, callback) => {
|
||||||
await this.joinRoom(socket, tableId)
|
await this.joinRoom(socket, tableId)
|
||||||
|
|
||||||
// Reply with all users in current room
|
// Reply with all users in current room
|
||||||
|
@ -24,28 +24,28 @@ export default class GridSocket extends BaseSocket {
|
||||||
})
|
})
|
||||||
|
|
||||||
// Handle users selecting a new cell
|
// Handle users selecting a new cell
|
||||||
socket.on(GridSocketEvents.SelectCell, cellId => {
|
socket.on(GridSocketEvent.SelectCell, cellId => {
|
||||||
this.updateUser(socket, { focusedCellId: cellId })
|
this.updateUser(socket, { focusedCellId: cellId })
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
emitRowUpdate(ctx: any, row: Row) {
|
emitRowUpdate(ctx: any, row: Row) {
|
||||||
const tableId = getTableId(ctx)
|
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) {
|
emitRowDeletion(ctx: any, id: string) {
|
||||||
const tableId = getTableId(ctx)
|
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) {
|
emitTableUpdate(table: Table) {
|
||||||
this.io
|
this.io
|
||||||
.in(table._id!)
|
.in(table._id!)
|
||||||
.emit(GridSocketEvents.TableChange, { id: table._id, table })
|
.emit(GridSocketEvent.TableChange, { id: table._id, table })
|
||||||
}
|
}
|
||||||
|
|
||||||
emitTableDeletion(id: string) {
|
emitTableDeletion(id: string) {
|
||||||
this.io.in(id).emit(GridSocketEvents.TableChange, { id, table: null })
|
this.io.in(id).emit(GridSocketEvent.TableChange, { id, table: null })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ import currentApp from "../middleware/currentapp"
|
||||||
import { createAdapter } from "@socket.io/redis-adapter"
|
import { createAdapter } from "@socket.io/redis-adapter"
|
||||||
import { Socket } from "socket.io"
|
import { Socket } from "socket.io"
|
||||||
import { getSocketPubSubClients } from "../utilities/redis"
|
import { getSocketPubSubClients } from "../utilities/redis"
|
||||||
import { SocketEvents, SocketSessionTTL } from "@budibase/shared-core"
|
import { SocketEvent, SocketSessionTTL } from "@budibase/shared-core"
|
||||||
import { SocketSession } from "@budibase/types"
|
import { SocketSession } from "@budibase/types"
|
||||||
|
|
||||||
export class BaseSocket {
|
export class BaseSocket {
|
||||||
|
@ -90,14 +90,8 @@ export class BaseSocket {
|
||||||
// Initialise redis before handling connections
|
// Initialise redis before handling connections
|
||||||
this.initialise().then(() => {
|
this.initialise().then(() => {
|
||||||
this.io.on("connection", async socket => {
|
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
|
// Add built in handler for heartbeats
|
||||||
socket.on(SocketEvents.Heartbeat, async () => {
|
socket.on(SocketEvent.Heartbeat, async () => {
|
||||||
console.log(socket.data.email, "heartbeat received")
|
console.log(socket.data.email, "heartbeat received")
|
||||||
await this.extendSessionTTL(socket.data.sessionId)
|
await this.extendSessionTTL(socket.data.sessionId)
|
||||||
})
|
})
|
||||||
|
@ -180,7 +174,7 @@ export class BaseSocket {
|
||||||
)
|
)
|
||||||
const prunedSessionIds = sessionIds.filter((id, idx) => {
|
const prunedSessionIds = sessionIds.filter((id, idx) => {
|
||||||
if (!sessionsExist[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 false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
|
@ -223,7 +217,7 @@ export class BaseSocket {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Notify other users
|
// Notify other users
|
||||||
socket.to(room).emit(SocketEvents.UserUpdate, user)
|
socket.to(room).emit(SocketEvent.UserUpdate, user)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Disconnects a socket from its current room
|
// Disconnects a socket from its current room
|
||||||
|
@ -249,7 +243,7 @@ export class BaseSocket {
|
||||||
)
|
)
|
||||||
|
|
||||||
// Notify other users
|
// 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.
|
// Updates a connected user's metadata, assuming a room change is not required.
|
||||||
|
|
|
@ -68,24 +68,23 @@ export const SqlNumberTypeRangeMap = {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
export const SocketEvents = {
|
export enum SocketEvent {
|
||||||
UserUpdate: "UserUpdate",
|
UserUpdate = "UserUpdate",
|
||||||
UserDisconnect: "UserDisconnect",
|
UserDisconnect = "UserDisconnect",
|
||||||
GetUsers: "GetUsers",
|
Heartbeat = "Heartbeat",
|
||||||
Heartbeat: "Heartbeat",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const GridSocketEvents = {
|
export enum GridSocketEvent {
|
||||||
RowChange: "RowChange",
|
RowChange = "RowChange",
|
||||||
TableChange: "TableChange",
|
TableChange = "TableChange",
|
||||||
SelectTable: "SelectTable",
|
SelectTable = "SelectTable",
|
||||||
SelectCell: "SelectCell",
|
SelectCell = "SelectCell",
|
||||||
}
|
}
|
||||||
|
|
||||||
export const BuilderSocketEvents = {
|
export enum BuilderSocketEvent {
|
||||||
SelectApp: "SelectApp",
|
SelectApp = "SelectApp",
|
||||||
TableChange: "TableChange",
|
TableChange = "TableChange",
|
||||||
DatasourceChange: "DatasourceChange",
|
DatasourceChange = "DatasourceChange",
|
||||||
}
|
}
|
||||||
|
|
||||||
export const SocketSessionTTL = 60
|
export const SocketSessionTTL = 60
|
||||||
|
|
Loading…
Reference in New Issue