< Summary

Class:Microsoft.Extensions.Azure.ClientOptionsMonitor`2
Assembly:Microsoft.Extensions.Azure
File(s):C:\Git\azure-sdk-for-net\sdk\core\Microsoft.Extensions.Azure\src\Internal\ClientOptionsMonitor.cs
Covered lines:8
Uncovered lines:25
Coverable lines:33
Total lines:105
Line coverage:24.2% (8 of 33)
Covered branches:2
Total branches:10
Branch coverage:20% (2 of 10)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-50%50%
InvokeChanged(...)-0%0%
get_CurrentValue()-0%100%
Get(...)-100%50%
OnChange(...)-0%100%
Dispose()-0%0%
.ctor(...)-0%100%
OnChange(...)-0%100%
Dispose()-0%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\core\Microsoft.Extensions.Azure\src\Internal\ClientOptionsMonitor.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Collections.Generic;
 6using Microsoft.Extensions.Options;
 7using Microsoft.Extensions.Primitives;
 8
 9namespace Microsoft.Extensions.Azure
 10{
 11    // Slightly adjusted copy of https://github.com/aspnet/Extensions/blob/master/src/Options/Options/src/OptionsMonitor
 12    internal class ClientOptionsMonitor<TClient, TOptions> : IOptionsMonitor<TOptions>, IDisposable where TOptions : cla
 13    {
 14        private readonly IOptionsMonitorCache<TOptions> _cache;
 15        private readonly ClientOptionsFactory<TClient, TOptions> _factory;
 16        private readonly IEnumerable<IOptionsChangeTokenSource<TOptions>> _sources;
 4217        private readonly List<IDisposable> _registrations = new List<IDisposable>();
 18        private event Action<TOptions, string> _onChange;
 19
 4220        public ClientOptionsMonitor(ClientOptionsFactory<TClient, TOptions> factory, IEnumerable<IOptionsChangeTokenSour
 21        {
 4222            _factory = factory;
 4223            _sources = sources;
 4224            _cache = cache;
 25
 026            foreach (var source in _sources)
 27            {
 028                var registration = ChangeToken.OnChange(
 029                    () => source.GetChangeToken(),
 030                    (name) => InvokeChanged(name),
 031                    source.Name);
 32
 033                _registrations.Add(registration);
 34            }
 4235        }
 36
 37        private void InvokeChanged(string name)
 38        {
 039            name = name ?? Microsoft.Extensions.Options.Options.DefaultName;
 040            _cache.TryRemove(name);
 041            var options = Get(name);
 42
 043            _onChange?.Invoke(options, name);
 044        }
 45
 46        /// <summary>
 47        /// The present value of the options.
 48        /// </summary>
 49        public TOptions CurrentValue
 50        {
 051            get => Get(Microsoft.Extensions.Options.Options.DefaultName);
 52        }
 53
 54        /// <summary>
 55        /// Returns a configured <typeparamref name="TOptions"/> instance with the given <paramref name="name"/>.
 56        /// </summary>
 57        public virtual TOptions Get(string name)
 58        {
 5059            name = name ?? Microsoft.Extensions.Options.Options.DefaultName;
 9260            return _cache.GetOrAdd(name, () => _factory.Create(name));
 61        }
 62
 63        /// <summary>
 64        /// Registers a listener to be called whenever <typeparamref name="TOptions"/> changes.
 65        /// </summary>
 66        /// <param name="listener">The action to be invoked when <typeparamref name="TOptions"/> has changed.</param>
 67        /// <returns>An <see cref="IDisposable"/> which should be disposed to stop listening for changes.</returns>
 68        public IDisposable OnChange(Action<TOptions, string> listener)
 69        {
 070            var disposable = new ChangeTrackerDisposable(this, listener);
 071            _onChange += disposable.OnChange;
 072            return disposable;
 73        }
 74
 75        /// <summary>
 76        /// Removes all change registration subscriptions.
 77        /// </summary>
 78        public void Dispose()
 79        {
 80            // Remove all subscriptions to the change tokens
 081            foreach (var registration in _registrations)
 82            {
 083                registration.Dispose();
 84            }
 85
 086            _registrations.Clear();
 087        }
 88
 89        internal class ChangeTrackerDisposable : IDisposable
 90        {
 91            private readonly Action<TOptions, string> _listener;
 92            private readonly ClientOptionsMonitor<TClient, TOptions> _monitor;
 93
 094            public ChangeTrackerDisposable(ClientOptionsMonitor<TClient, TOptions> monitor, Action<TOptions, string> lis
 95            {
 096                _listener = listener;
 097                _monitor = monitor;
 098            }
 99
 0100            public void OnChange(TOptions options, string name) => _listener.Invoke(options, name);
 101
 0102            public void Dispose() => _monitor._onChange -= OnChange;
 103        }
 104    }
 105}