@@ -37,13 +37,24 @@ class ServientException implements Exception {
3737class Servient {
3838 /// Creates a new [Servient] .
3939 ///
40- /// A custom [contentSerdes] can be passed that supports other media types
41- /// than the default ones.
40+ /// The [Servient] can be preconfigured with a [List] of
41+ /// [ProtocolClientFactory] s.
42+ /// However, it is also possible to dynamically [addClientFactory] s and
43+ /// [removeClientFactory] s at runtime.
44+ ///
45+ /// If you want to support a custom media type not already included in the
46+ /// [ContentSerdes] class, a custom [contentSerdes] object can be passed as an
47+ /// argument.
4248 Servient ({
49+ List <ProtocolClientFactory >? clientFactories,
4350 ServerSecurityCallback ? serverSecurityCallback,
4451 ContentSerdes ? contentSerdes,
4552 }) : contentSerdes = contentSerdes ?? ContentSerdes (),
46- _serverSecurityCallback = serverSecurityCallback;
53+ _serverSecurityCallback = serverSecurityCallback {
54+ for (final clientFactory in clientFactories ?? < ProtocolClientFactory > []) {
55+ addClientFactory (clientFactory);
56+ }
57+ }
4758
4859 final List <ProtocolServer > _servers = [];
4960 final Map <String , ProtocolClientFactory > _clientFactories = {};
@@ -184,13 +195,21 @@ class Servient {
184195 List <String > get clientSchemes =>
185196 _clientFactories.keys.toList (growable: false );
186197
187- /// Adds a new [clientFactory] to this [Servient.]
198+ /// Adds a new [clientFactory] to this [Servient] .
188199 void addClientFactory (ProtocolClientFactory clientFactory) {
189200 for (final scheme in clientFactory.schemes) {
190201 _clientFactories[scheme] = clientFactory;
191202 }
192203 }
193204
205+ /// Removes a [ProtocolClientFactory] matching the given [scheme] from this
206+ /// [Servient] , if present.
207+ ///
208+ /// If a [ProtocolClientFactory] was removed, the method returns it, otherwise
209+ /// the return value is `null` .
210+ ProtocolClientFactory ? removeClientFactory (String scheme) =>
211+ _clientFactories.remove (scheme);
212+
194213 /// Checks whether a [ProtocolClient] is avaiable for a given [scheme] .
195214 bool hasClientFor (String scheme) => _clientFactories.containsKey (scheme);
196215
0 commit comments