Understanding Sending / Pausing / Sending in Racket Web Framework

I am trying to understand an example related to the send / pause / send code in the Racket Web Framework tutorial http://docs.racket-lang.org/continue/ .

For convenience, there is a code that puzzles me:

#lang web-server/insta
; start: request -> response
   (define (start request)
      (show-counter 0 request))

; show-counter: number request -> doesn't return
; Displays a number that hyperlinked: when the link is pressed,
; returns a new page with the incremented number.
(define (show-counter n request)
  (local [(define (response-generator embed/url)
            (response/xexpr
             `(html (head (title "Counting example"))
                    (body
                     (a ((href ,(embed/url next-number-handler)))
                        ,(number->string n))))))

          (define (next-number-handler request)
            (show-counter (+ n 1) request))]

    (send/suspend/dispatch response-generator)))

I have two questions:

(Most important) Where is / url built in? I don’t see that it is defined in this code, but my understanding of continuations is rudimentary, so maybe I am missing something.

What is the purpose of local? I can remove it and it seems the code works the same.

+4
source share
1 answer

The embed / url function is the argument of the response generator:

(define (response-generator embed/url) ...)

(send/suspend/dispatch response-generator), :

1. a procedure `p` given a "continuation" (here next-number-handler)" 
   generates an url
2. the function `response-generator` is called with `p` as argument.
3. the page `(html ... ,(embed-url next-number-handler)) is generated
   (note: (embed-url next-number-handler) calls `p` and the resulting url is inserted into the page)

4. [send]     the page is sent to the client
5. [suspend]  the server suspends the program
6. [dispatch] receives an request generated by clicking link whose url were
              generated in 3.  The handler associated to the url 
              (here next-number-handler) is looked up, and the handler 
              is called.

send/suspend/dispatch, :

(define (send/suspend/dispatch response-generator )
  (let/ec escape
    (define (callback->url callback)
      (let/ec return-url
        (escape (callback (send/suspend return-url)))))
    (send/back (response-generator callback->url))))
+3

All Articles