diff --git a/.config/dotnet-tools.json b/.config/dotnet-tools.json index 0cbc674..9cc1a0c 100644 --- a/.config/dotnet-tools.json +++ b/.config/dotnet-tools.json @@ -3,13 +3,13 @@ "isRoot": true, "tools": { "nbgv": { - "version": "3.4.244", + "version": "3.6.133", "commands": [ "nbgv" ] }, "paket": { - "version": "7.1.4", + "version": "8.0.0", "commands": [ "paket" ] diff --git a/src/Bolero.Templating.Server/Attributes.fs b/src/Bolero.Templating.Server/Attributes.fs new file mode 100644 index 0000000..1fc091d --- /dev/null +++ b/src/Bolero.Templating.Server/Attributes.fs @@ -0,0 +1,9 @@ +namespace Bolero.Templating.Server + +open System + +[] +type internal TimeSpanConstantAttribute(miliseconds) = + inherit Attribute() + member _.Value = TimeSpan.FromMilliseconds miliseconds + diff --git a/src/Bolero.Templating.Server/Bolero.Templating.Server.fsproj b/src/Bolero.Templating.Server/Bolero.Templating.Server.fsproj index 6e7dad9..7b97904 100644 --- a/src/Bolero.Templating.Server/Bolero.Templating.Server.fsproj +++ b/src/Bolero.Templating.Server/Bolero.Templating.Server.fsproj @@ -7,6 +7,7 @@ Library + diff --git a/src/Bolero.Templating.Server/Templating.fs b/src/Bolero.Templating.Server/Templating.fs index ca690c8..e8e258a 100644 --- a/src/Bolero.Templating.Server/Templating.fs +++ b/src/Bolero.Templating.Server/Templating.fs @@ -24,6 +24,7 @@ open System.Collections.Concurrent open System.IO open System.Threading.Tasks open System.Runtime.CompilerServices +open System.Runtime.InteropServices open Microsoft.AspNetCore.Builder open Microsoft.AspNetCore.Routing open Microsoft.AspNetCore.SignalR @@ -178,27 +179,38 @@ module Impl = for handler in handlers do handler.Dispose() -[] -type ServerTemplatingExtensions = +[] +module private Constants = + + let [] DefaultTemplateDir = "." + let [] DefaultDelay = 100. + + +[] +type ServerTemplatingExtensions() = [] - static member AddHotReload(this: IServiceCollection, configure: WatcherConfig -> WatcherConfig) : IServiceCollection = + static member AddHotReload(this: IServiceCollection, configure: Func) : IServiceCollection = this.AddSignalR().AddJsonProtocol() |> ignore - let config = configure { Directory = "."; Delay = TimeSpan.FromMilliseconds 100. } + let config = configure.Invoke { Directory = DefaultTemplateDir; Delay = TimeSpan.FromMilliseconds DefaultDelay } this.AddSingleton(config) .AddSingleton() .AddTransient() [] - static member AddHotReload(this: IServiceCollection, ?templateDir: string, ?delay: TimeSpan) : IServiceCollection = + static member AddHotReload( + this: IServiceCollection, + [] templateDir: string, + [] delay: TimeSpan) + : IServiceCollection = ServerTemplatingExtensions.AddHotReload(this, fun config -> { - Directory = defaultArg templateDir config.Directory - Delay = defaultArg delay config.Delay + Directory = templateDir + Delay = delay }) [] - static member UseHotReload(this: IEndpointRouteBuilder, ?urlPath: string) : unit = + static member UseHotReload(this: IEndpointRouteBuilder, [] urlPath: string) : unit = this.ServiceProvider.GetService().Start() - let urlPath = defaultArg urlPath HotReloadSettings.Default.Url + let urlPath = urlPath this.MapHub(urlPath) |> ignore diff --git a/src/Bolero.Templating/Settings.fs b/src/Bolero.Templating/Settings.fs index 6b57fd3..082ec41 100644 --- a/src/Bolero.Templating/Settings.fs +++ b/src/Bolero.Templating/Settings.fs @@ -20,6 +20,12 @@ namespace Bolero.Templating +[] +module HotReloadConstants = + + let [] DefaultUrl = "/bolero-reload" + let [] DefaultReconnectDelay = 5000 + type HotReloadSettings = { Url: string @@ -28,6 +34,6 @@ type HotReloadSettings = static member Default = { - Url = "/bolero-reload" - ReconnectDelayInMs = 5000 + Url = HotReloadConstants.DefaultUrl + ReconnectDelayInMs = HotReloadConstants.DefaultReconnectDelay }