mirror of
https://gitlab.nic.cz/turris/reforis/foris-js.git
synced 2024-12-26 00:21:36 +01:00
Add WS unsubscribtion.
Code review improvements.
This commit is contained in:
parent
5bb298270b
commit
f30685d9c2
|
@ -61,9 +61,36 @@ export class WebSockets {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
subscribe(params) {
|
subscribe(module) {
|
||||||
this.waitForConnection(() => {
|
this.waitForConnection(() => {
|
||||||
this.send("subscribe", params);
|
this.send("subscribe", module);
|
||||||
|
});
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
unbind(module, action, callback) {
|
||||||
|
const callbacks = this.callbacks[module][action];
|
||||||
|
|
||||||
|
const index = callbacks.indexOf(callback);
|
||||||
|
if (index !== -1) {
|
||||||
|
callbacks.splice(index, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (callbacks.length === 0) {
|
||||||
|
delete this.callbacks[module][action];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Object.keys(this.callbacks[module]).length === 0) {
|
||||||
|
this.unsubscribe(module);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsubscribe(module) {
|
||||||
|
this.waitForConnection(() => {
|
||||||
|
this.send("unsubscribe", module);
|
||||||
|
delete this.callbacks[module];
|
||||||
});
|
});
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -82,15 +109,15 @@ export class WebSockets {
|
||||||
let chain;
|
let chain;
|
||||||
try {
|
try {
|
||||||
chain = this.callbacks[json.module][json.action];
|
chain = this.callbacks[json.module][json.action];
|
||||||
} catch (e) {
|
} catch (error) {
|
||||||
if (e instanceof TypeError) {
|
if (error instanceof TypeError) {
|
||||||
console.log(`Callback for this message wasn't found:${e.data}`);
|
console.log(`Callback for this message wasn't found:${error.data}`);
|
||||||
} else throw e;
|
} else throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof chain === "undefined") return;
|
if (typeof chain === "undefined") return;
|
||||||
|
|
||||||
for (let i = 0; i < chain.length; i++) chain[i](json);
|
chain.forEach((callback) => callback(json));
|
||||||
}
|
}
|
||||||
|
|
||||||
close() {
|
close() {
|
||||||
|
|
|
@ -11,12 +11,21 @@ export function useWSForisModule(ws, module, action = "update_settings") {
|
||||||
const [data, setData] = useState(null);
|
const [data, setData] = useState(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (ws && module) {
|
// Sometime we want to disable this hook if WS is not passed. We can't make conditional
|
||||||
ws.subscribe(module)
|
// hooks, but we can disable it here. It's used especially in ForisForm when a module
|
||||||
.bind(module, action, (msg) => {
|
// doesn't present any WS endpoint.
|
||||||
|
if (!ws) return;
|
||||||
|
|
||||||
|
function callback(msg) {
|
||||||
setData(msg.data);
|
setData(msg.data);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ws.subscribe(module)
|
||||||
|
.bind(module, action, callback);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
ws.unbind(module, action, callback);
|
||||||
|
};
|
||||||
}, [action, module, ws]);
|
}, [action, module, ws]);
|
||||||
|
|
||||||
return [data];
|
return [data];
|
||||||
|
|
Loading…
Reference in New Issue
Block a user