import { action, makeObservable, observable, runInAction } from 'mobx'; import { Networking } from '../Network'; import { CurrentUserUtils } from './CurrentUserUtils'; export class PingManager { // create static instance and getter for global use @observable static _instance: PingManager; static get Instance(): PingManager { return PingManager._instance; } @observable IsBeating = true; private setIsBeating = action((status: boolean) => { this.IsBeating = status; setTimeout(this.showAlert, 100); }); 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 = async (): Promise => { try { const res = await Networking.PostToServer('/ping', { date: new Date() }); runInAction(() => (CurrentUserUtils.ServerVersion = res.message)); !this.IsBeating && this.setIsBeating(true); } catch { if (this.IsBeating) { this.setIsBeating(false); } } }; // not used now, but may need to clear interval private _interval: NodeJS.Timeout | null = null; INTERVAL_SECONDS = 1; constructor() { makeObservable(this); PingManager._instance = this; this._interval = setInterval(this.sendPing, this.INTERVAL_SECONDS * 1000); } }