Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: issue where modules would disappear when schema changed #2963

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading