Skip to content

Commit

Permalink
feat(core): add cleanup methods
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Mar 27, 2022
1 parent 330171d commit a1c2f90
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/src/core/consumed_thing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,18 @@ class ConsumedThing implements scripting_api.ConsumedThing {

return operationTypes.contains(operationType.toShortString());
}

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

/// Private class providing a tuple of a [ProtocolClient] and a [Form].
Expand Down
22 changes: 22 additions & 0 deletions lib/src/core/servient.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,16 @@ class Servient {
for (final clientFactory in _clientFactories.values) {
clientFactory.destroy();
}
_clientFactories.clear();
for (final consumedThing in _consumedThings.values) {
consumedThing.destroy();
}
_consumedThings.clear();

final serverStatuses =
_servers.map((server) => server.stop()).toList(growable: false);
await Future.wait(serverStatuses);
serverStatuses.clear();
}

void _cleanUpForms(Iterable<InteractionAffordance>? interactionAffordances) {
Expand Down Expand Up @@ -120,6 +126,22 @@ class Servient {
return true;
}

/// Removes and cleans up the resources of the [ConsumedThing] with the given
/// [id].
///
/// 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;
}

return false;
}

/// Adds a [ConsumedThing] to the servient if it hasn't been registered
/// before.
///
Expand Down

0 comments on commit a1c2f90

Please sign in to comment.