Skip to content

Commit

Permalink
refactor: do not store ConsumedThings in InternalServient
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed May 24, 2024
1 parent 37fbb28 commit 509969a
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 52 deletions.
18 changes: 0 additions & 18 deletions lib/src/core/implementation/consumed_thing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -416,22 +416,4 @@ class ConsumedThing implements scripting_api.ConsumedThing {
_subscribedEvents.remove(key);
}
}

/// Cleans up the resources used by this [ConsumedThing].
bool destroy({bool external = true}) {
for (final observedProperty in _observedProperties.values) {
observedProperty.stop();
}
_observedProperties.clear();
for (final subscribedEvent in _subscribedEvents.values) {
subscribedEvent.stop();
}
_subscribedEvents.clear();

if (external) {
return servient.deregisterConsumedThing(this);
}

return false;
}
}
36 changes: 2 additions & 34 deletions lib/src/core/implementation/servient.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ class InternalServient implements Servient {
final List<ProtocolServer> _servers = [];
final Map<String, ProtocolClientFactory> _clientFactories = {};
final Map<String, ExposedThing> _things = {};
final Set<ConsumedThing> _consumedThings = {};

final ServerSecurityCallback? _serverSecurityCallback;

Expand Down Expand Up @@ -127,10 +126,6 @@ class InternalServient implements Servient {
clientFactory.destroy();
}
_clientFactories.clear();
for (final consumedThing in _consumedThings) {
consumedThing.destroy();
}
_consumedThings.clear();

final serverStatuses = _servers.map((server) => server.stop()).toList();
await Future.wait(serverStatuses);
Expand Down Expand Up @@ -177,29 +172,6 @@ class InternalServient implements Servient {
return true;
}

/// Removes and cleans up the resources of a [ConsumedThing].
///
/// If the [ConsumedThing] has not been registered before, `false` is
/// returned, otherwise `true`.
bool destroyConsumedThing(ConsumedThing consumedThing) {
return consumedThing.destroy(external: false);
}

/// De-registers 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
/// before.
///
/// Returns `false` if the [thing] has already been registered, otherwise
/// `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];

Expand Down Expand Up @@ -270,12 +242,8 @@ class InternalServient implements Servient {
/// Consumes a [ThingDescription] and returns a [scripting_api.ConsumedThing].
Future<scripting_api.ConsumedThing> consume(
ThingDescription thingDescription,
) async {
final newThing = ConsumedThing(this, thingDescription);
addConsumedThing(newThing);

return newThing;
}
) async =>
ConsumedThing(this, thingDescription);

/// Exposes a Thing based on an [scripting_api.ExposedThingInit].
Future<scripting_api.ExposedThing> produce(
Expand Down

0 comments on commit 509969a

Please sign in to comment.