Update grid socket middleware to support V2 views

This commit is contained in:
Andrew Kingston 2023-08-15 16:55:05 +01:00
parent 1ddd0bca21
commit a6da5fd2bb
2 changed files with 42 additions and 46 deletions

View File

@ -7,7 +7,7 @@ export const createGridWebsocket = context => {
const socket = createWebsocket("/socket/grid")
const connectToDatasource = datasource => {
if (!socket.connected || datasource?.type !== "table") {
if (!socket.connected) {
return
}
// Identify which table we are editing

View File

@ -20,23 +20,20 @@ export default class GridSocket extends BaseSocket {
async onConnect(socket: Socket) {
// Initial identification of connected spreadsheet
socket.on(
GridSocketEvent.SelectDatasource,
async ({ datasource, appId }, callback) => {
if (datasource?.type !== "table") {
return
}
const tableId = datasource.tableId
socket.on(GridSocketEvent.SelectDatasource, async (payload, callback) => {
const ds = payload.datasource
const appId = payload.appId
const resourceId = ds?.type === "table" ? ds?.tableId : ds?.id
// Ignore if no table or app specified
if (!tableId || !appId) {
if (!resourceId || !appId) {
socket.disconnect(true)
return
}
// Create context
const ctx = createContext(this.app, socket, {
resourceId: tableId,
resourceId,
appId,
})
@ -55,7 +52,7 @@ export default class GridSocket extends BaseSocket {
await runMiddlewares(ctx, middlewares, async () => {
// Middlewares are finished and we have permission
// Join room for this resource
const room = `${appId}-${tableId}`
const room = `${appId}-${resourceId}`
await this.joinRoom(socket, room)
// Reply with all users in current room
@ -65,8 +62,7 @@ export default class GridSocket extends BaseSocket {
} catch (error) {
socket.disconnect(true)
}
}
)
})
// Handle users selecting a new cell
socket.on(GridSocketEvent.SelectCell, ({ cellId }) => {