Attempt to emit events that include the originator.

This commit is contained in:
Sam Rose 2023-10-25 11:38:31 +01:00
parent 78afba63de
commit 52f97fbd1f
No known key found for this signature in database
3 changed files with 31 additions and 12 deletions

View File

@ -168,7 +168,7 @@ export async function migrate(ctx: UserCtx<MigrateRequest, MigrateResponse>) {
let result = await sdk.tables.migrate(table, oldColumn, newColumn) let result = await sdk.tables.migrate(table, oldColumn, newColumn)
for (let table of result.tablesUpdated) { for (let table of result.tablesUpdated) {
builderSocket?.emitTableUpdate(ctx, table) builderSocket?.emitTableUpdate(ctx, table, { includeSelf: true })
} }
ctx.status = 200 ctx.status = 200

View File

@ -1,5 +1,5 @@
import authorized from "../middleware/authorized" import authorized from "../middleware/authorized"
import { BaseSocket } from "./websocket" import { BaseSocket, EmitOptions } from "./websocket"
import { permissions, events, context } from "@budibase/backend-core" import { permissions, events, context } from "@budibase/backend-core"
import http from "http" import http from "http"
import Koa from "koa" import Koa from "koa"
@ -100,11 +100,17 @@ export default class BuilderSocket extends BaseSocket {
}) })
} }
emitTableUpdate(ctx: any, table: Table) { emitTableUpdate(ctx: any, table: Table, options?: EmitOptions) {
this.emitToRoom(ctx, ctx.appId, BuilderSocketEvent.TableChange, { this.emitToRoom(
id: table._id, ctx,
table, ctx.appId,
}) BuilderSocketEvent.TableChange,
{
id: table._id,
table,
},
options
)
gridSocket?.emitTableUpdate(ctx, table) gridSocket?.emitTableUpdate(ctx, table)
} }

View File

@ -11,6 +11,12 @@ import { SocketSession } from "@budibase/types"
import { v4 as uuid } from "uuid" import { v4 as uuid } from "uuid"
import { createContext, runMiddlewares } from "./middleware" import { createContext, runMiddlewares } from "./middleware"
export interface EmitOptions {
// Whether to include the originator of the request from the broadcast,
// defaults to false.
includeSelf?: boolean
}
const anonUser = () => ({ const anonUser = () => ({
_id: uuid(), _id: uuid(),
email: "user@mail.com", email: "user@mail.com",
@ -270,10 +276,17 @@ export class BaseSocket {
// Emit an event to everyone in a room, including metadata of whom // Emit an event to everyone in a room, including metadata of whom
// the originator of the request was // the originator of the request was
emitToRoom(ctx: any, room: string | string[], event: string, payload: any) { emitToRoom(
this.io.in(room).emit(event, { ctx: any,
...payload, room: string | string[],
apiSessionId: ctx.headers?.[Header.SESSION_ID], event: string,
}) payload: any,
options?: EmitOptions
) {
let emitPayload = { ...payload }
if (!options?.includeSelf) {
emitPayload.apiSessionId = ctx.headers?.[Header.SESSION_ID]
}
this.io.in(room).emit(event, emitPayload)
} }
} }