2017-09-28 12:39:56 -06:00
|
|
|
import { store } from "../redux/store";
|
|
|
|
import { networkUp, networkDown } from "./actions";
|
2017-09-29 13:46:08 -06:00
|
|
|
import { Edge } from "./interfaces";
|
2019-09-05 15:44:12 -06:00
|
|
|
import { Actions } from "../constants";
|
2017-09-28 12:39:56 -06:00
|
|
|
|
|
|
|
/* ABOUT THIS FILE: These functions allow us to mark the network as up or
|
2017-09-28 14:51:03 -06:00
|
|
|
down from anywhere within the app (even outside of React-Redux). I usually avoid
|
|
|
|
directly importing `store`, but in this particular instance it might be
|
|
|
|
unavoidable. */
|
2017-09-28 12:39:56 -06:00
|
|
|
|
2017-12-27 08:51:52 -07:00
|
|
|
/** throttle calls to these functions to avoid unnecessary re-paints. */
|
|
|
|
const SLOWDOWN_TIME = 1500;
|
|
|
|
|
|
|
|
const lastCalledAt: Record<Edge, number> = {
|
|
|
|
"user.api": 0, "user.mqtt": 0, "bot.mqtt": 0
|
|
|
|
};
|
|
|
|
|
|
|
|
function shouldThrottle(edge: Edge, now: number): boolean {
|
|
|
|
const then = lastCalledAt[edge];
|
2019-01-07 22:21:48 -07:00
|
|
|
const diff = now - then;
|
|
|
|
return diff < SLOWDOWN_TIME;
|
2017-12-27 08:51:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function bumpThrottle(edge: Edge, now: number) {
|
|
|
|
lastCalledAt[edge] = now;
|
|
|
|
}
|
|
|
|
|
2019-09-05 15:44:12 -06:00
|
|
|
export const dispatchQosStart = (id: string) => {
|
|
|
|
store.dispatch({
|
|
|
|
type: Actions.START_QOS_PING,
|
|
|
|
payload: { id }
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-09-05 16:27:42 -06:00
|
|
|
export let dispatchNetworkUp = (edge: Edge, at: number, qosPingId?: string) => {
|
2019-09-05 15:44:12 -06:00
|
|
|
console.log("TODO: Insert ID HERE");
|
2019-09-05 09:16:25 -06:00
|
|
|
if (shouldThrottle(edge, at)) { return; }
|
2019-09-05 16:27:42 -06:00
|
|
|
store.dispatch(networkUp(edge, at, qosPingId));
|
2019-09-05 09:16:25 -06:00
|
|
|
bumpThrottle(edge, at);
|
2017-12-27 08:51:52 -07:00
|
|
|
};
|
2017-09-28 12:39:56 -06:00
|
|
|
|
2019-09-05 16:27:42 -06:00
|
|
|
export let dispatchNetworkDown = (edge: Edge, at: number, qosPingId?: string) => {
|
2019-09-05 15:44:12 -06:00
|
|
|
console.log("TODO: Insert ID HERE");
|
2019-09-05 09:16:25 -06:00
|
|
|
if (shouldThrottle(edge, at)) { return; }
|
2019-09-05 16:27:42 -06:00
|
|
|
store.dispatch(networkDown(edge, at, qosPingId));
|
2019-09-05 09:16:25 -06:00
|
|
|
bumpThrottle(edge, at);
|
2017-12-27 08:51:52 -07:00
|
|
|
};
|