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 socket = createWebsocket("/socket/grid")
const connectToDatasource = datasource => { const connectToDatasource = datasource => {
if (!socket.connected || datasource?.type !== "table") { if (!socket.connected) {
return return
} }
// Identify which table we are editing // Identify which table we are editing

View File

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