mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2025-09-15 23:06:07 -04:00
Fix #548
This commit is contained in:
parent
5e659352d7
commit
adc360eaaa
@ -34,6 +34,8 @@ import org.jackhuang.hmcl.auth.NoCharacterException;
|
||||
import org.jackhuang.hmcl.auth.ServerResponseMalformedException;
|
||||
import org.jackhuang.hmcl.util.gson.UUIDTypeAdapter;
|
||||
|
||||
import javafx.beans.binding.ObjectBinding;
|
||||
|
||||
public class YggdrasilAccount extends Account {
|
||||
|
||||
private final YggdrasilService service;
|
||||
@ -48,6 +50,8 @@ public class YggdrasilAccount extends Account {
|
||||
this.username = requireNonNull(username);
|
||||
this.characterUUID = requireNonNull(session.getSelectedProfile().getId());
|
||||
this.session = requireNonNull(session);
|
||||
|
||||
addProfilePropertiesListener();
|
||||
}
|
||||
|
||||
protected YggdrasilAccount(YggdrasilService service, String username, String password, CharacterSelector selector) throws AuthenticationException {
|
||||
@ -73,6 +77,17 @@ public class YggdrasilAccount extends Account {
|
||||
|
||||
characterUUID = session.getSelectedProfile().getId();
|
||||
authenticated = true;
|
||||
|
||||
addProfilePropertiesListener();
|
||||
}
|
||||
|
||||
private ObjectBinding<Optional<CompleteGameProfile>> profilePropertiesBinding;
|
||||
private void addProfilePropertiesListener() {
|
||||
// binding() is thread-safe
|
||||
// hold the binding so that it won't be garbage-collected
|
||||
profilePropertiesBinding = service.getProfileRepository().binding(characterUUID, true);
|
||||
// and it's safe to add a listener to an ObjectBinding which does not have any listener attached before (maybe tricky)
|
||||
profilePropertiesBinding.addListener((a, b, c) -> this.invalidate());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -132,6 +132,14 @@ public class ObservableCache<K, V, E extends Exception> {
|
||||
}
|
||||
|
||||
public ObjectBinding<V> binding(K key) {
|
||||
return binding(key, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param quiet if true, calling get() on the returned binding won't toggle a query
|
||||
*/
|
||||
public ObjectBinding<V> binding(K key, boolean quiet) {
|
||||
// This method is thread-safe because ObservableHelper supports concurrent modification
|
||||
return Bindings.createObjectBinding(() -> {
|
||||
V result;
|
||||
boolean refresh;
|
||||
@ -144,7 +152,7 @@ public class ObservableCache<K, V, E extends Exception> {
|
||||
refresh = invalidated.containsKey(key);
|
||||
}
|
||||
}
|
||||
if (refresh) {
|
||||
if (!quiet && refresh) {
|
||||
query(key, executor);
|
||||
}
|
||||
return result;
|
||||
|
@ -56,6 +56,10 @@ public class ObservableOptionalCache<K, V, E extends Exception> {
|
||||
return backed.binding(key);
|
||||
}
|
||||
|
||||
public ObjectBinding<Optional<V>> binding(K key, boolean quiet) {
|
||||
return backed.binding(key, quiet);
|
||||
}
|
||||
|
||||
public void invalidate(K key) {
|
||||
backed.invalidate(key);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user