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> <script>
import { tables } from "stores/backend" import { datasources, tables } from "stores/backend"
import EditRolesButton from "./buttons/EditRolesButton.svelte" import EditRolesButton from "./buttons/EditRolesButton.svelte"
import { TableNames } from "constants" import { TableNames } from "constants"
import { Grid } from "@budibase/frontend-core" import { Grid } from "@budibase/frontend-core"
@ -26,6 +26,15 @@
$: id = $tables.selected?._id $: id = $tables.selected?._id
$: isUsersTable = id === TableNames.USERS $: isUsersTable = id === TableNames.USERS
$: isInternal = $tables.selected?.type !== "external" $: 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> </script>
<div class="wrapper"> <div class="wrapper">
@ -37,7 +46,7 @@
allowDeleteRows={!isUsersTable} allowDeleteRows={!isUsersTable}
schemaOverrides={isUsersTable ? userSchemaOverrides : null} schemaOverrides={isUsersTable ? userSchemaOverrides : null}
showAvatars={false} showAvatars={false}
on:updatetable={e => tables.replaceTable(id, e.detail)} on:updatetable={handleGridTableUpdate}
> >
<svelte:fragment slot="controls"> <svelte:fragment slot="controls">
{#if isInternal} {#if isInternal}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -4,7 +4,7 @@ import { permissions } from "@budibase/backend-core"
import http from "http" import http from "http"
import Koa from "koa" import Koa from "koa"
import { getTableId } from "../api/controllers/row/utils" 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 { Socket } from "socket.io"
import { GridSocketEvent } from "@budibase/shared-core" 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) const tableId = getTableId(ctx)
this.emitToRoom( this.emitToRoom(ctx, tableId, GridSocketEvent.RowChange, {
ctx, id: row._id,
tableId, row,
GridSocketEvent.RowChange, })
{
id: row._id,
row,
},
options
)
} }
emitRowDeletion(ctx: any, id: string, options?: SocketMessageOptions) { emitRowDeletion(ctx: any, id: string) {
const tableId = getTableId(ctx) const tableId = getTableId(ctx)
this.emitToRoom( this.emitToRoom(ctx, tableId, GridSocketEvent.RowChange, { id, row: null })
ctx,
tableId,
GridSocketEvent.RowChange,
{ id, row: null },
options
)
} }
emitTableUpdate(ctx: any, table: Table, options?: SocketMessageOptions) { emitTableUpdate(ctx: any, table: Table) {
this.emitToRoom( this.emitToRoom(ctx, table._id!, GridSocketEvent.TableChange, {
ctx, id: table._id,
table._id!, table,
GridSocketEvent.TableChange, })
{
id: table._id,
table,
},
options
)
} }
emitTableDeletion(ctx: any, id: string, options?: SocketMessageOptions) { emitTableDeletion(ctx: any, id: string) {
this.emitToRoom( this.emitToRoom(ctx, id, GridSocketEvent.TableChange, { id, table: null })
ctx,
id,
GridSocketEvent.TableChange,
{ id, table: null },
options
)
} }
} }

View File

@ -9,7 +9,7 @@ import { createAdapter } from "@socket.io/redis-adapter"
import { Socket } from "socket.io" import { Socket } from "socket.io"
import { getSocketPubSubClients } from "../utilities/redis" import { getSocketPubSubClients } from "../utilities/redis"
import { SocketEvent, SocketSessionTTL } from "@budibase/shared-core" import { SocketEvent, SocketSessionTTL } from "@budibase/shared-core"
import { SocketSession, SocketMessageOptions } from "@budibase/types" import { SocketSession } from "@budibase/types"
export class BaseSocket { export class BaseSocket {
io: Server io: Server
@ -277,23 +277,10 @@ export class BaseSocket {
} }
// Emit an event to everyone in a room // Emit an event to everyone in a room
emitToRoom( emitToRoom(ctx: any, room: string, event: string, payload: any) {
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]
}
this.io.in(room).emit(event, { this.io.in(room).emit(event, {
...payload, ...payload,
apiSessionId, apiSessionId: ctx.headers?.[Header.SESSION_ID],
}) })
} }
} }

View File

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