From 45a990977c772b7eed0c63603f60647cb4be26d2 Mon Sep 17 00:00:00 2001 From: Jan Romann Date: Tue, 19 Dec 2023 10:38:06 +0100 Subject: [PATCH] fixup! feat!: simplify usage of clientFactories --- lib/src/core/servient.dart | 40 ++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/lib/src/core/servient.dart b/lib/src/core/servient.dart index 6a5165eb..158711a7 100644 --- a/lib/src/core/servient.dart +++ b/lib/src/core/servient.dart @@ -40,15 +40,18 @@ class Servient { /// A custom [contentSerdes] can be passed that supports other media types /// than the default ones. Servient({ - required List clientFactories, + List? clientFactories, ServerSecurityCallback? serverSecurityCallback, ContentSerdes? contentSerdes, - }) : _clientFactories = _processProtocolClientFactories(clientFactories), - contentSerdes = contentSerdes ?? ContentSerdes(), - _serverSecurityCallback = serverSecurityCallback; + }) : contentSerdes = contentSerdes ?? ContentSerdes(), + _serverSecurityCallback = serverSecurityCallback { + for (final clientFactory in clientFactories ?? []) { + addClientFactory(clientFactory); + } + } final List _servers = []; - final Map _clientFactories; + final Map _clientFactories = {}; final Map _things = {}; final Map _consumedThings = {}; @@ -57,18 +60,6 @@ class Servient { /// The [ContentSerdes] object that is used for serializing/deserializing. final ContentSerdes contentSerdes; - static Map _processProtocolClientFactories( - List 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 @@ -198,6 +189,21 @@ class Servient { List 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);