Skip to content

Commit

Permalink
Reload transform configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
thohng committed May 14, 2024
1 parent 23151ef commit 2a36c0c
Showing 1 changed file with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Primitives;

namespace NetLah.Extensions.Configuration;

public class TransformConfigurationProvider : ConfigurationProvider
{
private readonly IConfigurationSection _configurationSection;
private object? _lock = null;
private IChangeToken? _token;

public TransformConfigurationProvider(IConfigurationSection configurationSection)
{
_configurationSection = configurationSection;
}

private void OnChange(object? obj)
{
Data.Clear();
InternalLoad();
OnReload();
}

public override void Load()
{
InternalLoad();

if (Interlocked.CompareExchange(ref _lock, new object(), null) == null)
{
_token = _configurationSection.GetReloadToken();
_token.RegisterChangeCallback(OnChange, this);
}
}

private void InternalLoad()
{
foreach (var item in _configurationSection.GetChildren())
{
Expand All @@ -37,13 +58,13 @@ public override void Load()
}
}
}
}

private void TryParse(string prefix, string keyValue)
{
var pos = keyValue.IndexOf('=');
var key = keyValue[..pos];
var value = keyValue[(pos + 1)..];
Data[prefix + key] = value;
void TryParse(string prefix, string keyValue)
{
var pos = keyValue.IndexOf('=');
var key = keyValue[..pos];
var value = keyValue[(pos + 1)..];
Data[prefix + key] = value;
}
}
}

0 comments on commit 2a36c0c

Please sign in to comment.