Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const openBleByQuery = async query => {
const m = query.match(/^ble:?(.*)/);
if (!m) throw new Error("ble regexp should match");
const [, q] = m;
if (cacheBle[query]) return cacheBle[query];
const t = await (!q
? TransportNodeBle.create()
: Observable.create(TransportNodeBle.listen)
.pipe(
first(
e =>
(e.device.name || "").toLowerCase().includes(q.toLowerCase()) ||
e.device.id.toLowerCase() === q.toLowerCase()
),
switchMap(e => TransportNodeBle.open(e.descriptor))
)
.toPromise());
cacheBle[query] = t;
t.on("disconnect", () => {
delete cacheBle[query];
});
return t;
};