2022-11-02 10:42:02 +01:00
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
|
|
2023-02-06 10:06:33 +01:00
|
|
|
const baseWebsocketUrl = JSON.parse(window.localStorage.getItem('serverBaseURL')!).replace(
|
|
|
|
|
'http',
|
|
|
|
|
'ws',
|
|
|
|
|
);
|
2022-11-02 10:42:02 +01:00
|
|
|
|
|
|
|
|
const useSubscription = <T>(path: string, callback?: (newValue: T) => boolean | void) => {
|
|
|
|
|
const [state, setState] = useState<T | undefined>();
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const wsc = new WebSocket(`${baseWebsocketUrl}${path}`);
|
|
|
|
|
|
|
|
|
|
wsc.onmessage = (e) => {
|
|
|
|
|
const data = JSON.parse(e.data) as T;
|
|
|
|
|
if (callback) {
|
|
|
|
|
// If callback is specified, only update state if callback returns true
|
|
|
|
|
// This is so that useSubscription can be used without causing rerender
|
|
|
|
|
if (callback(data) === true) {
|
|
|
|
|
setState(data);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
setState(data);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return () => wsc.close();
|
|
|
|
|
}, [path]);
|
|
|
|
|
|
|
|
|
|
return { data: state };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default useSubscription;
|