From 754cc8ee39e4b1eef1ec9ed8601e9cf31badea7e Mon Sep 17 00:00:00 2001 From: polos Date: Wed, 10 May 2017 16:50:53 +0200 Subject: [PATCH] add macro SYNCALL to WebEngine example (for synchronous funcalls) --- examples/M-modules/webengine/minimal.lisp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/examples/M-modules/webengine/minimal.lisp b/examples/M-modules/webengine/minimal.lisp index 50ff9b4..17cd06f 100644 --- a/examples/M-modules/webengine/minimal.lisp +++ b/examples/M-modules/webengine/minimal.lisp @@ -9,6 +9,7 @@ ;; N.B: The following functions are asynchronous (because WebEngine ;; is all asynchronous). +;; ;; So, we won't have a return value, but need to pass a function. ;; (See 'FunctorOrLambda' in both Qt Assistant and EQL sources.) @@ -21,6 +22,27 @@ (|runJavaScript| (|page| *view*) expression (lambda (result) (qmsg result)))) +;; Introducing macro SYNCALL allows to call asynchronous functions +;; as if they were synchronous (more convenient in simple cases). + +(defmacro syncall (expression) + "Converts asynchronous function calls to synchronous ones, using a local event loop." + (let ((result (gensym)) + (ev-loop (gensym))) + `(let (,result) + (qlet ((,ev-loop "QEventLoop")) + (,@expression (lambda (x) + (setf ,result x) + (|exit| ,ev-loop))) + (|exec| ,ev-loop)) + ,result))) + +(defun to-text* () + (print (syncall (|toPlainText| (|page| *view*))))) + +(defun js* (expression) + (qmsg (syncall (|runJavaScript| (|page| *view*) expression)))) + (x:do-with *view* (|setUrl| (qnew "QUrl(QString)" "http://planet.lisp.org/")) (|resize| '(800 600))