mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-21 06:59:09 +00:00
refactor: maintain an isOnline flag in client
This commit is contained in:
committed by
Carlos Valente
parent
c1d53b0e55
commit
fb83f48752
@@ -16,11 +16,17 @@ export const useRuntimeStore = <T>(selector: (state: RuntimeStore) => T) =>
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Allows patching a property of the runtime store
|
* Allows patching a property of the runtime store
|
||||||
* @param key
|
|
||||||
* @param value
|
|
||||||
*/
|
*/
|
||||||
export function patchRuntime<K extends keyof RuntimeStore>(key: K, value: RuntimeStore[K]): void {
|
export function patchRuntimeProperty<K extends keyof RuntimeStore>(key: K, value: RuntimeStore[K]) {
|
||||||
const state = runtimeStore.getState();
|
const state = runtimeStore.getState();
|
||||||
state[key] = value;
|
state[key] = value;
|
||||||
runtimeStore.setState({ ...state });
|
runtimeStore.setState({ ...state });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allows patching the entire runtime store
|
||||||
|
*/
|
||||||
|
export function patchRuntime(patch: Partial<RuntimeStore>) {
|
||||||
|
const state = runtimeStore.getState();
|
||||||
|
runtimeStore.setState({ ...state, ...patch });
|
||||||
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import {
|
|||||||
} from '../stores/clientStore';
|
} from '../stores/clientStore';
|
||||||
import { addDialog } from '../stores/dialogStore';
|
import { addDialog } from '../stores/dialogStore';
|
||||||
import { addLog } from '../stores/logger';
|
import { addLog } from '../stores/logger';
|
||||||
import { patchRuntime, runtimeStore } from '../stores/runtime';
|
import { patchRuntime, patchRuntimeProperty } from '../stores/runtime';
|
||||||
|
|
||||||
export let websocket: WebSocket | null = null;
|
export let websocket: WebSocket | null = null;
|
||||||
let reconnectTimeout: NodeJS.Timeout | null = null;
|
let reconnectTimeout: NodeJS.Timeout | null = null;
|
||||||
@@ -39,12 +39,14 @@ export const connectSocket = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
socketSendJson('set-client-type', 'ontime');
|
socketSendJson('set-client-type', 'ontime');
|
||||||
|
|
||||||
socketSendJson('set-client-path', location.pathname + location.search);
|
socketSendJson('set-client-path', location.pathname + location.search);
|
||||||
|
setOnlineStatus(true);
|
||||||
};
|
};
|
||||||
|
|
||||||
websocket.onclose = () => {
|
websocket.onclose = () => {
|
||||||
console.warn('WebSocket disconnected');
|
console.warn('WebSocket disconnected');
|
||||||
|
setOnlineStatus(false);
|
||||||
|
|
||||||
if (shouldReconnect) {
|
if (shouldReconnect) {
|
||||||
reconnectTimeout = setTimeout(() => {
|
reconnectTimeout = setTimeout(() => {
|
||||||
console.warn('WebSocket: attempting reconnect');
|
console.warn('WebSocket: attempting reconnect');
|
||||||
@@ -73,8 +75,8 @@ export const connectSocket = () => {
|
|||||||
switch (type) {
|
switch (type) {
|
||||||
case 'pong': {
|
case 'pong': {
|
||||||
const offset = (new Date().getTime() - new Date(payload).getTime()) * 0.5;
|
const offset = (new Date().getTime() - new Date(payload).getTime()) * 0.5;
|
||||||
patchRuntime('ping', offset);
|
patchRuntimeProperty('ping', offset);
|
||||||
updateDevTools({ ping: offset }, ['PING']);
|
updateDevTools({ ping: offset });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'client-id': {
|
case 'client-id': {
|
||||||
@@ -131,64 +133,65 @@ export const connectSocket = () => {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'ontime': {
|
case 'ontime': {
|
||||||
runtimeStore.setState(payload as RuntimeStore);
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- removing the key from the payload
|
||||||
if (!isProduction) {
|
const { ping, ...serverPayload } = payload as Partial<RuntimeStore>;
|
||||||
ontimeQueryClient.setQueryData(RUNTIME, data.payload);
|
|
||||||
}
|
patchRuntime(serverPayload);
|
||||||
|
updateDevTools(serverPayload);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'ontime-clock': {
|
case 'ontime-clock': {
|
||||||
patchRuntime('clock', payload);
|
patchRuntimeProperty('clock', payload);
|
||||||
updateDevTools({ clock: payload });
|
updateDevTools({ clock: payload });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'ontime-timer': {
|
case 'ontime-timer': {
|
||||||
patchRuntime('timer', payload);
|
patchRuntimeProperty('timer', payload);
|
||||||
updateDevTools({ timer: payload });
|
updateDevTools({ timer: payload });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'ontime-onAir': {
|
case 'ontime-onAir': {
|
||||||
patchRuntime('onAir', payload);
|
patchRuntimeProperty('onAir', payload);
|
||||||
updateDevTools({ onAir: payload });
|
updateDevTools({ onAir: payload });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'ontime-message': {
|
case 'ontime-message': {
|
||||||
patchRuntime('message', payload);
|
patchRuntimeProperty('message', payload);
|
||||||
updateDevTools({ message: payload });
|
updateDevTools({ message: payload });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'ontime-runtime': {
|
case 'ontime-runtime': {
|
||||||
patchRuntime('runtime', payload);
|
patchRuntimeProperty('runtime', payload);
|
||||||
updateDevTools({ runtime: payload });
|
updateDevTools({ runtime: payload });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'ontime-eventNow': {
|
case 'ontime-eventNow': {
|
||||||
patchRuntime('eventNow', payload);
|
patchRuntimeProperty('eventNow', payload);
|
||||||
updateDevTools({ eventNow: payload });
|
updateDevTools({ eventNow: payload });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'ontime-currentBlock': {
|
case 'ontime-currentBlock': {
|
||||||
patchRuntime('currentBlock', payload);
|
patchRuntimeProperty('currentBlock', payload);
|
||||||
updateDevTools({ currentBlock: payload });
|
updateDevTools({ currentBlock: payload });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'ontime-publicEventNow': {
|
case 'ontime-publicEventNow': {
|
||||||
patchRuntime('publicEventNow', payload);
|
patchRuntimeProperty('publicEventNow', payload);
|
||||||
updateDevTools({ publicEventNow: payload });
|
updateDevTools({ publicEventNow: payload });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'ontime-eventNext': {
|
case 'ontime-eventNext': {
|
||||||
patchRuntime('eventNext', payload);
|
patchRuntimeProperty('eventNext', payload);
|
||||||
updateDevTools({ eventNext: payload });
|
updateDevTools({ eventNext: payload });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'ontime-publicEventNext': {
|
case 'ontime-publicEventNext': {
|
||||||
patchRuntime('publicEventNext', payload);
|
patchRuntimeProperty('publicEventNext', payload);
|
||||||
updateDevTools({ publicEventNext: payload });
|
updateDevTools({ publicEventNext: payload });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'ontime-auxtimer1': {
|
case 'ontime-auxtimer1': {
|
||||||
patchRuntime('auxtimer1', payload);
|
patchRuntimeProperty('auxtimer1', payload);
|
||||||
updateDevTools({ auxtimer1: payload });
|
updateDevTools({ auxtimer1: payload });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -232,11 +235,23 @@ export const socketSendJson = (type: string, payload?: unknown) => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
function updateDevTools(newData: Partial<RuntimeStore>, store = RUNTIME) {
|
function updateDevTools(newData: Partial<RuntimeStore>) {
|
||||||
if (!isProduction) {
|
if (!isProduction) {
|
||||||
ontimeQueryClient.setQueryData(store, (oldData: RuntimeStore) => ({
|
ontimeQueryClient.setQueryData(RUNTIME, (oldData: RuntimeStore) => ({
|
||||||
...oldData,
|
...oldData,
|
||||||
...newData,
|
...newData,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allows setting the status of the client
|
||||||
|
* We leverage the ping as an indication of the client's online status
|
||||||
|
* @example ping < 0 - client is offline
|
||||||
|
* @example ping > 0 -> client is online
|
||||||
|
*/
|
||||||
|
function setOnlineStatus(status: boolean) {
|
||||||
|
const derivedPing = status ? 1 : -1;
|
||||||
|
patchRuntimeProperty('ping', derivedPing);
|
||||||
|
updateDevTools({ ping: derivedPing });
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user