blob: 593978b4afaef51b3c81d7c09ddf465111e6e89d (
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
|
import { action, makeObservable, observable } from 'mobx';
import { Networking } from '../Network';
import { SnappingManager } from './SnappingManager';
export class PingManager {
PING_INTERVAL_SECONDS = 1;
// not used now, but may need to clear interval
private _interval: NodeJS.Timeout | null = null;
// create static instance and getter for global use
// eslint-disable-next-line no-use-before-define
@observable private static _instance: PingManager;
@observable IsBeating = true;
static get Instance(): PingManager {
return PingManager._instance;
}
constructor() {
makeObservable(this);
PingManager._instance = this;
this._interval = setInterval(this.sendPing, this.PING_INTERVAL_SECONDS * 1000);
}
showAlert = () => alert(PingManager.Instance.IsBeating ? 'The server connection is active' : 'The server connection has been interrupted.NOTE: Any changes made will appear to persist but will be lost after a browser refreshes.');
sendPing = () => {
const setIsBeating = action((status: boolean) => {
this.IsBeating = status;
setTimeout(this.showAlert, 100);
});
Networking.PostToServer('/DashPing', { date: new Date() })
.then(res => {
SnappingManager.SetServerVersion((res as { message: string }).message);
!this.IsBeating && setIsBeating(true);
})
.catch(() => this.IsBeating && setIsBeating(false));
};
}
|