Skip to content

Commit a50b7c6

Browse files
author
Daisuke Baba
committed
Cancel the corresponding subscription on performing read()
1 parent 46667d8 commit a50b7c6

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

src/generic-ble.js

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -437,28 +437,38 @@ export default function(RED) {
437437
if (readables.length === 0) {
438438
return Promise.resolve();
439439
}
440+
let notifiables = this.characteristics.filter(c => {
441+
if (c.notifiable) {
442+
if (uuids.length === 0) {
443+
return true;
444+
}
445+
return uuids.indexOf(c.uuid) >= 0;
446+
}
447+
});
440448
// perform read here right now
441449
let readObj = {};
442-
return Promise.all(readables.map((r) => {
443-
// {uuid:'characteristic-uuid-to-read'}
444-
return new Promise((resolve, reject) => {
445-
r.object.read(
446-
(err, data) => {
447-
if (err) {
450+
return Promise.all(notifiables.map((n) => n.unsubscribe())).then(() => {
451+
return Promise.all(readables.map((r) => {
452+
// {uuid:'characteristic-uuid-to-read'}
453+
return new Promise((resolve, reject) => {
454+
r.object.read(
455+
(err, data) => {
456+
if (err) {
457+
if (TRACE) {
458+
this.log(`<Read> ${r.uuid} => FAIL`);
459+
}
460+
return reject(err);
461+
}
448462
if (TRACE) {
449-
this.log(`<Read> ${r.uuid} => FAIL`);
463+
this.log(`<Read> ${r.uuid} => ${JSON.stringify(data)}`);
450464
}
451-
return reject(err);
452-
}
453-
if (TRACE) {
454-
this.log(`<Read> ${r.uuid} => ${JSON.stringify(data)}`);
465+
readObj[r.uuid] = data;
466+
resolve();
455467
}
456-
readObj[r.uuid] = data;
457-
resolve();
458-
}
459-
);
460-
});
461-
})).then(() => {
468+
);
469+
});
470+
}));
471+
}).then(() => {
462472
return readObj;
463473
});
464474
});

0 commit comments

Comments
 (0)