Prevent GC using (run body)

This commit is contained in:
David Botton 2021-01-12 15:05:00 -05:00
parent 68882c1692
commit d11148bcc9
12 changed files with 48 additions and 9 deletions

View file

@ -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 (set-on-click hello-element ; Now we set a function to handle clicks
(lambda (obj) ; In this case we use an anonymous function (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 ;; 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 ;; exist, take a look through the CLOG manual or the file clog-element.lisp

View file

@ -43,6 +43,22 @@
:navigator (make-clog-navigator connection-id)))) :navigator (make-clog-navigator connection-id))))
(set-body (html-document body) body))) (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 ;; ;; window ;;
;;;;;;;;;;;; ;;;;;;;;;;;;

View file

@ -309,6 +309,7 @@ application."
(clog-body class) (clog-body class)
"CLOG-Body - Properties" "CLOG-Body - Properties"
(run generic-function)
(window generic-function) (window generic-function)
(html-document generic-function) (html-document generic-function)
(location generic-function) (location generic-function)

View file

@ -23,7 +23,12 @@
(set-on-click hello-element ; Now we set a function to handle clicks (set-on-click hello-element ; Now we set a function to handle clicks
(lambda (obj) ; In this case we use an anonymous function (lambda (obj) ; In this case we use an anonymous function
(declare (ignore obj)) (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 ;; 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 ;; exist, take a look through the CLOG manual or the file clog-element.lisp

View file

@ -27,7 +27,8 @@
(dotimes (n x) (dotimes (n x)
(create-child body (create-child body
(format nil "<p>Clicked ~A times.</p>" x)) (format nil "<p>Clicked ~A times.</p>" x))
(scroll-to (window body) 0 (height body)))))))) (scroll-to (window body) 0 (height body))))))
(run body)))
(defun start-tutorial () (defun start-tutorial ()
"Start turtorial." "Start turtorial."

View file

@ -20,7 +20,8 @@
(dotimes (n x) (dotimes (n x)
(create-child body (create-child body
(format nil "<p>Clicked ~A times.</p>" x)) (format nil "<p>Clicked ~A times.</p>" x))
(sleep x))))))) (sleep x)))))
(run body)))
;; Running this version of the last tutorial and clicking quickly on the (click me!) ;; 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_. ;; will demonstrate an important aspect of CLOG, events can happen in _parallel_.

View file

@ -16,7 +16,9 @@
#'my-on-click) #'my-on-click)
(set-on-click (create-child body "<h3>Click me too!</h3>") (set-on-click (create-child body "<h3>Click me too!</h3>")
#'my-on-click)) #'my-on-click)
(run body))
(defun start-tutorial () (defun start-tutorial ()
"Start turtorial." "Start turtorial."

View file

@ -20,7 +20,9 @@
#'my-on-click) #'my-on-click)
(setf (connection-data-item body "changer") (setf (connection-data-item body "changer")
(create-child body "<h1>I change</h1>"))) (create-child body "<h1>I change</h1>"))
(run body))
(defun start-tutorial () (defun start-tutorial ()
"Start turtorial." "Start turtorial."

View file

@ -31,7 +31,8 @@
(setf (title (html-document body)) "Tutorial 6") (setf (title (html-document body)) "Tutorial 6")
(set-on-click (create-child body "<h1>(click me to start!)</h1>") (set-on-click (create-child body "<h1>(click me to start!)</h1>")
#'my-on-click)) #'my-on-click)
(run body))
(defun start-tutorial () (defun start-tutorial ()
"Start turtorial." "Start turtorial."

View file

@ -79,6 +79,8 @@
(sleep .02)))) (sleep .02))))
(error (c) (error (c)
(format t "Lost connection.~%~%~A" 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 () (defun start-tutorial ()
"Start turtorial." "Start turtorial."

View file

@ -94,7 +94,9 @@
(setf (positioning div3) :absolute) (setf (positioning div3) :absolute)
(set-on-mouse-down div3 'on-mouse-down) (set-on-mouse-down div3 'on-mouse-down)
(create-span div3 "Hello world!"))) (create-span div3 "Hello world!")
(run body)))
(defun start-tutorial () (defun start-tutorial ()
"Start turtorial." "Start turtorial."

View file

@ -80,7 +80,8 @@
(select-tab p2))) (select-tab p2)))
(set-on-click t3 (lambda (obj) (set-on-click t3 (lambda (obj)
(setf last-tab obj) (setf last-tab obj)
(select-tab p3)))))) (select-tab p3))))
(run body)))
(defun start-tutorial () (defun start-tutorial ()
"Start turtorial." "Start turtorial."