Need help regarding Async and fsi

后端 未结 5 921
我寻月下人不归
我寻月下人不归 2020-12-17 05:24

I\'d like to write some code that runs a sequence of F# scripts (.fsx). The thing is that I could have literally hundreds of scripts and if I do that:

let sh         


        
5条回答
  •  执念已碎
    2020-12-17 05:40

    It is possible to simplify version of Subject from blogpost. instead of returning imitation of event, getSubject can return workflow.

    Result workflow itself is state machine with two states 1. Event wasn't triggered yet: all pending listeners should be registered 2. Value is already set, listener is served immediately In code it will appear like this:

    type SubjectState<'T> = Listen of ('T -> unit) list | Value of 'T
    

    getSubject implementation is trivial

    let getSubject (e : IEvent<_, _>) = 
        let state = ref (Listen [])
        let switchState v = 
            let listeners = 
                lock state (fun () ->
                    match !state with
                    | Listen ls -> 
                        state := Value v 
                        ls
                    | _ -> failwith "Value is set twice"
                )
            for l in listeners do l v
    
        Async.StartWithContinuations(
            Async.AwaitEvent e,
            switchState,
            ignore,
            ignore
        )
    
    Async.FromContinuations(fun (cont, _, _) ->
        let ok, v = lock state (fun () ->
            match !state with
            | Listen ls ->
                state := Listen (cont::ls)
                false, Unchecked.defaultof<_>
            | Value v ->
                true, v
            )
        if ok then cont v
        )
    

提交回复
热议问题