Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/app/monitor-pings.ts
1496 views
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
// Monitor ping events from webapp_client and use them to set some
7
// ping state in the page store.
8
9
import { redux } from "../app-framework";
10
import { webapp_client } from "../webapp-client";
11
12
export function init_ping(): void {
13
webapp_client.on("ping", (ping_time: number): void => {
14
let ping_time_smooth = redux.getStore("page").get("avgping") ?? ping_time;
15
16
// reset outside 3x
17
if (ping_time > 3 * ping_time_smooth || ping_time_smooth > 3 * ping_time) {
18
ping_time_smooth = ping_time;
19
} else {
20
const decay = 1 - Math.exp(-1);
21
ping_time_smooth = decay * ping_time_smooth + (1 - decay) * ping_time;
22
}
23
redux.getActions("page").set_ping(ping_time, Math.round(ping_time_smooth));
24
});
25
}
26
27