-
Notifications
You must be signed in to change notification settings - Fork 392
/
Copy pathtunnel-state.ts
116 lines (96 loc) · 4.14 KB
/
tunnel-state.ts
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { connectEnabled, disconnectEnabled, reconnectEnabled } from '../shared/connect-helper';
import { ErrorStateCause, ILocation, 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();
private receivedFullDiskAccessError = false;
private lastKnownDisconnectedLocation: Partial<ILocation> | undefined;
public constructor(private delegate: TunnelStateHandlerDelegate) {}
public get hasReceivedFullDiskAccessError() {
return this.receivedFullDiskAccessError;
}
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, location: this.lastKnownDisconnectedLocation }
: { state },
);
this.tunnelStateFallbackScheduler.schedule(() => {
if (this.tunnelStateFallback) {
this.setTunnelState(this.tunnelStateFallback);
this.tunnelStateFallback = undefined;
}
}, 3000);
}
public handleNewTunnelState(newState: TunnelState) {
if (newState.state === 'error' && newState.details) {
if (newState.details.cause === ErrorStateCause.needFullDiskPermissions) {
this.receivedFullDiskAccessError = true;
}
}
// 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 {
if (newState.state === 'disconnected' && newState.location !== undefined) {
this.lastKnownDisconnectedLocation = newState.location;
}
if (
newState.state === 'disconnecting' ||
(newState.state === 'disconnected' && newState.location === undefined)
) {
newState.location = this.lastKnownDisconnectedLocation;
}
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);
}
}