aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/PingManager.ts
diff options
context:
space:
mode:
authorNathan-SR <144961007+Nathan-SR@users.noreply.github.com>2025-03-04 04:32:50 -0500
committerNathan-SR <144961007+Nathan-SR@users.noreply.github.com>2025-03-04 04:32:50 -0500
commit95abdada5a275fc258fa72781f7f3c40c0b306ea (patch)
tree6d729cebe0937ae81108005de9895b5398d1f475 /src/client/util/PingManager.ts
parent0a8f3739cf5c30852f18751a4c05d81e0dabe928 (diff)
parent215ad40efa2e343e290d18bffbc55884829f1a0d (diff)
Merge branch 'master' of https://github.com/brown-dash/Dash-Web into Merge
Diffstat (limited to 'src/client/util/PingManager.ts')
-rw-r--r--src/client/util/PingManager.ts43
1 files changed, 18 insertions, 25 deletions
diff --git a/src/client/util/PingManager.ts b/src/client/util/PingManager.ts
index 255e9cee0..0e4f8cab0 100644
--- a/src/client/util/PingManager.ts
+++ b/src/client/util/PingManager.ts
@@ -1,44 +1,37 @@
-import { action, makeObservable, observable, runInAction } from 'mobx';
+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 static _instance: PingManager;
+ @observable private static _instance: PingManager;
@observable IsBeating = true;
static get Instance(): PingManager {
return PingManager._instance;
}
- // 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);
+ this._interval = setInterval(this.sendPing, this.PING_INTERVAL_SECONDS * 1000);
}
- 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.');
- 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<void> => {
- try {
- const res = await Networking.PostToServer('/ping', { date: new Date() });
- runInAction(() => {
- SnappingManager.SetServerVersion(res.message);
- });
- !this.IsBeating && this.setIsBeating(true);
- } catch {
- if (this.IsBeating) {
- this.setIsBeating(false);
- }
- }
+ sendPing = () => {
+ const setIsBeating = action((status: boolean) => {
+ this.IsBeating = status;
+ setTimeout(this.showAlert, 100);
+ });
+ Networking.PostToServer('/ping', { date: new Date() })
+ .then(res => {
+ SnappingManager.SetServerVersion((res as { message: string }).message);
+ !this.IsBeating && setIsBeating(true);
+ })
+ .catch(() => this.IsBeating && setIsBeating(false));
};
}