Accept invalid IDs in useSlave and useInstance

Gracefully handle invalid IDs in useSlave and useInstance as this is
necessary when when the ID isn't known by the component using the hook,
since it can't conditionally call the hook.
This commit is contained in:
Hornwitser committed 2021-06-10 10:26:40 +02:00
1 parent 82c3d27bff
commit 9b34b949a6
2 files changed
+9 -11

No files matched your search

+5 -6
View File
@@ -10,21 +10,20 @@ export function useInstance(id) {
let [instance, setInstance] = useState({ loading: true });
function updateInstance() {
if (!Number.isInteger(id)) {
setInstance({ missing: true });
return;
}
libLink.messages.getInstance.send(control, { id }).then(result => {
setInstance({ ...result, present: true });
}).catch(err => {
logger.log(`Failed to get instance: ${err}`);
logger.error(`Failed to get instance: ${err}`);
setInstance({ missing: true });
});
}
useEffect(() => {
if (!Number.isInteger(id)) {
setInstance({ missing: true });
return undefined;
}
updateInstance();
function updateHandler(newInstance) {
+4 -5
View File
@@ -9,11 +9,6 @@ export function useSlave(id) {
let [slave, setSlave] = useState({ loading: true });
function updateSlave() {
if (!Number.isInteger(id)) {
setSlave({ missing: true });
return;
}
// XXX optimize by requesting only the slave in question
libLink.messages.listSlaves.send(control).then(result => {
let match = result.list.find(i => i.id === id);
@@ -26,6 +21,10 @@ export function useSlave(id) {
}
useEffect(() => {
if (!Number.isInteger(id)) {
setSlave({ missing: true });
return undefined;
}
updateSlave();
function updateHandler(newSlave) {