From 50245faec44efbcfad62a5557d2945b067ce4143 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sat, 17 Feb 2024 10:33:28 -0800 Subject: [PATCH] PMD fix --- .../cameraserver/source/CameraServerSourceType.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/plugins/cameraserver/src/main/java/edu/wpi/first/shuffleboard/plugin/cameraserver/source/CameraServerSourceType.java b/plugins/cameraserver/src/main/java/edu/wpi/first/shuffleboard/plugin/cameraserver/source/CameraServerSourceType.java index 62623f74f..25f63845c 100644 --- a/plugins/cameraserver/src/main/java/edu/wpi/first/shuffleboard/plugin/cameraserver/source/CameraServerSourceType.java +++ b/plugins/cameraserver/src/main/java/edu/wpi/first/shuffleboard/plugin/cameraserver/source/CameraServerSourceType.java @@ -26,7 +26,7 @@ import javafx.collections.ObservableMap; @UiHints(showConnectionIndicator = false) // same host as NetworkTables plugin, so no need to duplicate that information -public final class CameraServerSourceType extends SourceType { +public final class CameraServerSourceType extends SourceType implements AutoCloseable { public static final CameraServerSourceType INSTANCE = new CameraServerSourceType(); @@ -35,12 +35,13 @@ public final class CameraServerSourceType extends SourceType { private final ObservableMap availableSources = FXCollections.observableHashMap(); private final MultiSubscriber subscriber; + private final int listener; private CameraServerSourceType() { super("CameraServer", true, "camera_server://", CameraServerSourceType::forName); NetworkTableInstance inst = NetworkTableInstance.getDefault(); subscriber = new MultiSubscriber(inst, new String[] {"/CameraPublisher"}, PubSubOption.hidden(true)); - inst.addListener( + listener = inst.addListener( subscriber, EnumSet.of( NetworkTableEvent.Kind.kUnpublish, @@ -66,6 +67,12 @@ private CameraServerSourceType() { })); } + @Override + public void close() { + subscriber.close(); + NetworkTableInstance.getDefault().removeListener(listener); + } + public static CameraServerSource forName(String name) { return INSTANCE.sources.computeIfAbsent(name, CameraServerSource::new); }