When an app’s behavior depends on an external API, ensure the API call is both necessary (authoritative) and deterministic (verifiable). Concretely:
1) Don’t add “auxiliary” API calls when existing state already determines the outcome equivalently
2) Never decide correctness from paginated/partial results
?query=<exact name>) so the response can be checked for the specific item.3) Ensure async persistence writes the latest intended selection
Example pattern (filtered request + safe persistence):
const persist = (selection: { branch?: string }) => {
return CloudServiceClient.setCloudTaskTarget(/* ... */)
}
async function suggestBranchIfExists({ repoId, branchName }: { repoId: number; branchName: string }) {
// Deterministic: ask the server for the exact branch, not the first page.
const res = await CloudServiceClient.getRepositoryBranches(
RepositoryBranchesRequest.create({ repositoryId: repoId, query: branchName })
)
const exists = res.branches.includes(branchName)
// Safe write: base persistence on the current selection, not a captured stale value.
setSelected((current) =>
persist({
...current,
branch: exists ? branchName : current.branch,
})
)
}
Apply this as a standard when touching API-backed state: identify the authoritative source for the decision, use discriminating server queries for correctness, and prevent stale async writes.