Created
June 22, 2018 10:39
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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