1
0
mirror of https://gitlab.nic.cz/turris/reforis/foris-js.git synced 2025-08-05 20:23:46 +02:00

Add WS unsubscribtion.

Code review improvements.
This commit is contained in:
Bogdan Bodnar
2019-12-04 17:05:56 +01:00
parent 5bb298270b
commit f30685d9c2
2 changed files with 48 additions and 12 deletions

View File

@@ -11,12 +11,21 @@ export function useWSForisModule(ws, module, action = "update_settings") {
const [data, setData] = useState(null);
useEffect(() => {
if (ws && module) {
ws.subscribe(module)
.bind(module, action, (msg) => {
setData(msg.data);
});
// Sometime we want to disable this hook if WS is not passed. We can't make conditional
// hooks, but we can disable it here. It's used especially in ForisForm when a module
// doesn't present any WS endpoint.
if (!ws) return;
function callback(msg) {
setData(msg.data);
}
ws.subscribe(module)
.bind(module, action, callback);
return () => {
ws.unbind(module, action, callback);
};
}, [action, module, ws]);
return [data];