Revert websocket changes and just fetch datasources constantly

This commit is contained in:
Andrew Kingston 2023-06-15 09:27:45 +01:00
parent 5e5dc902d1
commit 99a8fc7c12
9 changed files with 63 additions and 124 deletions

View File

@ -1,5 +1,5 @@
<script>
import { tables } from "stores/backend"
import { datasources, tables } from "stores/backend"
import EditRolesButton from "./buttons/EditRolesButton.svelte"
import { TableNames } from "constants"
import { Grid } from "@budibase/frontend-core"
@ -26,6 +26,15 @@
$: id = $tables.selected?._id
$: isUsersTable = id === TableNames.USERS
$: isInternal = $tables.selected?.type !== "external"
const handleGridTableUpdate = async e => {
tables.replaceTable(id, e.detail)
// We need to refresh datasources when an external table changes
if (e.detail?.type === "external") {
await datasources.fetch()
}
}
</script>
<div class="wrapper">
@ -37,7 +46,7 @@
allowDeleteRows={!isUsersTable}
schemaOverrides={isUsersTable ? userSchemaOverrides : null}
showAvatars={false}
on:updatetable={e => tables.replaceTable(id, e.detail)}
on:updatetable={handleGridTableUpdate}
>
<svelte:fragment slot="controls">
{#if isInternal}

View File

@ -93,6 +93,7 @@
try {
await beforeSave()
table = await tables.save(newTable)
await datasources.fetch()
await afterSave(table)
} catch (e) {
notifications.error(e)

View File

@ -65,6 +65,7 @@
const updatedTable = cloneDeep(table)
updatedTable.name = updatedName
await tables.save(updatedTable)
await datasources.fetch()
notifications.success("Table renamed successfully")
}

View File

@ -90,12 +90,12 @@ export const deriveStores = context => {
// Update local state
table.set(newTable)
// Update server
await API.saveTable(newTable)
// Broadcast change to external state can be updated, as this change
// will not be received by the builder websocket because we caused it ourselves
dispatch("updatetable", newTable)
// Update server
await API.saveTable(newTable)
}
return {

View File

@ -322,9 +322,7 @@ export async function save(ctx: UserCtx) {
// Since tables are stored inside datasources, we need to notify clients
// that the datasource definition changed
const updatedDatasource = await db.get(datasource._id)
builderSocket?.emitDatasourceUpdate(ctx, updatedDatasource, {
includeOriginator: true,
})
builderSocket?.emitDatasourceUpdate(ctx, updatedDatasource)
return tableToSave
}
@ -352,6 +350,11 @@ export async function destroy(ctx: UserCtx) {
await db.put(datasource)
// Since tables are stored inside datasources, we need to notify clients
// that the datasource definition changed
const updatedDatasource = await db.get(datasource._id)
builderSocket?.emitDatasourceUpdate(ctx, updatedDatasource)
return tableToDelete
}

View File

@ -3,13 +3,7 @@ import { BaseSocket } from "./websocket"
import { permissions, events } from "@budibase/backend-core"
import http from "http"
import Koa from "koa"
import {
Datasource,
Table,
SocketSession,
ContextUser,
SocketMessageOptions,
} from "@budibase/types"
import { Datasource, Table, SocketSession, ContextUser } from "@budibase/types"
import { gridSocket } from "./index"
import { clearLock, updateLock } from "../utilities/redis"
import { Socket } from "socket.io"
@ -72,61 +66,33 @@ export default class BuilderSocket extends BaseSocket {
}
}
emitTableUpdate(ctx: any, table: Table, options?: SocketMessageOptions) {
this.emitToRoom(
ctx,
ctx.appId,
BuilderSocketEvent.TableChange,
{
emitTableUpdate(ctx: any, table: Table) {
this.emitToRoom(ctx, ctx.appId, BuilderSocketEvent.TableChange, {
id: table._id,
table,
},
options
)
gridSocket?.emitTableUpdate(ctx, table, options)
})
gridSocket?.emitTableUpdate(ctx, table)
}
emitTableDeletion(ctx: any, id: string, options?: SocketMessageOptions) {
this.emitToRoom(
ctx,
ctx.appId,
BuilderSocketEvent.TableChange,
{
emitTableDeletion(ctx: any, id: string) {
this.emitToRoom(ctx, ctx.appId, BuilderSocketEvent.TableChange, {
id,
table: null,
},
options
)
gridSocket?.emitTableDeletion(ctx, id, options)
})
gridSocket?.emitTableDeletion(ctx, id)
}
emitDatasourceUpdate(
ctx: any,
datasource: Datasource,
options?: SocketMessageOptions
) {
this.emitToRoom(
ctx,
ctx.appId,
BuilderSocketEvent.DatasourceChange,
{
emitDatasourceUpdate(ctx: any, datasource: Datasource) {
this.emitToRoom(ctx, ctx.appId, BuilderSocketEvent.DatasourceChange, {
id: datasource._id,
datasource,
},
options
)
})
}
emitDatasourceDeletion(ctx: any, id: string, options?: SocketMessageOptions) {
this.emitToRoom(
ctx,
ctx.appId,
BuilderSocketEvent.DatasourceChange,
{
emitDatasourceDeletion(ctx: any, id: string) {
this.emitToRoom(ctx, ctx.appId, BuilderSocketEvent.DatasourceChange, {
id,
datasource: null,
},
options
)
})
}
}

View File

@ -4,7 +4,7 @@ import { permissions } from "@budibase/backend-core"
import http from "http"
import Koa from "koa"
import { getTableId } from "../api/controllers/row/utils"
import { Row, SocketMessageOptions, Table } from "@budibase/types"
import { Row, Table } from "@budibase/types"
import { Socket } from "socket.io"
import { GridSocketEvent } from "@budibase/shared-core"
@ -29,51 +29,27 @@ export default class GridSocket extends BaseSocket {
})
}
emitRowUpdate(ctx: any, row: Row, options?: SocketMessageOptions) {
emitRowUpdate(ctx: any, row: Row) {
const tableId = getTableId(ctx)
this.emitToRoom(
ctx,
tableId,
GridSocketEvent.RowChange,
{
this.emitToRoom(ctx, tableId, GridSocketEvent.RowChange, {
id: row._id,
row,
},
options
)
})
}
emitRowDeletion(ctx: any, id: string, options?: SocketMessageOptions) {
emitRowDeletion(ctx: any, id: string) {
const tableId = getTableId(ctx)
this.emitToRoom(
ctx,
tableId,
GridSocketEvent.RowChange,
{ id, row: null },
options
)
this.emitToRoom(ctx, tableId, GridSocketEvent.RowChange, { id, row: null })
}
emitTableUpdate(ctx: any, table: Table, options?: SocketMessageOptions) {
this.emitToRoom(
ctx,
table._id!,
GridSocketEvent.TableChange,
{
emitTableUpdate(ctx: any, table: Table) {
this.emitToRoom(ctx, table._id!, GridSocketEvent.TableChange, {
id: table._id,
table,
},
options
)
})
}
emitTableDeletion(ctx: any, id: string, options?: SocketMessageOptions) {
this.emitToRoom(
ctx,
id,
GridSocketEvent.TableChange,
{ id, table: null },
options
)
emitTableDeletion(ctx: any, id: string) {
this.emitToRoom(ctx, id, GridSocketEvent.TableChange, { id, table: null })
}
}

View File

@ -9,7 +9,7 @@ import { createAdapter } from "@socket.io/redis-adapter"
import { Socket } from "socket.io"
import { getSocketPubSubClients } from "../utilities/redis"
import { SocketEvent, SocketSessionTTL } from "@budibase/shared-core"
import { SocketSession, SocketMessageOptions } from "@budibase/types"
import { SocketSession } from "@budibase/types"
export class BaseSocket {
io: Server
@ -277,23 +277,10 @@ export class BaseSocket {
}
// Emit an event to everyone in a room
emitToRoom(
ctx: any,
room: string,
event: string,
payload: any,
options?: SocketMessageOptions
) {
// By default, we include the session API of the originator so that they can ignore
// this event. If we want to include the originator then we leave it unset to that all
// clients will react to it.
let apiSessionId = null
if (!options?.includeOriginator) {
apiSessionId = ctx.headers?.[Header.SESSION_ID]
}
emitToRoom(ctx: any, room: string, event: string, payload: any) {
this.io.in(room).emit(event, {
...payload,
apiSessionId,
apiSessionId: ctx.headers?.[Header.SESSION_ID],
})
}
}

View File

@ -7,7 +7,3 @@ export interface SocketSession {
room?: string
connectedAt: number
}
export interface SocketMessageOptions {
includeOriginator?: boolean
}