optimized filtering of found blinksticks

This commit is contained in:
Daniel Sommer 2022-02-25 15:50:32 +01:00
parent 4aa965fece
commit b3b123bea8

View file

@ -18,6 +18,14 @@ let blinksticks;
async function findBlinkstick(index, ignoreFilter) { async function findBlinkstick(index, ignoreFilter) {
if (!global.config.blinkstick?.cache || blinksticks === undefined) { if (!global.config.blinkstick?.cache || blinksticks === undefined) {
blinksticks = blinkstick.findAll(); blinksticks = blinkstick.findAll();
if (global.config.blinkstick?.serials?.length > 0) {
blinksticks = blinksticks.filter((blinkstick) => {
return global.config.blinkstick.serials.includes(blinkstick.serial);
});
if (blinksticks.length === 0) {
throw new Error('could not find any blinkstick matching the defined serial(s)');
}
}
} }
if (blinksticks.length === 0) { if (blinksticks.length === 0) {
throw new Error('could not find any blinkstick, make sure at least one blinkstick is connected'); throw new Error('could not find any blinkstick, make sure at least one blinkstick is connected');
@ -30,15 +38,6 @@ async function findBlinkstick(index, ignoreFilter) {
if (isNaN(index)) { if (isNaN(index)) {
index = 0; index = 0;
} }
if (global.config.blinkstick?.serials?.length > 0) {
blinksticks = blinksticks.filter((blinkstick) => {
return global.config.blinkstick.serials.includes(blinkstick.serial);
});
if (blinksticks.length === 0) {
throw new Error('could not find any blinkstick matching the defined serial(s)');
}
}
if (index > blinksticks.length - 1) { if (index > blinksticks.length - 1) {
throw new Error('there is no blinkstick for index \'' + index + '\''); throw new Error('there is no blinkstick for index \'' + index + '\'');
} }