public
Description: An easy way to make web apps (in PLT Scheme)
Homepage: http://blog.leftparen.com
Clone URL: git://github.com/vegashacker/leftparen.git
Click here to lend your support to: leftparen and make a donation at www.pledgie.com !
leftparen / closures.scm
100644 100 lines (87 sloc) 3.184 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#lang scheme/base
 
(require (file "util.scm")
         "web-export.ss"
         (file "web-support.scm")
         (file "settings.scm"))
 
(provide add-closure!
         call-closure
         closure-key->url
         make-closure-key
         body-as-closure-key
         body-as-url
         handle-closure-in-req
         num-closures-in-memory
         )
 
(define-syntax body-as-closure-key-aux
  (syntax-rules ()
    ((_ req-iden key-iden is-sticky-expr body ...)
     (let ((is-sticky is-sticky-expr))
       (add-closure! #:key key-iden
                     (lambda (req-iden)
                       ;; first cleanup after itself (if not sticky)
                       (unless is-sticky (hash-remove! CLOSURES key-iden))
                       ;; then run the actual closure...
                       body ...))))))
 
(define-syntax body-as-closure-key
  (syntax-rules ()
    ((_ (req-iden #:sticky) body ...)
     (let ((key-iden (make-closure-key)))
       (body-as-closure-key-aux req-iden key-iden #t body ...)))
    ((_ (req-iden key-expr #:sticky) body ...)
     (let ((key-iden key-expr))
       (body-as-closure-key-aux req-iden key-iden #t body ...)))
    ((_ (req-iden) body ...)
     (let ((key-iden (make-closure-key)))
       (body-as-closure-key-aux req-iden key-iden #f body ...)))
    ((_ (req-iden key-expr) body ...)
     (let ((key-iden key-expr))
       (let ((key-iden key-expr))
         (body-as-closure-key-aux req-iden key-iden #f body ...))))))
 
;;
;; body-as-url
;;
;; Forms:
;; (body-as-url (req) body ...)
;; (body-as-url (req fn-key) body ...)
;; (body-as-url (req #:sticky) body ...)
;; (body-as-url (req fn-key #:sticky) body ...)
;;
;; If fn-key is given, it will be the key used to map to the body.
;; (This provides a way for the developer to reuse fns in certain situations.)
;;
;; If #:sticky appears at the end of the first argument, then the closure will not
;; be removed form memory after it's invoked (on a server restart, however, all closures
;; --regardless of stickiness--are cleared).
;;
(define-syntax body-as-url
  (syntax-rules ()
    ((_ (identifiers ...) body ...)
     (closure-key->url (body-as-closure-key (identifiers ...) body ...)))))
 
(define (closure-key->url clos-key)
  (format "~A?~A=~A"
          (setting *WEB_APP_URL*)
          (setting *CLOSURE_URL_KEY*)
          clos-key))
 
(define-syntax handle-closure-in-req
  (syntax-rules ()
    ((_ req no-closure-body ...)
     (let ((url-key (setting *CLOSURE_URL_KEY*))
           (binds (request-bindings req)))
       (if (exists-binding? url-key binds)
           (call-closure (extract-binding/single url-key binds) req)
           (begin no-closure-body ...))))))
 
(define CLOSURES (make-hash))
 
(define (make-closure-key)
  (random-key-string 20))
 
;; returns the key
(define (add-closure! clos #:key (key #f))
  (let ((key (or key (make-closure-key))))
    (hash-set! CLOSURES key clos)
    key))
 
(define (call-closure key req)
  ((hash-ref CLOSURES key (lambda ()
                            (lambda (req)
                              (format "Expired or missing function '~A'." key))))
   req))
 
(define (num-closures-in-memory)
  (hash-count CLOSURES))