| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using Microsoft.Extensions.Options; |
| | 7 | | using Microsoft.Extensions.Primitives; |
| | 8 | |
|
| | 9 | | namespace 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; |
| 42 | 17 | | private readonly List<IDisposable> _registrations = new List<IDisposable>(); |
| | 18 | | private event Action<TOptions, string> _onChange; |
| | 19 | |
|
| 42 | 20 | | public ClientOptionsMonitor(ClientOptionsFactory<TClient, TOptions> factory, IEnumerable<IOptionsChangeTokenSour |
| | 21 | | { |
| 42 | 22 | | _factory = factory; |
| 42 | 23 | | _sources = sources; |
| 42 | 24 | | _cache = cache; |
| | 25 | |
|
| 0 | 26 | | foreach (var source in _sources) |
| | 27 | | { |
| 0 | 28 | | var registration = ChangeToken.OnChange( |
| 0 | 29 | | () => source.GetChangeToken(), |
| 0 | 30 | | (name) => InvokeChanged(name), |
| 0 | 31 | | source.Name); |
| | 32 | |
|
| 0 | 33 | | _registrations.Add(registration); |
| | 34 | | } |
| 42 | 35 | | } |
| | 36 | |
|
| | 37 | | private void InvokeChanged(string name) |
| | 38 | | { |
| 0 | 39 | | name = name ?? Microsoft.Extensions.Options.Options.DefaultName; |
| 0 | 40 | | _cache.TryRemove(name); |
| 0 | 41 | | var options = Get(name); |
| | 42 | |
|
| 0 | 43 | | _onChange?.Invoke(options, name); |
| 0 | 44 | | } |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// The present value of the options. |
| | 48 | | /// </summary> |
| | 49 | | public TOptions CurrentValue |
| | 50 | | { |
| 0 | 51 | | 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 | | { |
| 50 | 59 | | name = name ?? Microsoft.Extensions.Options.Options.DefaultName; |
| 92 | 60 | | 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 | | { |
| 0 | 70 | | var disposable = new ChangeTrackerDisposable(this, listener); |
| 0 | 71 | | _onChange += disposable.OnChange; |
| 0 | 72 | | 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 |
| 0 | 81 | | foreach (var registration in _registrations) |
| | 82 | | { |
| 0 | 83 | | registration.Dispose(); |
| | 84 | | } |
| | 85 | |
|
| 0 | 86 | | _registrations.Clear(); |
| 0 | 87 | | } |
| | 88 | |
|
| | 89 | | internal class ChangeTrackerDisposable : IDisposable |
| | 90 | | { |
| | 91 | | private readonly Action<TOptions, string> _listener; |
| | 92 | | private readonly ClientOptionsMonitor<TClient, TOptions> _monitor; |
| | 93 | |
|
| 0 | 94 | | public ChangeTrackerDisposable(ClientOptionsMonitor<TClient, TOptions> monitor, Action<TOptions, string> lis |
| | 95 | | { |
| 0 | 96 | | _listener = listener; |
| 0 | 97 | | _monitor = monitor; |
| 0 | 98 | | } |
| | 99 | |
|
| 0 | 100 | | public void OnChange(TOptions options, string name) => _listener.Invoke(options, name); |
| | 101 | |
|
| 0 | 102 | | public void Dispose() => _monitor._onChange -= OnChange; |
| | 103 | | } |
| | 104 | | } |
| | 105 | | } |