Make Browser JavaScript part more robust when the widget is disposed
authorMathieu Baudier <mbaudier@argeo.org>
Thu, 2 Nov 2023 14:32:34 +0000 (15:32 +0100)
committerMathieu Baudier <mbaudier@argeo.org>
Thu, 2 Nov 2023 14:32:34 +0000 (15:32 +0100)
swt/org.argeo.app.swt/src/org/argeo/app/swt/js/SwtBrowserJsPart.java

index 6f680e28816ed3217e583b97f091567bc53d2983..6782f5dd22a03c6d371d914340cd226805b371f8 100644 (file)
@@ -114,10 +114,8 @@ public class SwtBrowserJsPart implements JsClient {
                assert browser.getDisplay().equals(Display.findDisplay(Thread.currentThread())) : "Not the proper UI thread.";
                if (!readyStage.isDone())
                        throw new IllegalStateException("Methods returning a result can only be called after UI initialisation.");
-               // wait for the context to be ready
-//             boolean ready = readyStage.join();
-//             if (!ready)
-//                     throw new IllegalStateException("Component is not initialised.");
+               if (browser.isDisposed())
+                       return null;
                Object result = browser.evaluate(String.format(Locale.ROOT, js, args));
                return result;
        }
@@ -126,6 +124,8 @@ public class SwtBrowserJsPart implements JsClient {
        public void execute(String js, Object... args) {
                String jsToExecute = String.format(Locale.ROOT, js, args);
                if (readyStage.isDone()) {
+                       if (browser.isDisposed())
+                               return;
                        boolean success = browser.execute(jsToExecute);
                        if (!success)
                                throw new RuntimeException("JavaScript execution failed.");
@@ -156,6 +156,8 @@ public class SwtBrowserJsPart implements JsClient {
         * instead.
         */
        protected void doExecute(String js, Object... args) {
+               if (browser.isDisposed())
+                       return;
                browser.execute(String.format(Locale.ROOT, js, args));
        }
 
@@ -173,6 +175,8 @@ public class SwtBrowserJsPart implements JsClient {
 
                @Override
                public void run() {
+                       if (browser.isDisposed())
+                               return;
                        boolean success = browser.execute(js);
                        if (!success && log.isTraceEnabled())
                                log.error("Pre-ready JavaScript failed: " + js);