/src/async.lisp

http://github.com/mtravers/wuwei · Lisp · 55 lines · 18 code · 9 blank · 28 comment · 0 complexity · c1939389f77da039c000d255e84909ab MD5 · raw file

  1. (in-package :wu)
  2. ;;; +=========================================================================+
  3. ;;; | Copyright (c) 2009, 2010 Mike Travers and CollabRx, Inc |
  4. ;;; | |
  5. ;;; | Released under the MIT Open Source License |
  6. ;;; | http://www.opensource.org/licenses/mit-license.php |
  7. ;;; | |
  8. ;;; | Permission is hereby granted, free of charge, to any person obtaining |
  9. ;;; | a copy of this software and associated documentation files (the |
  10. ;;; | "Software"), to deal in the Software without restriction, including |
  11. ;;; | without limitation the rights to use, copy, modify, merge, publish, |
  12. ;;; | distribute, sublicense, and/or sell copies of the Software, and to |
  13. ;;; | permit persons to whom the Software is furnished to do so, subject to |
  14. ;;; | the following conditions: |
  15. ;;; | |
  16. ;;; | The above copyright notice and this permission notice shall be included |
  17. ;;; | in all copies or substantial portions of the Software. |
  18. ;;; | |
  19. ;;; | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
  20. ;;; | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
  21. ;;; | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
  22. ;;; | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
  23. ;;; | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
  24. ;;; | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
  25. ;;; | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
  26. ;;; +=========================================================================+
  27. ;;; Author: Mike Travers
  28. (export '(async))
  29. ;;; Render a DIV with PRE-TEXT now, and replace it with the results of BODY when that completes.
  30. ;;; Note that the use of continuations replaces a ton of special-purpose machinery.
  31. (defmacro async ((&key (pre-text "waiting...") spinner) &body body)
  32. `(let ((%id (gensym "async")))
  33. (html
  34. ((:div :id %id)
  35. (:princ ,pre-text)
  36. (when ,spinner
  37. (html (image-tag "spinner.gif"))))
  38. ((:script :type "text/javascript")
  39. (:princ
  40. (remote-function
  41. (ajax-continuation ()
  42. (render-update
  43. (:update
  44. %id
  45. ,@body)))
  46. :in-function? nil))))))