From d11148bcc976709da6387eb70cb0cfa317475e27 Mon Sep 17 00:00:00 2001 From: David Botton Date: Tue, 12 Jan 2021 15:05:00 -0500 Subject: [PATCH] Prevent GC using (run body) --- README.md | 7 ++++++- clog-body.lisp | 16 ++++++++++++++++ clog.lisp | 1 + tutorial/01-tutorial.lisp | 7 ++++++- tutorial/02-tutorial.lisp | 3 ++- tutorial/03-tutorial.lisp | 3 ++- tutorial/04-tutorial.lisp | 4 +++- tutorial/05-tutorial.lisp | 4 +++- tutorial/06-tutorial.lisp | 3 ++- tutorial/07-tutorial.lisp | 2 ++ tutorial/08-tutorial.lisp | 4 +++- tutorial/09-tutorial.lisp | 3 ++- 12 files changed, 48 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 8c50fa0..7d09820 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,12 @@ Sample CLOG app with code base so far (See tutorial 7 for a video game :) : (set-on-click hello-element ; Now we set a function to handle clicks (lambda (obj) ; In this case we use an anonymous function - (setf (color hello-element) "green"))))) + (setf (color hello-element) "green"))) + + (run body))) ; Keep our thread alive until connection closes + ; and prevent garbage collection of our CLOG-Objects + ; until no longer needed. + ;; To see all the events one can set and the many properties and styles that ;; exist, take a look through the CLOG manual or the file clog-element.lisp diff --git a/clog-body.lisp b/clog-body.lisp index 30b5e54..f8149ce 100644 --- a/clog-body.lisp +++ b/clog-body.lisp @@ -43,6 +43,22 @@ :navigator (make-clog-navigator connection-id)))) (set-body (html-document body) body))) +;;;;;;;;; +;; run ;; +;;;;;;;;; + +(defgeneric run (clog-body) + (:documentation "Keeps the main thread alive to prevent garbage +collection of local objects when not using connection-data objects +or global objects.")) + +(defmethod run ((obj clog-body)) + (loop + (if (validp obj) + (sleep 10) + (return (format t "Closing id ~A~%" + (connection-id obj)))))) + ;;;;;;;;;;;; ;; window ;; ;;;;;;;;;;;; diff --git a/clog.lisp b/clog.lisp index 2e0726f..085b3e5 100644 --- a/clog.lisp +++ b/clog.lisp @@ -309,6 +309,7 @@ application." (clog-body class) "CLOG-Body - Properties" + (run generic-function) (window generic-function) (html-document generic-function) (location generic-function) diff --git a/tutorial/01-tutorial.lisp b/tutorial/01-tutorial.lisp index b923311..d92cda9 100644 --- a/tutorial/01-tutorial.lisp +++ b/tutorial/01-tutorial.lisp @@ -23,7 +23,12 @@ (set-on-click hello-element ; Now we set a function to handle clicks (lambda (obj) ; In this case we use an anonymous function (declare (ignore obj)) - (setf (color hello-element) "green"))))) + (setf (color hello-element) "green"))) + + (run body))) ; Keep our thread alive until connection closes + ; and prevent garbage collection of our CLOG-Objects + ; until no longer needed. + ;; To see all the events one can set and the many properties and styles that ;; exist, take a look through the CLOG manual or the file clog-element.lisp diff --git a/tutorial/02-tutorial.lisp b/tutorial/02-tutorial.lisp index ddef493..7654a2d 100644 --- a/tutorial/02-tutorial.lisp +++ b/tutorial/02-tutorial.lisp @@ -27,7 +27,8 @@ (dotimes (n x) (create-child body (format nil "

Clicked ~A times.

" x)) - (scroll-to (window body) 0 (height body)))))))) + (scroll-to (window body) 0 (height body)))))) + (run body))) (defun start-tutorial () "Start turtorial." diff --git a/tutorial/03-tutorial.lisp b/tutorial/03-tutorial.lisp index f87a4fb..3452960 100644 --- a/tutorial/03-tutorial.lisp +++ b/tutorial/03-tutorial.lisp @@ -20,7 +20,8 @@ (dotimes (n x) (create-child body (format nil "

Clicked ~A times.

" x)) - (sleep x))))))) + (sleep x))))) + (run body))) ;; 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_. diff --git a/tutorial/04-tutorial.lisp b/tutorial/04-tutorial.lisp index 6d374a8..fe39ec3 100644 --- a/tutorial/04-tutorial.lisp +++ b/tutorial/04-tutorial.lisp @@ -16,7 +16,9 @@ #'my-on-click) (set-on-click (create-child body "

Click me too!

") - #'my-on-click)) + #'my-on-click) + + (run body)) (defun start-tutorial () "Start turtorial." diff --git a/tutorial/05-tutorial.lisp b/tutorial/05-tutorial.lisp index 57e470d..569fa15 100644 --- a/tutorial/05-tutorial.lisp +++ b/tutorial/05-tutorial.lisp @@ -20,7 +20,9 @@ #'my-on-click) (setf (connection-data-item body "changer") - (create-child body "

I change

"))) + (create-child body "

I change

")) + + (run body)) (defun start-tutorial () "Start turtorial." diff --git a/tutorial/06-tutorial.lisp b/tutorial/06-tutorial.lisp index 5863e75..7c4508e 100644 --- a/tutorial/06-tutorial.lisp +++ b/tutorial/06-tutorial.lisp @@ -31,7 +31,8 @@ (setf (title (html-document body)) "Tutorial 6") (set-on-click (create-child body "

(click me to start!)

") - #'my-on-click)) + #'my-on-click) + (run body)) (defun start-tutorial () "Start turtorial." diff --git a/tutorial/07-tutorial.lisp b/tutorial/07-tutorial.lisp index 3238d85..1803b86 100644 --- a/tutorial/07-tutorial.lisp +++ b/tutorial/07-tutorial.lisp @@ -79,6 +79,8 @@ (sleep .02)))) (error (c) (format t "Lost connection.~%~%~A" c)))) +;; There is no reason to run (run body) here as we check for (validp body) +;; and once the connection dies so does the on-new-window thread. (defun start-tutorial () "Start turtorial." diff --git a/tutorial/08-tutorial.lisp b/tutorial/08-tutorial.lisp index 4ed83f2..6120438 100644 --- a/tutorial/08-tutorial.lisp +++ b/tutorial/08-tutorial.lisp @@ -94,7 +94,9 @@ (setf (positioning div3) :absolute) (set-on-mouse-down div3 'on-mouse-down) - (create-span div3 "Hello world!"))) + (create-span div3 "Hello world!") + + (run body))) (defun start-tutorial () "Start turtorial." diff --git a/tutorial/09-tutorial.lisp b/tutorial/09-tutorial.lisp index 7372850..540b721 100644 --- a/tutorial/09-tutorial.lisp +++ b/tutorial/09-tutorial.lisp @@ -80,7 +80,8 @@ (select-tab p2))) (set-on-click t3 (lambda (obj) (setf last-tab obj) - (select-tab p3)))))) + (select-tab p3)))) + (run body))) (defun start-tutorial () "Start turtorial."