From b54450ed38f3b47790ffadfa695fa0c146f0b61e Mon Sep 17 00:00:00 2001 From: Jan Romann Date: Sun, 19 May 2024 19:13:50 +0200 Subject: [PATCH] feat!: allow for consuming the same TD twice --- .../core/implementation/consumed_thing.dart | 8 ++++- lib/src/core/implementation/servient.dart | 34 +++++++------------ lib/src/core/implementation/wot.dart | 9 ++--- test/core/consumed_thing_test.dart | 1 - test/core/wot_test.dart | 8 ++--- 5 files changed, 27 insertions(+), 33 deletions(-) diff --git a/lib/src/core/implementation/consumed_thing.dart b/lib/src/core/implementation/consumed_thing.dart index c29204c0..bf4fda7c 100644 --- a/lib/src/core/implementation/consumed_thing.dart +++ b/lib/src/core/implementation/consumed_thing.dart @@ -417,7 +417,7 @@ class ConsumedThing implements scripting_api.ConsumedThing { } /// Cleans up the resources used by this [ConsumedThing]. - void destroy() { + bool destroy({bool external = true}) { for (final observedProperty in _observedProperties.values) { observedProperty.stop(); } @@ -426,5 +426,11 @@ class ConsumedThing implements scripting_api.ConsumedThing { subscribedEvent.stop(); } _subscribedEvents.clear(); + + if (external) { + return servient.deregisterConsumedthing(this); + } + + return false; } } diff --git a/lib/src/core/implementation/servient.dart b/lib/src/core/implementation/servient.dart index 4db0bb7e..20a80b2a 100644 --- a/lib/src/core/implementation/servient.dart +++ b/lib/src/core/implementation/servient.dart @@ -51,7 +51,7 @@ class Servient { final List _servers = []; final Map _clientFactories = {}; final Map _things = {}; - final Map _consumedThings = {}; + final Set _consumedThings = {}; final ServerSecurityCallback? _serverSecurityCallback; @@ -85,7 +85,7 @@ class Servient { clientFactory.destroy(); } _clientFactories.clear(); - for (final consumedThing in _consumedThings.values) { + for (final consumedThing in _consumedThings) { consumedThing.destroy(); } _consumedThings.clear(); @@ -135,20 +135,20 @@ class Servient { return true; } - /// Removes and cleans up the resources of the [ConsumedThing] with the given - /// [id]. + /// Removes and cleans up the resources of a [ConsumedThing]. /// /// If the [ConsumedThing] has not been registered before, `false` is /// returned, otherwise `true`. - bool destroyConsumedThing(String id) { - final existingThing = _consumedThings.remove(id); - - if (existingThing != null) { - existingThing.destroy(); - return true; - } + bool destroyConsumedThing(ConsumedThing consumedThing) { + return consumedThing.destroy(external: false); + } - return false; + /// Deregisters the given [consumedThing]. + /// + /// If the [ConsumedThing] has not been registered before, `false` is + /// returned, otherwise `true`. + bool deregisterConsumedthing(ConsumedThing consumedThing) { + return _consumedThings.remove(consumedThing); } /// Adds a [ConsumedThing] to the servient if it hasn't been registered @@ -156,15 +156,7 @@ class Servient { /// /// Returns `false` if the [thing] has already been registered, otherwise /// `true`. - bool addConsumedThing(ConsumedThing thing) { - final id = thing.identifier; - if (_consumedThings.containsKey(id)) { - return false; - } - - _consumedThings[id] = thing; - return true; - } + bool addConsumedThing(ConsumedThing thing) => _consumedThings.add(thing); /// Returns an [ExposedThing] with the given [id] if it has been registered. ExposedThing? thing(String id) => _things[id]; diff --git a/lib/src/core/implementation/wot.dart b/lib/src/core/implementation/wot.dart index 0585b200..35a0da0a 100644 --- a/lib/src/core/implementation/wot.dart +++ b/lib/src/core/implementation/wot.dart @@ -35,12 +35,9 @@ class WoT implements scripting_api.WoT { ThingDescription thingDescription, ) async { final newThing = ConsumedThing(_servient, thingDescription); - if (_servient.addConsumedThing(newThing)) { - return newThing; - } else { - final id = thingDescription.identifier; - throw DartWotException(id); - } + _servient.addConsumedThing(newThing); + + return newThing; } /// Exposes a Thing based on a (partial) TD. diff --git a/test/core/consumed_thing_test.dart b/test/core/consumed_thing_test.dart index 81c17f6c..075f7412 100644 --- a/test/core/consumed_thing_test.dart +++ b/test/core/consumed_thing_test.dart @@ -262,7 +262,6 @@ void main() { ); await servient.shutdown(); - expect(servient.destroyConsumedThing(parsedTd.identifier), false); }, skip: true, // TODO: Replace with test with local server ); diff --git a/test/core/wot_test.dart b/test/core/wot_test.dart index c7011ff0..33eb1621 100644 --- a/test/core/wot_test.dart +++ b/test/core/wot_test.dart @@ -9,7 +9,7 @@ import "package:test/test.dart"; void main() { group("WoT should", () { - test("throw an execption when consuming the same TD twice", () async { + test("not throw an execption when consuming the same TD twice", () async { const thingDescriptionJson = { "@context": "https://www.w3.org/2022/wot/td/v1.1", "title": "Test Thing", @@ -22,9 +22,9 @@ void main() { final wot = await Servient().start(); - await wot.consume(thingDescription); - final result = wot.consume(thingDescription); - await expectLater(result, throwsA(isA())); + final firstConsumedThing = await wot.consume(thingDescription); + final secondConsumedThing = await wot.consume(thingDescription); + expect(firstConsumedThing != secondConsumedThing, isTrue); }); test(