Skip to content

Instantly share code, notes, and snippets.

@eoinahern
Created June 22, 2018 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eoinahern/b9ec1ad54ff4e220adb1728f26117b30 to your computer and use it in GitHub Desktop.
Save eoinahern/b9ec1ad54ff4e220adb1728f26117b30 to your computer and use it in GitHub Desktop.
rx problem a guy was having on slack. put into a gist because the code was formatted badly. An attempt to make the code more readable.
Interceptor addHeadersInterceptor = chain -> {
if (tokenInvalid){
String token = getToken()); //If multiple call stays here blocked.
}
};
public String getToken() {
requestNewToken();
return tokenSubject.filter(token -> token != null).toBlocking().first();
}
private void requestNewToken() {
tokenDisposable = Observable.create((ObservableEmitter<String> emitter) -> {
try {
String token = getNewToken();
if (!emitter.isDisposed()) {
emitter.onNext(token);
}
} catch (Exception error) {
if (!emitter.isDisposed()) {
emitter.onError(error);
}
}
}).retryWhen(//Got some condition here to handle some exceptions)
.subscribe(token -> { tokenSubject.onNext(token); });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment