Skip to content

Commit

Permalink
Merge pull request #709 from contember/fix/connect-entity-undefined
Browse files Browse the repository at this point in the history
fix(binding): throw error in (dis)connectEntityAtField if not registered
  • Loading branch information
matej21 authored May 27, 2024
2 parents e9ee70b + bf53a1f commit d9e0506
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions packages/binding/src/core/operations/EntityOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ export class EntityOperations {
}

for (const state of StateIterator.eachSiblingRealm(outerState)) {
const targetHasOneMarkers = this.resolveHasOneRelationMarkers(
getEntityMarker(state).fields,
fieldName,
`Cannot connect at field '${fieldName}' as it doesn't refer to a has one relation.`,
)
const targetHasOneMarkers = this.resolveHasOneRelationMarkers(getEntityMarker(state).fields, fieldName, 'connect')
for (const targetHasOneMarker of targetHasOneMarkers) {
const previouslyConnectedState = state.children.get(targetHasOneMarker.placeholderName)

Expand Down Expand Up @@ -169,11 +165,7 @@ export class EntityOperations {
const persistedData = this.treeStore.persistedEntityData.get(outerState.entity.id.uniqueValue)

for (const state of StateIterator.eachSiblingRealm(outerState)) {
const targetHasOneMarkers = this.resolveHasOneRelationMarkers(
getEntityMarker(state).fields,
fieldName,
`Cannot disconnect at field '${fieldName}' as it doesn't refer to a has one relation.`,
)
const targetHasOneMarkers = this.resolveHasOneRelationMarkers(getEntityMarker(state).fields, fieldName, 'disconnect')
for (const targetHasOneMarker of targetHasOneMarkers) {
const stateToDisconnect = state.children.get(targetHasOneMarker.placeholderName)

Expand Down Expand Up @@ -293,20 +285,20 @@ export class EntityOperations {
private *resolveHasOneRelationMarkers(
container: EntityFieldMarkersContainer,
field: FieldName,
message: string,
type: 'connect' | 'disconnect',
): IterableIterator<HasOneRelationMarker> {
const placeholders = container.placeholders.get(field)

if (placeholders === undefined) {
return
throw new BindingError(`Cannot ${type} at field '${field}' as it wasn't registered during static render.`)
}
const normalizedPlaceholders = placeholders instanceof Set ? placeholders : [placeholders]

for (const placeholderName of normalizedPlaceholders) {
const hasOneRelation = container.markers.get(placeholderName)

if (!(hasOneRelation instanceof HasOneRelationMarker)) {
throw new BindingError(message)
throw new BindingError(`Cannot ${type} at field '${field}' as it doesn't refer to a has one relation.`)
}
yield hasOneRelation
}
Expand Down

0 comments on commit d9e0506

Please sign in to comment.