From afefb6d1c4099fbedef39de5fc0cebabe44895fc Mon Sep 17 00:00:00 2001 From: Kostya Date: Mon, 12 Oct 2026 16:36:22 +0300 Subject: ui-fx: run engine calls off the JavaFX thread Resolving, opening and trading called the contexts straight from the JavaFX thread. That was fine while they were in-process, but against a server each one is an HTTP request and would freeze the window while it is in flight. Run them on a background thread and handle the result back on the JavaFX thread. --- .../ui/desktop/controllers/AppController.java | 112 +++++++++++++-------- 1 file changed, 71 insertions(+), 41 deletions(-) (limited to 'ui-fx/src/main') diff --git a/ui-fx/src/main/java/market/guess/ui/desktop/controllers/AppController.java b/ui-fx/src/main/java/market/guess/ui/desktop/controllers/AppController.java index c582051..416770e 100644 --- a/ui-fx/src/main/java/market/guess/ui/desktop/controllers/AppController.java +++ b/ui-fx/src/main/java/market/guess/ui/desktop/controllers/AppController.java @@ -2,8 +2,12 @@ package market.guess.ui.desktop.controllers; import java.math.BigDecimal; import java.nio.file.Paths; +import java.util.concurrent.CompletableFuture; +import java.util.function.Consumer; +import java.util.function.Supplier; import javafx.animation.FadeTransition; import javafx.animation.PauseTransition; +import javafx.application.Platform; import javafx.beans.binding.Bindings; import javafx.collections.ListChangeListener; import javafx.css.PseudoClass; @@ -20,6 +24,7 @@ import javafx.scene.layout.StackPane; import javafx.stage.FileChooser; import javafx.stage.FileChooser.ExtensionFilter; import javafx.util.Duration; +import market.guess.api.Result; import market.guess.model.event.CreateEventRequest; import market.guess.model.event.EventStatus; import market.guess.model.event.MechanismType; @@ -201,10 +206,8 @@ public class AppController { return appView != null ? appView.getState() : null; } - /** * Refreshes UI state across all components. Kept for lifecycle compatibility; delegates to * targeted update methods. - */ public void refresh() { applyInitialState(); } @@ -532,26 +535,39 @@ public class AppController { var state = appView.getState(); var e = state.getSelectedEvent(); if (e == null) return; - var res = appView.getMarketContext().settleEvent(state.getActingUserName(), e.id, option); - if (res.isSuccess()) { - state.refreshData(); - state.setDialog(ModalDialog.NONE); - toast("Event resolved as " + option); - } else { - toast("Failed to resolve event: " + res.getMessage()); - } + var user = state.getActingUserName(); + async( + () -> appView.getMarketContext().settleEvent(user, e.id, option), + res -> { + if (res.isSuccess()) { + state.refreshData(); + state.setDialog(ModalDialog.NONE); + toast("Event resolved as " + option); + } else { + toast("Failed to resolve event: " + res.getMessage()); + } + }); } public void openEvent(EventData e) { - var state = appView.getState(); - if (e.status == EventStatus.DRAFT) { - var res = appView.getCatalogContext().openEvent(e.id); - if (!res.isSuccess()) { - toast("Failed to open event: " + res.getMessage()); - return; - } - state.refreshData(); + if (e.status != EventStatus.DRAFT) { + showTradePanel(e); + return; } + async( + () -> appView.getCatalogContext().openEvent(e.id), + res -> { + if (!res.isSuccess()) { + toast("Failed to open event: " + res.getMessage()); + return; + } + appView.getState().refreshData(); + showTradePanel(e); + }); + } + + private void showTradePanel(EventData e) { + var state = appView.getState(); // The trade panel lives on the Users tab and follows the selected event. state.setSelectedEventId(e.id); state.setActiveTab(AppTab.USERS); @@ -565,28 +581,42 @@ public class AppController { public void placeOrder( String eventKey, String optionKey, String side, BigDecimal price, long quantity) { var state = appView.getState(); - var res = - appView - .getMarketContext() - .placeOrder(state.getActingUserName(), eventKey, optionKey, side, price, quantity); - if (res.isSuccess()) { - state.refreshData(); - var receipt = res.getData(); - boolean isBuy = "BUY".equalsIgnoreCase(side); - if (receipt.trades() == null || receipt.trades().isEmpty()) { - toast("Resting " + side + " order placed: " + quantity + " @ " + Format.money(price)); - } else { - toast( - "Order placed  " - + (isBuy ? "Paid " : "Received ") - + Format.money(new BigDecimal(receipt.totalPaid())) - + " for " - + quantity - + " " - + optionKey); - } - } else { - toast("Trade failed: " + res.getMessage()); - } + var user = state.getActingUserName(); + async( + () -> + appView + .getMarketContext() + .placeOrder(user, eventKey, optionKey, side, price, quantity), + res -> { + if (res.isSuccess()) { + state.refreshData(); + var receipt = res.getData(); + boolean isBuy = "BUY".equalsIgnoreCase(side); + if (receipt.trades() == null || receipt.trades().isEmpty()) { + toast("Resting " + side + " order placed: " + quantity + " @ " + Format.money(price)); + } else { + toast( + "Order placed  " + + (isBuy ? "Paid " : "Received ") + + Format.money(new BigDecimal(receipt.totalPaid())) + + " for " + + quantity + + " " + + optionKey); + } + } else { + toast("Trade failed: " + res.getMessage()); + } + }); + } + + private void async(Supplier> call, Consumer> then) { + CompletableFuture.supplyAsync(call) + .thenAcceptAsync(then, Platform::runLater) + .exceptionally( + ex -> { + Platform.runLater(() -> toast("Something went wrong: " + ex.getMessage())); + return null; + }); } } -- cgit v1.2.3