Tutorial 03

This commit is contained in:
David Botton 2021-01-06 02:13:34 -05:00
parent a378dff2f7
commit f897486c81
2 changed files with 72 additions and 0 deletions

33
tutorial/03-tutorial.lisp Normal file
View file

@ -0,0 +1,33 @@
(defpackage #:clog-user
(:use #:cl #:clog)
(:export start-tutorial))
(in-package :clog-user)
(defun on-new-window (body)
"On-new-window handler."
(setf (title (html-document body)) "Tutorial 2")
(let ((hello-element
(create-child body "<h1>Hello World! (click me!)</h1>")))
(let ((x 0))
(set-on-click hello-element
(lambda ()
(incf x)
(dotimes (n x)
(create-child body
(format nil "<p>Clicked ~A times.</p>" x))
(sleep x)))))))
;; Running this version of the last tutorial and clicking quickly on the (click me!)
;; will demonstrate an important aspect of CLOG, events can happen in _parallel_.
;; This means that appropriate precautions to thread protect data should be taken
;; and that events do not wait for previous event handlers to complete.
(defun start-tutorial ()
"Start turtorial."
(initialize #'on-new-window)
(open-browser))

39
tutorial/README.md Normal file
View file

@ -0,0 +1,39 @@
To run a tutorial, start emacs/slime or your CL Lisp in the common-lisp/clog directory:
```
CL-USER> (ql:quickload :clog)
To load "clog":
Load 1 ASDF system:
clog
; Loading "clog"
...........................
(:CLOG)
```
Load the demo:
```
CL-USER> (load "/Users/dbotton/common-lisp/clog/tutorial/01-tutorial.lisp")
#P"/Users/dbotton/common-lisp/clog/tutorial/01-tutorial.lisp"
```
Start the demo:
```
CL-USER> (clog-user:start-tutorial)
Hunchentoot server is started.
Listening on 0.0.0.0:8080.
HTTP listening on : 0.0.0.0:8080
HTML Root : static-files/
Boot file default : /boot.html
```
Most demos startup a browser, if not use http://127.0.0.1:8080
Tutorial Summary
01-tutorial.lisp - Hello World
02-tutorial.lisp - Closures in CLOG
03-tutorial.lisp - Events fire in parallel