Skip to content

Commit

Permalink
Implement C# friendly API
Browse files Browse the repository at this point in the history
  • Loading branch information
xperiandri committed Nov 28, 2023
1 parent 4fd134b commit 0b60da2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
9 changes: 9 additions & 0 deletions src/Bolero.Templating.Server/Attributes.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Bolero.Templating.Server

open System

[<AttributeUsage(AttributeTargets.Parameter ||| AttributeTargets.Field)>]
type internal TimeSpanConstantAttribute(miliseconds) =
inherit Attribute()
member _.Value = TimeSpan.FromMilliseconds miliseconds

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<OutputType>Library</OutputType>
</PropertyGroup>
<ItemGroup>
<Compile Include="Attributes.fs" />
<Compile Include="Templating.fs" />
<None Include="paket.references" />
<Content Include="paket.template" />
Expand Down
30 changes: 21 additions & 9 deletions src/Bolero.Templating.Server/Templating.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -178,27 +179,38 @@ module Impl =
for handler in handlers do
handler.Dispose()

[<Extension>]
type ServerTemplatingExtensions =
[<AutoOpen>]
module private Constants =

let [<Literal>] DefaultTemplateDir = "."
let [<Literal>] DefaultDelay = 100.


[<Extension; AbstractClass; Sealed>]
type ServerTemplatingExtensions() =

[<Extension>]
static member AddHotReload(this: IServiceCollection, configure: WatcherConfig -> WatcherConfig) : IServiceCollection =
static member AddHotReload(this: IServiceCollection, configure: Func<WatcherConfig, WatcherConfig>) : 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<Watcher>()
.AddTransient<IClient, Client>()

[<Extension>]
static member AddHotReload(this: IServiceCollection, ?templateDir: string, ?delay: TimeSpan) : IServiceCollection =
static member AddHotReload(
this: IServiceCollection,
[<Optional; DefaultParameterValue(DefaultTemplateDir)>] templateDir: string,
[<Optional; TimeSpanConstantAttribute(DefaultDelay)>] delay: TimeSpan)
: IServiceCollection =
ServerTemplatingExtensions.AddHotReload(this, fun config ->
{
Directory = defaultArg templateDir config.Directory
Delay = defaultArg delay config.Delay
Directory = templateDir
Delay = delay
})

[<Extension>]
static member UseHotReload(this: IEndpointRouteBuilder, ?urlPath: string) : unit =
static member UseHotReload(this: IEndpointRouteBuilder, [<Optional; DefaultParameterValue(HotReloadConstants.DefaultUrl)>] urlPath: string) : unit =
this.ServiceProvider.GetService<Watcher>().Start()
let urlPath = defaultArg urlPath HotReloadSettings.Default.Url
let urlPath = urlPath
this.MapHub<HotReloadHub>(urlPath) |> ignore
10 changes: 8 additions & 2 deletions src/Bolero.Templating/Settings.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@

namespace Bolero.Templating

[<RequireQualifiedAccess>]
module HotReloadConstants =

let [<Literal>] DefaultUrl = "/bolero-reload"
let [<Literal>] DefaultReconnectDelay = 5000

type HotReloadSettings =
{
Url: string
Expand All @@ -28,6 +34,6 @@ type HotReloadSettings =

static member Default =
{
Url = "/bolero-reload"
ReconnectDelayInMs = 5000
Url = HotReloadConstants.DefaultUrl
ReconnectDelayInMs = HotReloadConstants.DefaultReconnectDelay
}

0 comments on commit 0b60da2

Please sign in to comment.