Skip to content

Commit

Permalink
fixup! feat!: simplify usage of clientFactories
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Dec 19, 2023
1 parent 2f20660 commit 45a9909
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions lib/src/core/servient.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,18 @@ class Servient {
/// A custom [contentSerdes] can be passed that supports other media types
/// than the default ones.
Servient({
required List<ProtocolClientFactory> clientFactories,
List<ProtocolClientFactory>? clientFactories,
ServerSecurityCallback? serverSecurityCallback,
ContentSerdes? contentSerdes,
}) : _clientFactories = _processProtocolClientFactories(clientFactories),
contentSerdes = contentSerdes ?? ContentSerdes(),
_serverSecurityCallback = serverSecurityCallback;
}) : contentSerdes = contentSerdes ?? ContentSerdes(),
_serverSecurityCallback = serverSecurityCallback {
for (final clientFactory in clientFactories ?? <ProtocolClientFactory>[]) {
addClientFactory(clientFactory);
}
}

final List<ProtocolServer> _servers = [];
final Map<String, ProtocolClientFactory> _clientFactories;
final Map<String, ProtocolClientFactory> _clientFactories = {};
final Map<String, ExposedThing> _things = {};
final Map<String, ConsumedThing> _consumedThings = {};

Expand All @@ -57,18 +60,6 @@ class Servient {
/// The [ContentSerdes] object that is used for serializing/deserializing.
final ContentSerdes contentSerdes;

static Map<String, ProtocolClientFactory> _processProtocolClientFactories(
List<ProtocolClientFactory> protocolFactories,
) {
return Map.fromEntries(
protocolFactories.expand((protocolFactory) sync* {
for (final scheme in protocolFactory.schemes) {
yield MapEntry(scheme, protocolFactory);
}
}),
);
}

/// Starts this [Servient] and returns a [WoT] runtime object.
///
/// The [WoT] runtime can be used for consuming, procuding, and discovering
Expand Down Expand Up @@ -198,6 +189,21 @@ class Servient {
List<String> get clientSchemes =>
_clientFactories.keys.toList(growable: false);

/// Adds a new [clientFactory] to this [Servient].
void addClientFactory(ProtocolClientFactory clientFactory) {
for (final scheme in clientFactory.schemes) {
_clientFactories[scheme] = clientFactory;
}
}

/// Removes a [ProtocolClientFactory] matching the given [scheme] from this
/// [Servient], if present.
///
/// If a [ProtocolClientFactory] was removed, the method returns it, otherwise
/// the return value is `null`.
ProtocolClientFactory? removeClientFactory(String scheme) =>
_clientFactories.remove(scheme);

/// Checks whether a [ProtocolClient] is avaiable for a given [scheme].
bool hasClientFor(String scheme) => _clientFactories.containsKey(scheme);

Expand Down

0 comments on commit 45a9909

Please sign in to comment.