[plt-scheme] Parameters and servlets
Jay McCarthy wrote:
> What are you actually trying to do? Obviously this example is
> contrived. I have a system called web-cells that solve the problems I
> was having with what I thought I wanted parameters to do.
I couldn't figure out how web-cells were meant to be used from
the source alone, so I experimented a little. The result works
for me (right now at least).
A persistent parameter is created with make-persistent-parameter:
(define current-page-title
(make-persistent-parameter "persistent/page-title"
"A title" ))
The call returns a normal parameter as result (so parameterize
can be used), and at the same time registers a saver, which
when called will save the current value of the parameter to disk.
If there exists a saved value make-persitent-parameter used
that value, otherwise it uses the given one.
The servlet-writer must remember to call save-persitent-parameters
in the function start just before the response is returned.
Since read/write is used only readable/writeable values must be
used - but it is straightforward to use serialize.ss if one
needs structs.
(module persistent-parameter mzscheme
(provide make-persistent-parameter
save-persistent-parameters)
(define current-saver (make-parameter (lambda () 'done)))
; saves all persistent parameters to disk
(define (save-persistent-parameters)
((current-saver)))
(define (make-persistent-parameter file value)
(define (save-value o)
(with-output-to-file file
(lambda () (write o))
'replace))
(define (read-value)
(with-input-from-file file
(lambda () (read))))
(if (file-exists? file)
(set! value (read-value))
(save-value value))
; We can't save the variable in the parameter's guard
; (since we can't "unsave" in in the end of a (parameterize ...)
; In stead we make a will-executor.
(let* ([p (make-parameter value)]
[cs (current-saver)])
(current-saver
(lambda ()
(save-value (p))
(cs)))
p)))
--
Jens Axel Søgaard