Skip to content

Commit

Permalink
Register TimeProvider as Singleton.
Browse files Browse the repository at this point in the history
  • Loading branch information
mayuki committed Mar 11, 2024
1 parent b71e80c commit 2b48271
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/R3.Blazor/ObservableSystemInitializationService.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
namespace R3;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace R3;

public static class BlazorR3Extensions
{
public static IServiceCollection AddBlazorR3(this IServiceCollection services)
{
return AddBlazorR3(services, _ => new SynchronizationContextTimeProvider(), null!);
return AddBlazorR3(services, _ => new SynchronizationContextTimeProvider(() => SynchronizationContext.Current), null!);
}

public static IServiceCollection AddBlazorR3(this IServiceCollection services, Action<Exception> unhandledExceptionHandler)
{
return AddBlazorR3(services, _ => new SynchronizationContextTimeProvider(), unhandledExceptionHandler);
return AddBlazorR3(services, _ => new SynchronizationContextTimeProvider(() => SynchronizationContext.Current), unhandledExceptionHandler);
}

public static IServiceCollection AddBlazorR3(this IServiceCollection services, Func<IServiceProvider, TimeProvider> timeProviderFactory)
Expand All @@ -20,7 +22,7 @@ public static IServiceCollection AddBlazorR3(this IServiceCollection services, F
public static IServiceCollection AddBlazorR3(this IServiceCollection services, Func<IServiceProvider, TimeProvider> timeProviderFactory, Action<Exception> unhandledExceptionHandler)
{
services.AddHttpContextAccessor();
services.AddScoped<TimeProvider>(timeProviderFactory);
services.TryAddSingleton<TimeProvider>(timeProviderFactory);
services.AddHostedService(sp => new ObservableSystemInitializationService(sp.GetRequiredService<IHttpContextAccessor>(), unhandledExceptionHandler));

return services;
Expand Down
17 changes: 14 additions & 3 deletions src/R3/SynchronizationContextTimeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public sealed class SynchronizationContextTimeProvider : TimeProvider
{
readonly SynchronizationContext? synchronizationContext;
readonly Func<SynchronizationContext?> synchronizationContextAccessor;
readonly TimeProvider timeProvider;

public SynchronizationContextTimeProvider()
Expand All @@ -15,15 +15,26 @@ public SynchronizationContextTimeProvider(SynchronizationContext? synchronizatio
{
}

public SynchronizationContextTimeProvider(Func<SynchronizationContext?> synchronizationContextAccessor)
: this(synchronizationContextAccessor, TimeProvider.System)
{
}

public SynchronizationContextTimeProvider(SynchronizationContext? synchronizationContext, TimeProvider timeProvider)
{
this.synchronizationContext = synchronizationContext;
this.synchronizationContextAccessor = () => synchronizationContext;
this.timeProvider = timeProvider;
}

public SynchronizationContextTimeProvider(Func<SynchronizationContext?> synchronizationContextAccessor, TimeProvider timeProvider)
{
this.synchronizationContextAccessor = synchronizationContextAccessor;
this.timeProvider = timeProvider;
}

public override ITimer CreateTimer(TimerCallback callback, object? state, TimeSpan dueTime, TimeSpan period)
{
return new SynchronizationContextTimer(timeProvider, synchronizationContext, callback, state, dueTime, period);
return new SynchronizationContextTimer(timeProvider, synchronizationContextAccessor(), callback, state, dueTime, period);
}
}

Expand Down

0 comments on commit 2b48271

Please sign in to comment.