Make event handlers in Options class standard IEvent for idiomatic F# #131
Replies: 8 comments 1 reply
-
@BentTranberg I think this would be a breaking change. Maybe you can suggest it for the V2 https://github.com/nats-io/nats.net.v2 |
Beta Was this translation helpful? Give feedback.
-
I suspect that repo is not public, since the link doesn't work for me. |
Beta Was this translation helpful? Give feedback.
-
Yeah, sorry about that. I'm transferring this to that repo. |
Beta Was this translation helpful? Give feedback.
-
@BentTranberg is this something still required in the new codebase here? (sorry don't know much about F#) |
Beta Was this translation helpful? Give feedback.
-
Any F# developers? |
Beta Was this translation helpful? Give feedback.
-
Sorry, I'm drowning in work these days, so haven't had a chance. Will try this weekend. |
Beta Was this translation helpful? Give feedback.
-
No worries. I thought maybe you weren't interested anymore. Please take your time. |
Beta Was this translation helpful? Give feedback.
-
It works the way I would like it to work, so I guess the issue can be closed now. I usually don't close issues myself, but take the liberty to do it this time. I've tested not just compilation, but also runtime. As if that was needed, haha. Of course this was not surprising in the least. I assume the problem I reported is in a source that was not reused here, since this one is based on AlterNats from what I read somewhere. I'm posting my test snippet here, just in case anybody is interested. Simply create an F# console application, overwrite the contents of Program.fs with this, and add NATS.Client.Core from NuGet or wherever. You will notice that using NatsOpts from F# is very cumbersome. The So here's the snippet. If you run the program, then shut down the NATS server, you will see all events fire, and not just module TestNatsFSharp.Main
open System
open System.Threading.Tasks
open Microsoft.Extensions.Logging
open NATS.Client.Core
[<EntryPoint>]
let main (args: string array) =
let url = NatsOpts.Default.Url
let name = NatsOpts.Default.Name
let echo = NatsOpts.Default.Echo
let verbose = NatsOpts.Default.Verbose
let headers = NatsOpts.Default.Headers
let authOpts = NatsOpts.Default.AuthOpts
let tlsOpts = NatsOpts.Default.TlsOpts
let serializer = NatsOpts.Default.Serializer
let loggerFactory = NatsOpts.Default.LoggerFactory
let writerBufferSize = NatsOpts.Default.WriterBufferSize
let readerBufferSize = NatsOpts.Default.ReaderBufferSize
let useThreadPoolCallback = NatsOpts.Default.UseThreadPoolCallback
let inboxPrefix = NatsOpts.Default.InboxPrefix
let noRandomize = NatsOpts.Default.NoRandomize
let pingInterval = NatsOpts.Default.PingInterval
let maxPingOut = NatsOpts.Default.MaxPingOut
let reconnectWait = NatsOpts.Default.ReconnectWait
let reconnectJitter = NatsOpts.Default.ReconnectJitter
let connectTimeout = NatsOpts.Default.ConnectTimeout
let objectPoolSize = NatsOpts.Default.ObjectPoolSize
let requestTimeout = NatsOpts.Default.RequestTimeout
let commandTimeout = NatsOpts.Default.CommandTimeout
let subscriptionCleanUpInterval = NatsOpts.Default.SubscriptionCleanUpInterval
let writerCommandBufferLimit = NatsOpts.Default.WriterCommandBufferLimit
let headerEncoding = NatsOpts.Default.HeaderEncoding
let waitUntilSent = NatsOpts.Default.WaitUntilSent
// Change from default above.
let loggerFactory = new MinimumConsoleLoggerFactory(LogLevel.Error)
let options = NatsOpts(url, name, echo, verbose, headers, authOpts, tlsOpts,
serializer, loggerFactory, writerBufferSize, readerBufferSize, useThreadPoolCallback,
inboxPrefix, noRandomize, pingInterval, maxPingOut, reconnectWait, reconnectJitter,
connectTimeout, objectPoolSize, requestTimeout, commandTimeout,
subscriptionCleanUpInterval, writerCommandBufferLimit, headerEncoding, waitUntilSent)
let connection = new NatsConnection(options)
if true then
connection.ConnectionOpened.Add (fun _ -> Console.WriteLine "ConnectionOpened listener called.")
connection.ConnectionDisconnected.Add (fun _ -> Console.WriteLine "ConnectionDisconnected listener called.")
connection.ReconnectFailed.Add (fun _ -> Console.WriteLine "ReconnectFailed listener called.")
else
connection.ConnectionOpened.AddHandler (fun _ _ -> Console.WriteLine "ConnectionOpened delegate called.")
connection.ConnectionDisconnected.AddHandler (fun _ _ -> Console.WriteLine "ConnectionDisconnected delegate called.")
connection.ReconnectFailed.AddHandler (fun _ _ -> Console.WriteLine "ReconnectFailed delegate called.")
let myTask = task {
Console.WriteLine "Connecting..."
do! connection.ConnectAsync()
Console.WriteLine "Connected... now waiting 1 sec ..."
do! Task.Delay 1000
//Console.WriteLine "... then disposing ..."
//do! connection.DisposeAsync()
//Console.WriteLine "Disposed... now waiting 1 sec ..."
//do! Task.Delay 1000
Console.WriteLine "Exiting task."
}
myTask.Wait()
Console.WriteLine "Press any key to exit application."
Console.ReadKey() |> ignore
0
I did not find a DisconnectAsync or even a Disconnect. Maybe it's coming? I plan to use this library in the future. I hope to be able to test it more with F#. I already have some experience with AlterNats and F#. |
Beta Was this translation helpful? Give feedback.
-
Feature Request
Make events in the Options class standard IEvent. This will make it easier to exploit more advanced features related to the use of events in F#, and probably also in C#.
Use Case:
These are not currently possible in F#.
opts.DisconnectedEventHandler.Add disconnectedEvent
opts.DisconnectedEventHandler.AddHandler disconnectedEventHandler
// Equivalent to += in C#.Proposed Change:
Add the
event
keyword to the declarations of events in theOptions
class, so that the events become standardIEvent
, and theAdd
andAddHandler
methods appear in F#.Who Benefits From The Change(s)?
F# coders in particular, but most probably also C# coders wanting to use Rx, Observable, IEvent dependent features.
Alternative Approaches
The current and probably only approach to coding these events in F# is like this, which is all right but a bit unexpected, and so a slight hassle.
Beta Was this translation helpful? Give feedback.
All reactions