/examples/async.lisp

http://github.com/mtravers/wuwei · Lisp · 53 lines · 46 code · 6 blank · 1 comment · 1 complexity · 2a5686567dd5578839c0980006fc175a MD5 · raw file

  1. (in-package :wu)
  2. (publish-code)
  3. ;;; An example of the use of ASYNC.
  4. (defun fact (n)
  5. (if (zerop n) 1
  6. (* n (fact (- n 1)))))
  7. (publish :path "/async-demo"
  8. :content-type "text/html"
  9. :function 'async-demo)
  10. (defun async-demo (req ent)
  11. (with-session (req ent)
  12. (with-http-response-and-body (req ent)
  13. (html
  14. (:head
  15. (css-includes "wuwei.css")
  16. (javascript-includes "prototype.js" "effects.js" "dragdrop.js"))
  17. (let ((continuation
  18. (ajax-continuation (:keep t :args (n))
  19. (render-update
  20. (:update "answer" "")
  21. (:hide "error_box"))
  22. (let ((n (parse-integer n)))
  23. (assert (> n 0) (n) "Must be positive")
  24. (render-update
  25. (:update "answer"
  26. (async (:pre-text "Wait for it..." :spinner t)
  27. (html (:princ "It's ")
  28. (:princ (log (fact n)))))))))))
  29. (html
  30. (:body
  31. (example-header #.(this-pathname))
  32. (:h3 "Async and error handling demo")
  33. (:princ "This demo shows off asynchronous results (with the async macro), error handling, and a few assorted other features. The checkbox selects between two different error handling styles.")
  34. :p
  35. (checkbox-to-remote "Show errors on page?"
  36. (ajax-continuation (:args (checked) :keep t)
  37. (setf *ajax-error-box?* (equal checked "true")))
  38. *ajax-error-box?*)
  39. :p
  40. (:princ (format nil "Compute log(n!): (try an non-integer argument)"))
  41. ((:form :method :post :onsubmit (remote-function continuation :form t))
  42. ((:input :name "n" :value "5000"))
  43. ((:input :type :submit)))
  44. ((:div :id "answer"))
  45. ((:div :id "error_box" :style "display:none;"))
  46. (tracker)
  47. )))))))