Skip to content

Commit 46667d8

Browse files
author
Daisuke Baba
committed
Unsubscribe all on stopping the current flows
1 parent 35da9c5 commit 46667d8

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/generic-ble.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,16 @@ export default function(RED) {
242242
obj.object.on('data', func);
243243
return true;
244244
},
245+
unsubscribe: () => {
246+
return new Promise((resolve) => {
247+
if (obj.notifiable) {
248+
delete obj.dataListener;
249+
obj.object.unsubscribe(resolve);
250+
} else {
251+
return resolve();
252+
}
253+
});
254+
},
245255
};
246256
return obj;
247257
}
@@ -329,6 +339,9 @@ export default function(RED) {
329339
return Promise.resolve(peripheral.state);
330340
}
331341
},
342+
shutdown: () => {
343+
return Promise.all(this.characteristics.map((c) => c.unsubscribe()));
344+
},
332345
register: (node) => {
333346
this.nodes[node.id] = node;
334347
},
@@ -519,8 +532,9 @@ export default function(RED) {
519532
}
520533
});
521534
});
522-
this.on('close', () => {
535+
this.on('close', (done) => {
523536
Object.keys(configBleDevices).forEach(k => delete configBleDevices[k]);
537+
this.operations.shutdown().then(done).catch(done);
524538
});
525539
}
526540
}

0 commit comments

Comments
 (0)