On this page:
web-cell?
make-web-cell
web-cell-ref
web-cell-shadow
2.7.1 Stateless Web Cells
web-cell?
make-web-cell
web-cell-ref
web-cell-shadow
Version: 4.1.3

2.7 Web Cells

 (require web-server/servlet/web-cells)

The web-server/servlet/web-cells library provides the interface to Web cells.

A Web cell is a kind of state defined relative to the frame tree. The frame-tree is a mirror of the user’s browsing session. Every time a continuation is invoked, a new frame (called the current frame) is created as a child of the current frame when the continuation was captured.

You should use Web cells if you want an effect to be encapsulated in all interactions linked from (in a transitive sense) the HTTP response being generated. For more information on their semantics, consult the paper "Interaction-Safe State for the Web".

(web-cell? v)  boolean?
  v : any/c

Determines if v is a web-cell.

(make-web-cell v)  web-cell?
  v : any/c

Creates a web-cell with a default value of v.

(web-cell-ref wc)  any/c
  wc : web-cell?

Looks up the value of wc found in the nearest frame.

(web-cell-shadow wc v)  void
  wc : web-cell?
  v : any/c

Binds wc to v in the current frame, shadowing any other bindings to wc in the current frame.

Below is an extended example that demonstrates how Web cells allow the creation of reusable Web abstractions without requiring global transformations of the program into continuation or store passing style.

  #lang web-server/insta
  
  (define (start initial-request)
   (define counter1 (make-counter))
   (define counter2 (make-counter))
   (define include1 (include-counter counter1))
   (define include2 (include-counter counter2))
   (send/suspend/dispatch
    (lambda (embed/url)
      `(html
        (body (h2 "Double Counters")
              (div (h3 "First")
                   ,(include1 embed/url))
              (div (h3 "Second")
                   ,(include2 embed/url)))))))
  
  (define (make-counter)
   (make-web-cell 0))
  
  (define (include-counter a-counter)
   (let/cc k
     (let loop ()
       (k
        (lambda (embed/url)
          `(div (h3 ,(number->string (web-cell-ref a-counter)))
                (a ([href
                     ,(embed/url
                       (lambda _
                         ; A new frame has been created
                         (define last (web-cell-ref a-counter))
                         ; We can inspect the value at the parent
                         (web-cell-shadow a-counter (add1 last))
                         ; The new frame has been modified
                         (loop)))])
                   "+")))))))

2.7.1 Stateless Web Cells

 (require web-server/lang/web-cells)

The web-server/lang/web-cells library provides the same API as web-server/servlet/web-cells, but in a way compatible with the Web Language. The one difference is that make-web-cell is syntax, rather than a function.

(web-cell? v)  boolean?
  v : any/c
(make-web-cell default-expr)
(web-cell-ref wc)  any/c
  wc : web-cell?
(web-cell-shadow wc v)  void
  wc : web-cell?
  v : any/c

See web-server/servlet/web-cells.