Git Product home page Git Product logo

Comments (5)

JonCanning avatar JonCanning commented on May 10, 2024

Remember, an HttpHandler looks like this:

HttpContext -> Async<HttpContext option>

So noParameterFunction() is executed, logs "Hello" and returns an HttpHandler that will print "Hello" when it is executed

Try this

let noParameterFunction ctx =
  async {
    printfn "Hello"
    return! text "Hello" ctx
  }

from giraffe.

gerardtoconnor avatar gerardtoconnor commented on May 10, 2024

@JonCanning is spot on

It can be very useful though for when you want to pre-compile data before returning the handler as the code body is only executed once at startup and does not need to be run each time.

If you remove currying, it is easy to see why the body only runs once as per below which is the same as yours only wrapping a ctx function:

let noParameterFunction () =
    printfn "Hello"
   /// val text : string -> HttpContext -> Async<HttpContext option>
    (fun ctx -> text "Hello" ctx)

although Jon's example is fine, you can just as easily avoid async wrap with

let noParameterFunction ctx =
    printfn "Hello"
    text "Hello" ctx

As there is no async binding other than the return, which is itself producing an async result. You only need to use async binding when your handler is itself doing some waiting, otherwise just return the async of the internal handler being used.

from giraffe.

vtquan avatar vtquan commented on May 10, 2024

Thank you for the explanation and workarounds. I am still not clear on why the code body only runs once. Is it because the HttpHandler is returning an Async result so all non async code is only evaluated once? I look at the curry-less version and I don't see why it would print "Hello" each time the function is called.

from giraffe.

gerardtoconnor avatar gerardtoconnor commented on May 10, 2024

This might help explain.
Every HttpHandler is a function, it takes in a HttpContext and returns Async<HttpContext option>, I find that a nice way to write all handlers, regardless of multiple inputs are

let functionValue =
    fun (ctx : HttpContext) -> // route execution runs from here
        printfn "Hello"
        text "Hello" ctx

let myText (txt:string) =
    fun (ctx: HttpContext) ->
         printfn txt
         text txt ctx

let preCompiled () =
     // only evaluted once when funtion called
     let serverStart = DateTime.Now.ToString()
     // now return a HttpHandler Function that contains reference to our value evaluated on startup
     fun ctx -> text serverStart ctx // route execution runs from here

The value webApp that contains all the routes and compositions is fully evaluated into a tree of HttpHandler functions that bind together, therefore expressions in your pipeline will evaluate till they return HttpHandlers to bind & Compose to the rest of the pipeline. It is similar to using the >> operator, composing two normal functions together into a pipeline, if you did fn1 = fn2 () >> fn3, provided the result of fn2 () is a function of the same type for the input of fn3, we can compose together into one fn1 function, but fn2 is never run again after initial execution as it has returned its function result.

from giraffe.

vtquan avatar vtquan commented on May 10, 2024

I got it now. The functions are evaluated so that they can be passed on to form the HttpHandler function. Thank you.

from giraffe.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.