Skip to content

Commit

Permalink
fix: issue where modules were disappear when schema changed (#2963)
Browse files Browse the repository at this point in the history
  • Loading branch information
wesbillman authored Oct 3, 2024
1 parent 0a7e36b commit b5b6c48
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions frontend/console/src/api/schema/use-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { ControllerService } from '../../protos/xyz/block/ftl/v1/ftl_connect.ts'
import { DeploymentChangeType, type PullSchemaResponse } from '../../protos/xyz/block/ftl/v1/ftl_pb.ts'

const streamingSchemaKey = 'streamingSchema'
const currentDeployments: Record<string, string> = {}
const schemaMap: Record<string, PullSchemaResponse> = {}

export const useSchema = () => {
const client = useClient(ControllerService)
Expand All @@ -14,23 +16,35 @@ export const useSchema = () => {

const streamSchema = async (signal: AbortSignal) => {
try {
const schemaMap = new Map<string, PullSchemaResponse>()
for await (const response of client.pullSchema({}, { signal })) {
const moduleName = response.moduleName ?? ''
console.log(`schema changed: ${DeploymentChangeType[response.changeType]} ${moduleName}`)
const deploymentKey = response.deploymentKey ?? ''
console.log(`schema changed: ${DeploymentChangeType[response.changeType]} ${deploymentKey}`)
switch (response.changeType) {
case DeploymentChangeType.DEPLOYMENT_ADDED:
schemaMap.set(moduleName, response)
break
case DeploymentChangeType.DEPLOYMENT_CHANGED:
schemaMap.set(moduleName, response)
case DeploymentChangeType.DEPLOYMENT_CHANGED: {
const previousDeploymentKey = currentDeployments[moduleName]

currentDeployments[moduleName] = deploymentKey

if (previousDeploymentKey && previousDeploymentKey !== deploymentKey) {
delete schemaMap[previousDeploymentKey]
}

schemaMap[deploymentKey] = response
break
}

case DeploymentChangeType.DEPLOYMENT_REMOVED:
schemaMap.delete(moduleName)
if (currentDeployments[moduleName] === deploymentKey) {
delete schemaMap[deploymentKey]
delete currentDeployments[moduleName]
}
break
}

if (!response.more) {
const schema = Array.from(schemaMap.values()).sort((a, b) => a.schema?.name?.localeCompare(b.schema?.name ?? '') ?? 0)
const schema = Object.values(schemaMap).sort((a, b) => a.schema?.name?.localeCompare(b.schema?.name ?? '') ?? 0)
queryClient.setQueryData([streamingSchemaKey], schema ?? [])
}
}
Expand Down

0 comments on commit b5b6c48

Please sign in to comment.