Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ internal class RazorComponentEndpointDataSource<[DynamicallyAccessedMembers(Comp
private List<Endpoint>? _endpoints;
private CancellationTokenSource _cancellationTokenSource;
private IChangeToken _changeToken;
private IDisposable? _disposable; // THREADING: protected by _lock

// Internal for testing.
internal ComponentApplicationBuilder Builder => _builder;
Expand All @@ -45,6 +46,7 @@ public RazorComponentEndpointDataSource(
_renderModeEndpointProviders = renderModeEndpointProviders.ToArray();
_factory = factory;
_hotReloadService = hotReloadService;
HotReloadService.ClearCacheEvent += OnHotReloadClearCache;
DefaultBuilder = new RazorComponentsEndpointConventionBuilder(
_lock,
builder,
Expand Down Expand Up @@ -139,12 +141,23 @@ private void UpdateEndpoints()
_cancellationTokenSource = new CancellationTokenSource();
_changeToken = new CancellationChangeToken(_cancellationTokenSource.Token);
oldCancellationTokenSource?.Cancel();
oldCancellationTokenSource?.Dispose();
if (_hotReloadService is { MetadataUpdateSupported : true })
{
ChangeToken.OnChange(_hotReloadService.GetChangeToken, UpdateEndpoints);
_disposable?.Dispose();
_disposable = ChangeToken.OnChange(_hotReloadService.GetChangeToken, UpdateEndpoints);
}
}
}

public void OnHotReloadClearCache(Type[]? types)
{
lock (_lock)
{
_disposable?.Dispose();
_disposable = null;
}
}

public override IChangeToken GetChangeToken()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public HotReloadService()

private CancellationTokenSource _tokenSource = new();
private static event Action<Type[]?>? UpdateApplicationEvent;
internal static event Action<Type[]?>? ClearCacheEvent;

public bool MetadataUpdateSupported { get; internal set; }

Expand All @@ -27,11 +28,17 @@ public static void UpdateApplication(Type[]? changedTypes)
{
UpdateApplicationEvent?.Invoke(changedTypes);
}

public static void ClearCache(Type[]? types)
{
ClearCacheEvent?.Invoke(types);
}

private void NotifyUpdateApplication(Type[]? changedTypes)
{
var current = Interlocked.Exchange(ref _tokenSource, new CancellationTokenSource());
current.Cancel();
current.Dispose();
}

public void Dispose()
Expand Down