ToggleSwitch: Improve reliability

It's not great, but it's better than it was.

Signed-off-by: Samuel Holland <samuel@sholland.org>
This commit is contained in:
Samuel Holland
2018-01-08 04:31:04 -06:00
parent 668d90f063
commit b6ed17884f
2 changed files with 9 additions and 23 deletions
@@ -237,9 +237,10 @@ public final class TunnelManager extends BaseObservable {
CompletionStage<State> setTunnelState(final Tunnel tunnel, final State state) {
final CompletionStage<State> completion =
asyncWorker.supplyAsync(() -> backend.setState(tunnel, state));
completion.thenAccept(tunnel::onStateChanged);
completion.thenAccept(newState -> {
if (newState == State.UP)
completion.whenComplete((newState, e) -> {
// Ensure onStateChanged is always called (failure or not), and with the correct state.
tunnel.onStateChanged(e == null ? newState : tunnel.getState());
if (e == null && newState == State.UP)
setLastUsedTunnel(tunnel);
});
return completion;