Skip to content

Commit

Permalink
Fix and clean commentary
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Bourge committed Dec 13, 2018
1 parent 54470a8 commit a0942e1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,12 @@ interface WriteOperationAckStrategy extends ObservableTransformer<Boolean, Boole
*
* Timeouts after specified amount of time.
*
* The cache can be cleared to perform a new Gatt Discover and maybe find a service previously hidden
*
* @param timeout multiplier of TimeUnit after which the discovery will timeout in case of no return values
* @param timeUnit TimeUnit for the timeout
* @param clearCache Flag to ask the cache clearing (Warning, this functionnality can alter the good working of your app
* You should have strong knowledge of how it works)
* @return Observable emitting result a GATT service discovery.
* @throws BleGattCannotStartException with {@link BleGattOperationType#SERVICE_DISCOVERY} type, when it wasn't possible to start
* the discovery for internal reasons.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,31 @@ class ServiceDiscoveryManager {
}

Single<RxBleDeviceServices> getDiscoverServicesSingle(final long timeout, final TimeUnit timeoutTimeUnit) {
if (hasCachedResults) {
// optimisation to decrease the number of allocations
return deviceServicesObservable;
} else {
return deviceServicesObservable.doOnSubscribe(
new Consumer<Disposable>() {
@Override
public void accept(Disposable disposable) throws Exception {
timeoutBehaviorSubject.onNext(new TimeoutConfiguration(timeout, timeoutTimeUnit, Schedulers.computation()));
}
});
}
return getDiscoverServicesSingle(timeout, timeoutTimeUnit, false);
}

Single<RxBleDeviceServices> getDiscoverServicesSingle(final long timeout, final TimeUnit timeoutTimeUnit, Boolean clearCache) {
if (hasCachedResults) {
if (clearCache) {
hasCachedResults = false;
this.deviceServicesObservable = getTimeoutConfiguration().flatMap(scheduleActualDiscoveryWithTimeout())
.doOnSuccess(Functions.actionConsumer(new Action() {
@Override
public void run() throws Exception {
hasCachedResults = true;
}
}))
.doOnError(Functions.actionConsumer(new Action() {
@Override
public void run() {
reset();
}
}))
.cache();
}
if (hasCachedResults && !clearCache) {
// optimisation to decrease the number of allocations
return deviceServicesObservable;
} else {
if (clearCache) {
reset();
}
return deviceServicesObservable.doOnSubscribe(
new Consumer<Disposable>() {
@Override
Expand Down

0 comments on commit a0942e1

Please sign in to comment.