1
0
Fork 0
mirror of synced 2024-10-04 03:54:37 +13:00

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)
for (let table of result.tablesUpdated) {
builderSocket?.emitTableUpdate(ctx, table)
builderSocket?.emitTableUpdate(ctx, table, { includeSelf: true })
}
ctx.status = 200

View file

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

View file

@ -11,6 +11,12 @@ import { SocketSession } from "@budibase/types"
import { v4 as uuid } from "uuid"
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 = () => ({
_id: uuid(),
email: "user@mail.com",
@ -270,10 +276,17 @@ export class BaseSocket {
// Emit an event to everyone in a room, including metadata of whom
// the originator of the request was
emitToRoom(ctx: any, room: string | string[], event: string, payload: any) {
this.io.in(room).emit(event, {
...payload,
apiSessionId: ctx.headers?.[Header.SESSION_ID],
})
emitToRoom(
ctx: any,
room: string | string[],
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)
}
}