summaryrefslogtreecommitdiffhomepage
path: root/gui/src/main/tunnel-state.ts
blob: 029386f3b792c42911a560651c5fec7468e80866 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { connectEnabled, disconnectEnabled, reconnectEnabled } from '../shared/connect-helper';
import { TunnelState } from '../shared/daemon-rpc-types';
import { Scheduler } from '../shared/scheduler';

export interface TunnelStateProvider {
  getTunnelState(): TunnelState;
}

export interface TunnelStateHandlerDelegate {
  handleTunnelStateUpdate(tunnelState: TunnelState): void;
}

export default class TunnelStateHandler {
  // The current tunnel state
  private tunnelStateValue: TunnelState = { state: 'disconnected' };
  // When pressing connect/disconnect/reconnect the app assumes what the next state will be before
  // it get's the new state from the daemon. The latest state from the daemon is saved as fallback
  // if the assumed state isn't reached.
  private tunnelStateFallback?: TunnelState;
  // Scheduler for discarding the assumed next state.
  private tunnelStateFallbackScheduler = new Scheduler();

  public constructor(private delegate: TunnelStateHandlerDelegate) {}

  public get tunnelState() {
    return this.tunnelStateValue;
  }

  public resetFallback() {
    this.tunnelStateFallbackScheduler.cancel();
    this.tunnelStateFallback = undefined;
  }

  // This function sets a new tunnel state as an assumed next state and saves the current state as
  // fallback. The fallback is used if the assumed next state isn't reached.
  public expectNextTunnelState(state: 'connecting' | 'disconnecting') {
    this.tunnelStateFallback = this.tunnelState;

    this.setTunnelState(
      state === 'disconnecting' ? { state, details: 'nothing' as const } : { state },
    );

    this.tunnelStateFallbackScheduler.schedule(() => {
      if (this.tunnelStateFallback) {
        this.setTunnelState(this.tunnelStateFallback);
        this.tunnelStateFallback = undefined;
      }
    }, 3000);
  }

  public handleNewTunnelState(newState: TunnelState) {
    // If there's a fallback state set then the app is in an assumed next state and need to check
    // if it's now reached or if the current state should be ignored and set as the fallback state.
    if (this.tunnelStateFallback) {
      if (this.tunnelState.state === newState.state || newState.state === 'error') {
        this.tunnelStateFallbackScheduler.cancel();
        this.tunnelStateFallback = undefined;
      } else {
        this.tunnelStateFallback = newState;
        return;
      }
    }

    if (newState.state === 'disconnecting' && newState.details === 'reconnect') {
      // When reconnecting there's no need of showing the disconnecting state. This switches to the
      // connecting state immediately.
      this.expectNextTunnelState('connecting');
      this.tunnelStateFallback = newState;
    } else {
      this.setTunnelState(newState);
    }
  }

  public allowConnect(connectToDaemon: boolean, isLoggedIn: boolean) {
    return connectEnabled(connectToDaemon, isLoggedIn, this.tunnelState.state);
  }

  public allowReconnect(connectToDaemon: boolean, isLoggedIn: boolean) {
    return reconnectEnabled(connectToDaemon, isLoggedIn, this.tunnelState.state);
  }

  public allowDisconnect(connectToDaemon: boolean) {
    return disconnectEnabled(connectToDaemon, this.tunnelState.state);
  }

  private setTunnelState(newState: TunnelState) {
    this.tunnelStateValue = newState;
    this.delegate.handleTunnelStateUpdate(newState);
  }
}