This commit is contained in:
yushijinhun 2019-05-02 00:12:33 +08:00
parent 5e659352d7
commit adc360eaaa
No known key found for this signature in database
GPG Key ID: 5BC167F73EA558E4
3 changed files with 28 additions and 1 deletions

View File

@ -34,6 +34,8 @@ import org.jackhuang.hmcl.auth.NoCharacterException;
import org.jackhuang.hmcl.auth.ServerResponseMalformedException; import org.jackhuang.hmcl.auth.ServerResponseMalformedException;
import org.jackhuang.hmcl.util.gson.UUIDTypeAdapter; import org.jackhuang.hmcl.util.gson.UUIDTypeAdapter;
import javafx.beans.binding.ObjectBinding;
public class YggdrasilAccount extends Account { public class YggdrasilAccount extends Account {
private final YggdrasilService service; private final YggdrasilService service;
@ -48,6 +50,8 @@ public class YggdrasilAccount extends Account {
this.username = requireNonNull(username); this.username = requireNonNull(username);
this.characterUUID = requireNonNull(session.getSelectedProfile().getId()); this.characterUUID = requireNonNull(session.getSelectedProfile().getId());
this.session = requireNonNull(session); this.session = requireNonNull(session);
addProfilePropertiesListener();
} }
protected YggdrasilAccount(YggdrasilService service, String username, String password, CharacterSelector selector) throws AuthenticationException { 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(); characterUUID = session.getSelectedProfile().getId();
authenticated = true; 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 @Override

View File

@ -132,6 +132,14 @@ public class ObservableCache<K, V, E extends Exception> {
} }
public ObjectBinding<V> binding(K key) { 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(() -> { return Bindings.createObjectBinding(() -> {
V result; V result;
boolean refresh; boolean refresh;
@ -144,7 +152,7 @@ public class ObservableCache<K, V, E extends Exception> {
refresh = invalidated.containsKey(key); refresh = invalidated.containsKey(key);
} }
} }
if (refresh) { if (!quiet && refresh) {
query(key, executor); query(key, executor);
} }
return result; return result;

View File

@ -56,6 +56,10 @@ public class ObservableOptionalCache<K, V, E extends Exception> {
return backed.binding(key); return backed.binding(key);
} }
public ObjectBinding<Optional<V>> binding(K key, boolean quiet) {
return backed.binding(key, quiet);
}
public void invalidate(K key) { public void invalidate(K key) {
backed.invalidate(key); backed.invalidate(key);
} }