Translating async-await C# code to F# with respect to the scheduler

前端 未结 1 757
北海茫月
北海茫月 2020-12-30 04:07

I wonder if this is too a broad question, but recently I made myself to come across a piece of code I\'d like to be certain on how to translate from C# into proper F#. The j

相关标签:
1条回答
  • 2020-12-30 04:48

    You can use use TaskBuilder in FSharpx and pass in TaskScheduler.Current. Here's my attempt at translating RefreshHubs. Note that Task<unit> is used in lieu of Task.

    let RefreshHubs _ =
        let task = TaskBuilder(scheduler = TaskScheduler.Current)
        task {
            let addresses = 
                RoleEnvironment.Roles.["GPSTracker.Web"].Instances
                |> Seq.map (fun instance ->
                    let endpoint = instance.InstanceEndpoints.["InternalSignalR"]
                    sprintf "http://%O" endpoint.IPEndpoint
                )
                |> Seq.toList
    
            let newHubs = addresses |> List.filter (not << hubs.ContainsKey)
            let deadHubs = hubs.Keys |> Seq.filter (fun x -> 
                not (List.exists ((=) x) addresses))
    
            // remove dead hubs
            deadHubs |> Seq.iter (hubs.Remove >> ignore)
    
            // add new hubs
            let! _ = Task.WhenAll [| for hub in newHubs -> AddHub hub |]
            return ()
        }
    
    0 讨论(0)
提交回复
热议问题