fix threading issue on terminal wallet load

This commit is contained in:
Craig Raw 2022-10-11 12:16:18 +02:00
parent 778564a954
commit 81cde4756a

View file

@ -52,7 +52,10 @@ public class LoadWallet implements Runnable {
loadWalletService.setExecutor(Storage.LoadWalletService.getSingleThreadedExecutor()); loadWalletService.setExecutor(Storage.LoadWalletService.getSingleThreadedExecutor());
loadWalletService.setOnSucceeded(workerStateEvent -> { loadWalletService.setOnSucceeded(workerStateEvent -> {
WalletAndKey walletAndKey = loadWalletService.getValue(); WalletAndKey walletAndKey = loadWalletService.getValue();
SparrowTerminal.get().getGuiThread().invokeLater(() -> openWallet(storage, walletAndKey)); SparrowTerminal.get().getGuiThread().invokeLater(() -> {
SparrowTerminal.get().getGui().removeWindow(loadingDialog);
Platform.runLater(() -> openWallet(storage, walletAndKey));
});
}); });
loadWalletService.setOnFailed(workerStateEvent -> { loadWalletService.setOnFailed(workerStateEvent -> {
Throwable exception = workerStateEvent.getSource().getException(); Throwable exception = workerStateEvent.getSource().getException();
@ -77,7 +80,10 @@ public class LoadWallet implements Runnable {
loadWalletService.setOnSucceeded(workerStateEvent -> { loadWalletService.setOnSucceeded(workerStateEvent -> {
EventManager.get().post(new StorageEvent(storage.getWalletId(null), TimedEvent.Action.END, "Done")); EventManager.get().post(new StorageEvent(storage.getWalletId(null), TimedEvent.Action.END, "Done"));
WalletAndKey walletAndKey = loadWalletService.getValue(); WalletAndKey walletAndKey = loadWalletService.getValue();
SparrowTerminal.get().getGuiThread().invokeLater(() -> openWallet(storage, walletAndKey)); SparrowTerminal.get().getGuiThread().invokeLater(() -> {
SparrowTerminal.get().getGui().removeWindow(loadingDialog);
Platform.runLater(() -> openWallet(storage, walletAndKey));
});
}); });
loadWalletService.setOnFailed(workerStateEvent -> { loadWalletService.setOnFailed(workerStateEvent -> {
EventManager.get().post(new StorageEvent(storage.getWalletId(null), TimedEvent.Action.END, "Failed")); EventManager.get().post(new StorageEvent(storage.getWalletId(null), TimedEvent.Action.END, "Failed"));
@ -108,8 +114,6 @@ public class LoadWallet implements Runnable {
} }
private void openWallet(Storage storage, WalletAndKey walletAndKey) { private void openWallet(Storage storage, WalletAndKey walletAndKey) {
SparrowTerminal.get().getGui().removeWindow(loadingDialog);
try { try {
storage.restorePublicKeysFromSeed(walletAndKey.getWallet(), walletAndKey.getKey()); storage.restorePublicKeysFromSeed(walletAndKey.getWallet(), walletAndKey.getKey());
if(!walletAndKey.getWallet().isValid()) { if(!walletAndKey.getWallet().isValid()) {
@ -120,7 +124,7 @@ public class LoadWallet implements Runnable {
openWallet(entry.getValue(), entry.getKey()); openWallet(entry.getValue(), entry.getKey());
} }
if(walletAndKey.getWallet().isMasterWallet()) { if(walletAndKey.getWallet().isMasterWallet()) {
getOpeningDialog(walletAndKey.getWallet()).showDialog(SparrowTerminal.get().getGui()); SparrowTerminal.get().getGuiThread().invokeLater(() -> getOpeningDialog(walletAndKey.getWallet()).showDialog(SparrowTerminal.get().getGui()));
} }
} catch(Exception e) { } catch(Exception e) {
log.error("Wallet Error", e); log.error("Wallet Error", e);
@ -131,31 +135,29 @@ public class LoadWallet implements Runnable {
} }
private void addWallet(Storage storage, Wallet wallet) { private void addWallet(Storage storage, Wallet wallet) {
Platform.runLater(() -> { if(wallet.isNested()) {
if(wallet.isNested()) { WalletData walletData = SparrowTerminal.get().getWalletData().get(wallet.getMasterWallet());
WalletData walletData = SparrowTerminal.get().getWalletData().get(wallet.getMasterWallet()); WalletForm walletForm = new WalletForm(storage, wallet);
WalletForm walletForm = new WalletForm(storage, wallet); EventManager.get().register(walletForm);
EventManager.get().register(walletForm); walletData.getWalletForm().getNestedWalletForms().add(walletForm);
walletData.getWalletForm().getNestedWalletForms().add(walletForm); } else {
} else { EventManager.get().post(new WalletOpeningEvent(storage, wallet));
EventManager.get().post(new WalletOpeningEvent(storage, wallet));
WalletForm walletForm = new WalletForm(storage, wallet); WalletForm walletForm = new WalletForm(storage, wallet);
EventManager.get().register(walletForm); EventManager.get().register(walletForm);
SparrowTerminal.get().getWalletData().put(wallet, new WalletData(walletForm)); SparrowTerminal.get().getWalletData().put(wallet, new WalletData(walletForm));
List<WalletTabData> walletTabDataList = SparrowTerminal.get().getWalletData().values().stream() List<WalletTabData> walletTabDataList = SparrowTerminal.get().getWalletData().values().stream()
.map(data -> new WalletTabData(TabData.TabType.WALLET, data.getWalletForm())).collect(Collectors.toList()); .map(data -> new WalletTabData(TabData.TabType.WALLET, data.getWalletForm())).collect(Collectors.toList());
EventManager.get().post(new OpenWalletsEvent(DEFAULT_WINDOW, walletTabDataList)); EventManager.get().post(new OpenWalletsEvent(DEFAULT_WINDOW, walletTabDataList));
Set<File> walletFiles = new LinkedHashSet<>(); Set<File> walletFiles = new LinkedHashSet<>();
walletFiles.add(storage.getWalletFile()); walletFiles.add(storage.getWalletFile());
walletFiles.addAll(Config.get().getRecentWalletFiles().stream().limit(MAX_RECENT_WALLETS - 1).collect(Collectors.toList())); walletFiles.addAll(Config.get().getRecentWalletFiles().stream().limit(MAX_RECENT_WALLETS - 1).collect(Collectors.toList()));
Config.get().setRecentWalletFiles(Config.get().isLoadRecentWallets() ? new ArrayList<>(walletFiles) : Collections.emptyList()); Config.get().setRecentWalletFiles(Config.get().isLoadRecentWallets() ? new ArrayList<>(walletFiles) : Collections.emptyList());
} }
EventManager.get().post(new WalletOpenedEvent(storage, wallet)); EventManager.get().post(new WalletOpenedEvent(storage, wallet));
});
} }
public static DialogWindow getOpeningDialog(Wallet masterWallet) { public static DialogWindow getOpeningDialog(Wallet masterWallet) {