beaufortfrancois / sandbox

:baby_chick:
MIT License
71 stars 32 forks source link

Save references to services and characteristics, same as device #12

Open danjenkins opened 7 years ago

danjenkins commented 7 years ago

Instead of accessing the same services and charcteristics over and over - put these references in the constructor and reference them there

beaufortfrancois commented 7 years ago

Are you talking about https://beaufortfrancois.github.io/sandbox/web-bluetooth/generator/?

danjenkins commented 7 years ago

Oh sorry, Yes - I see it as its own thing :D

beaufortfrancois commented 7 years ago

I see why it is tempting to cache all of them. That's what I do at https://googlechrome.github.io/samples/web-bluetooth/read-characteristic-value-changed.html

However, I also like using getPrimaryService and getCharacteristic instead as I'm sure to always get "live" services/characteristics no matter what.

danjenkins commented 7 years ago

Hmmm, Just seems like a lot of code duplication and not ideal. By the time you add a couple more characteristics to the class, your class has a tonne of duplicate code. How would you feel about having a function that returned a promise and you'd pass the service and characteristic to the function - you'd then be able to reuse this for other characteristics (someone extending the class) and re-use it in the existing methods - meaning you could replace all of the getPrimaryService and getCharacteristic chained calls with this one function call?