Skip to content

Commit

Permalink
hook receive can return an IDisposable instead
Browse files Browse the repository at this point in the history
  • Loading branch information
pchalamet committed Feb 12, 2021
1 parent 5ea4c03 commit e5a7247
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 22 deletions.
6 changes: 2 additions & 4 deletions FBus.GenericHost.Tests/Container.fs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@ let startServer<'t> (session: FBus.Testing.Session) name callback =

let checkErrorHook = {
new IBusHook with
member this.OnEnter ctx = null
member this.OnBeforeProcessing ctx = null

member this.OnError ctx msg exn state =
member this.OnError ctx msg exn =
failwithf "No error shall be raised: %A" exn

member this.OnLeave ctx state = ()
}

let svcCollection = ServiceCollection() :> IServiceCollection
Expand Down
5 changes: 2 additions & 3 deletions FBus.Tests/Control.fs
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,13 @@ let ``Test bus control`` () =
let mutable onError = 0

let hook = { new FBus.IBusHook with
member _.OnEnter ctx = null
member this.OnError ctx msg exn state =
member _.OnBeforeProcessing ctx = null
member this.OnError ctx msg exn =
match msg with
| :? StringMessage as s when s.String = fatalString -> match exn with
| :? TestBusException -> onError <- onError + 1
| _ -> ()
| _ -> ()
member _.OnLeave ctx state = ()
}

let bus = configure() |> withName client
Expand Down
6 changes: 2 additions & 4 deletions FBus.Tests/Transports.fs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ type InMemoryHandler2() =
let startServer<'t> (session: FBus.Testing.Session) name =
let checkErrorHook = {
new IBusHook with
member _.OnEnter ctx = null
member _.OnError ctx msg exn state =
failwithf "No error shall be raised: %A" exn
member _.OnLeave ctx state = ()
member _.OnBeforeProcessing ctx = null
member _.OnError ctx msg exn = failwithf "No error shall be raised: %A" exn
}

let serverBus = FBus.Builder.configure() |> session.Use
Expand Down
8 changes: 3 additions & 5 deletions FBus/Control.fs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Bus(busConfig: BusConfiguration) =
member _.Publish msg = conversationHeaders() |> publish msg
member _.Send client msg = conversationHeaders() |> send client msg }

let hookState = busConfig.Hook |> Option.map (fun hook -> hook.OnEnter ctx) |> Option.defaultValue null
use hookState = busConfig.Hook |> Option.map (fun hook -> hook.OnBeforeProcessing ctx) |> Option.defaultValue null

try
let handlerInfo = match busConfig.Handlers |> Map.tryFind msgType with
Expand All @@ -50,12 +50,10 @@ type Bus(busConfig: BusConfiguration) =

msg <- busConfig.Serializer.Deserialize handlerInfo.MessageType content
handlerInfo.CallSite.Invoke(handler, [| ctx; msg |]) |> ignore

busConfig.Hook |> Option.iter (fun hook -> hook.OnLeave ctx hookState)
with
| :? Reflection.TargetInvocationException as tie -> busConfig.Hook |> Option.iter (fun hook -> hook.OnError ctx msg tie.InnerException hookState)
| :? Reflection.TargetInvocationException as tie -> busConfig.Hook |> Option.iter (fun hook -> hook.OnError ctx msg tie.InnerException)
reraise()
| exn -> busConfig.Hook |> Option.iter (fun hook -> hook.OnError ctx msg exn hookState)
| exn -> busConfig.Hook |> Option.iter (fun hook -> hook.OnError ctx msg exn)
reraise()

let newConversationHeaders () =
Expand Down
6 changes: 2 additions & 4 deletions FBus/FBus.fs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ type IBusSerializer =
abstract Deserialize: Type -> ReadOnlyMemory<byte> -> obj

type IBusHook =
abstract OnEnter: ctx:IBusConversation -> obj
abstract OnError: ctx:IBusConversation -> msg:obj -> exn: Exception -> state: obj -> unit
abstract OnLeave: ctx:IBusConversation -> state: obj -> unit

abstract OnBeforeProcessing: ctx:IBusConversation -> IDisposable
abstract OnError: ctx:IBusConversation -> msg:obj -> exn: Exception -> unit

type BusConfiguration =
{ Name: string
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,7 @@ See `FBus.IBusHook`.

FBus.IBusHook | Description | Comments
--------------|-------------|---------
`OnEnter` | Invoked before processing a message | Must not throw.
`OnLeave` | Invoked after processing a message without error | Must not throw.
`OnBeforeProcessing` | Invoked before processing a message | Must not throw - can return an IDisposable object released once processing is done.
`OnError` | Invoked on error | Must not throw.

## Available extensions
Expand Down

0 comments on commit e5a7247

Please sign in to comment.