From b3b123bea808901efeee3b6a0da2fc37b44f7a6f Mon Sep 17 00:00:00 2001 From: velvettear Date: Fri, 25 Feb 2022 15:50:32 +0100 Subject: [PATCH] optimized filtering of found blinksticks --- libs/blinkstick.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/libs/blinkstick.js b/libs/blinkstick.js index e4c6de0..4cfdf16 100644 --- a/libs/blinkstick.js +++ b/libs/blinkstick.js @@ -18,6 +18,14 @@ let blinksticks; async function findBlinkstick(index, ignoreFilter) { if (!global.config.blinkstick?.cache || blinksticks === undefined) { 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) { 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)) { 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) { throw new Error('there is no blinkstick for index \'' + index + '\''); }