+ Add annotated callbacks.

Callbacks may now be annotated with arbitrary arguments with which the
function will be called. See surflets/test.scm for a (non-didactic) example.

* The extended callbacks justify an extra structure for callbacks:
surflets/callbacks

- make-callback was moved from surflets/utilities to surflets/callbacks
This commit is contained in:
interp 2003-04-14 08:30:27 +00:00
parent c3e7abbdeb
commit c48f952cc5
5 changed files with 64 additions and 16 deletions

View File

@ -63,3 +63,6 @@
(else
(error "annotated-address: unknown message/bad argument(s)"
message (address-name address)))))))

View File

@ -0,0 +1,25 @@
;; Copyright 2002, 2003 Andreas Bernauer
(define (make-callback function)
(call-with-current-continuation
(lambda (exit)
(let* ((req (send/suspend (lambda (new-url)
(exit new-url)))))
(function req)))))
(define (make-annotated-callback function)
(let* ((annotated-address (make-annotated-address))
(dispatch
(lambda (req)
(let ((bindings (get-bindings req)))
(cond
((returned-via annotated-address bindings) =>
(lambda (args)
(apply function (cons req args))))
(else
(error "annotated-callback:
unexpected return values from website"))))))
(callback (make-callback dispatch)))
(lambda args
(annotated-address callback args))))

View File

@ -307,7 +307,6 @@
(define-interface surflets/utilities-interface
(export form-query-list
rev-append
make-callback
generate-unique-name))
;; Intelligent Addresses
@ -319,6 +318,10 @@
; address-add-annotation!
address-annotation))
(define-interface surflets/callbacks-interface
(export make-callback
make-annotated-callback))
;; Returned-via (dispatcher for input-fields and intelligent
;; addresses)
(define-interface surflets/returned-via-interface
@ -401,7 +404,6 @@
threads ;SLEEP
uri ;URI-PATH-LIST->PATH
with-locks ;WITH-LOCK
; inspect-exception ;WITH-INSPECTING-HANDLER
)
(files surflet-handler))
@ -551,8 +553,7 @@
;; Some utilities
(define-structure surflets/utilities surflets/utilities-interface
(open scheme
parse-html-forms
(subset surflet-handler/primitives (send/suspend)))
parse-html-forms)
(files utilities))
@ -563,8 +564,17 @@
(subset uri (escape-uri))
define-record-types
(subset surflets/utilities (generate-unique-name)))
)
(files addresses))
(define-structure surflets/callbacks surflets/callbacks-interface
(open scheme
srfi-23 ;error
surflets/addresses
(subset surflet-handler/primitives (send/suspend))
surflets/bindings
surflets/returned-via)
(files callbacks))
(define-structure surflets/returned-via surflets/returned-via-interface
(open scheme

View File

@ -20,13 +20,6 @@
(rev-app (cdr a) (cons (car a) b))
b)))
(define (make-callback function)
(call-with-current-continuation
(lambda (exit)
(let* ((req (send/suspend (lambda (new-url)
(exit new-url)))))
(function req)))))
;; FIXME: consider creating small names
(define generate-unique-name
(let ((id 0))

View File

@ -1,23 +1,40 @@
(define-structure surflet surflet-interface
(open scheme-with-scsh
surflets
surflets/utilities
surflets/callbacks
httpd-responses)
(begin
(define global '())
(define select (make-select-input-field '("a" "b" "c") #t '(@ (size 2))))
(define (result req arg)
(send-html
`(html (title "Result")
(body (h2 "Result")
(p "Returned via callback with arg" (br)
,(format #f "~s" arg))))))
(define (main req)
(set! global (cons 1 global))
(let* ((addr (make-annotated-address))
(let* ((an-cb (make-annotated-callback result))
(addr (make-annotated-address))
(req (send-html/suspend
(lambda (new-url)
`(html (body (h1 "This is from SUrflet")
(p "called " ,(length global) " times")
(url ,(addr new-url "ab=ba")) (br)
(url ,(addr new-url "be<ta")) (br)
(url ,(addr new-url)) (br)
(abba)
(p "Choose an annotated address:" (br)
(ul
(li (url ,(addr new-url "ab=ba") "ab=ba"))
(li (url ,(addr new-url "be<ta") "be<ta"))
(li (url ,(addr new-url) "<nothing>"))))
(p "Or choose an annotated callback" (br)
(ul
(li (url ,(an-cb 13) "13"))
(li (url ,(an-cb '(1 2 3)) "'(1 2 3)"))
(li (url ,(an-cb "hello") "hello"))
(li (url ,(an-cb #f) "#f"))))
(surflet-form
,new-url
POST