Improve offline/online detection

master
Garrett Mills 4 years ago
parent 0107716183
commit ab990caca4
Signed by: garrettmills
GPG Key ID: D2BF5FBA8298F246

@ -45,27 +45,103 @@ export class ApiService {
protected db: DatabaseService, protected db: DatabaseService,
protected connection: ConnectionService, protected connection: ConnectionService,
) { ) {
connection.monitor().subscribe(isConnected => { this.startOfflineObserver();
if ( !isConnected ) { }
this.makeOffline();
} else { protected stopOfflineObserverClosure?: any;
this.checkOnline().then(isOnline => {
if ( isOnline ) { public stopOfflineObserver() {
this.makeOnline(); if ( this.stopOfflineObserverClosure ) {
} else { this.stopOfflineObserverClosure();
this.makeOffline(); }
}
protected startOfflineObserver() {
const passiveCheckTime = 5;
const checkTimes = [5, 5, 10, 10, 15, 15, 20, 20, 30, 30, 30, 60, 60, 500];
let currentCheckTimeIndex = 0;
let hasNetConnection = true;
let hasServerConnection = true;
let online = true;
let passiveCheckInterval;
let activeCheckInterval;
let stopped = false;
this.stopOfflineObserverClosure = () => {
stopped = true;
clearInterval(passiveCheckInterval);
clearInterval(activeCheckInterval);
};
const checkServerConnection = async () => {
if ( !hasNetConnection ) {
return false;
}
return this.checkOnline();
};
const startPassiveCheck = () => {
passiveCheckInterval = setInterval(async () => {
if ( hasNetConnection ) {
const server = await checkServerConnection();
if ( server !== hasServerConnection ) {
hasServerConnection = server;
await handleNetConnectionEvent();
} }
}); }
}, passiveCheckTime * 1000);
};
const doActiveCheck = async () => {
if ( activeCheckInterval ) {
clearInterval(activeCheckInterval);
if ( currentCheckTimeIndex < (checkTimes.length - 1) ) {
currentCheckTimeIndex += 1;
}
} }
});
this.checkOnline().then(isConnected => { if ( hasNetConnection ) {
if ( !isConnected ) { const server = await checkServerConnection();
this.makeOffline(); if ( server !== hasServerConnection ) {
} else { hasServerConnection = server;
this.makeOnline(); await handleNetConnectionEvent();
} else {
if ( activeCheckInterval ) {
activeCheckInterval = setInterval(doActiveCheck, checkTimes[currentCheckTimeIndex] * 1000);
}
}
}
};
const handleNetConnectionEvent = async () => {
if ( stopped ) {
return;
} }
if ( online && (!hasNetConnection || !hasServerConnection) ) {
online = false;
await this.makeOffline();
clearInterval(passiveCheckInterval);
activeCheckInterval = setInterval(doActiveCheck, checkTimes[currentCheckTimeIndex] * 1000);
} else if ( !online && (hasNetConnection && hasServerConnection) ) {
if ( activeCheckInterval ) {
clearInterval(activeCheckInterval);
}
online = true;
currentCheckTimeIndex = 0;
await this.makeOnline();
startPassiveCheck();
}
};
this.connection.monitor().subscribe(isConnected => {
hasNetConnection = isConnected;
handleNetConnectionEvent();
}); });
startPassiveCheck();
} }
public checkOnline(): Promise<boolean> { public checkOnline(): Promise<boolean> {

Loading…
Cancel
Save