Skip to content

Commit

Permalink
Refactor to use single instead of casting back and forth
Browse files Browse the repository at this point in the history
  • Loading branch information
Giulio Petek committed Sep 8, 2016
1 parent 7378dbb commit b94833a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class OAuth2AccessTokenManagerGetValidAccessTokenSpecs extends AndroidSpecificat
tokenManager.getValidAccessToken(grant).subscribe()

then: "The refresh grant is asked for a new token TWICE"
2 * grant.grantNewAccessToken() >> just(accessToken)
2 * grant.grantNewAccessToken() >> just(accessToken) >> just(accessToken)
}

def "it should throw an IllegalArgumentException when the refreshAccessTokenGrant parameter is null"() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class OAuth2AccessTokenManager<TAccessToken extends OAuth2AccessToken> {
// Members

private final OAuth2AccessTokenStorage<TAccessToken> mStorage;
private Observable<TAccessToken> mTokenObservable;
private Single<TAccessToken> mTokenSingle;

// Constructor

Expand Down Expand Up @@ -72,8 +72,8 @@ public Single<TAccessToken> grantNewAccessToken(OAuth2Grant<TAccessToken> grant,
throw new IllegalArgumentException("Grant MUST NOT be null.");
}

if (mTokenObservable == null) {
mTokenObservable = grant
if (mTokenSingle == null) {
mTokenSingle = grant
.grantNewAccessToken()
.doOnSuccess(accessToken -> {
if (accessToken.expiresIn != null) {
Expand All @@ -84,13 +84,11 @@ public Single<TAccessToken> grantNewAccessToken(OAuth2Grant<TAccessToken> grant,

mStorage.storeAccessToken(accessToken);

mTokenObservable = null;
})
.toObservable()
.cache();
mTokenSingle = null;
});
}

return mTokenObservable.toSingle();
return mTokenSingle;
}

/**
Expand Down

0 comments on commit b94833a

Please sign in to comment.