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

Clean up removed links in GenericItemChannelLinkProvider #4326

Merged
merged 3 commits into from
Jul 27, 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
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