Skip to content

Commit

Permalink
Clean up removed links in GenericItemChannelLinkProvider (#4326)
Browse files Browse the repository at this point in the history
* Clean up removed links in GenericItemChannelLinkProvider
* defer processing until stopConfigurationUpdate is called

Signed-off-by: Jimmy Tanagra <[email protected]>
  • Loading branch information
jimtng authored and wborn committed Sep 7, 2024
1 parent 5b2886a commit f5fe880
Showing 1 changed file with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public class GenericItemChannelLinkProvider extends AbstractProvider<ItemChannel
/** caches binding configurations. maps itemNames to {@link ItemChannelLink}s */
protected Map<String, Map<ChannelUID, ItemChannelLink>> itemChannelLinkMap = new ConcurrentHashMap<>();

private Map<String, Set<ChannelUID>> addedItemChannels = new ConcurrentHashMap<>();

/**
* stores information about the context of items. The map has this content
* structure: context -> Set of Item names
Expand Down Expand Up @@ -120,6 +122,7 @@ private void createItemChannelLink(String context, String itemName, String chann
} else {
notifyListenersAboutUpdatedElement(oldLink, itemChannelLink);
}
addedItemChannels.computeIfAbsent(itemName, k -> new HashSet<>(2)).add(channelUIDObject);
}

@Override
Expand All @@ -146,6 +149,17 @@ public void stopConfigurationUpdate(String context) {
}
}
Optional.ofNullable(contextMap.get(context)).ifPresent(ctx -> ctx.removeAll(previousItemNames));

addedItemChannels.forEach((itemName, addedChannelUIDs) -> {
Map<ChannelUID, ItemChannelLink> links = itemChannelLinkMap.getOrDefault(itemName, Map.of());
Set<ChannelUID> removedChannelUIDs = new HashSet<>(links.keySet());
removedChannelUIDs.removeAll(addedChannelUIDs);
removedChannelUIDs.forEach(removedChannelUID -> {
ItemChannelLink link = links.remove(removedChannelUID);
notifyListenersAboutRemovedElement(link);
});
});
addedItemChannels.clear();
}

@Override
Expand Down

0 comments on commit f5fe880

Please sign in to comment.