Skip to content

demo revision IDs suggestion for firebase installations #860

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,8 @@ public Task<String> getId() {
*/
@NonNull
@Override
public synchronized Task<InstallationTokenResult> getAuthToken(
@AuthTokenOption int authTokenOption) {
if (authTokenOption == FORCE_REFRESH) {
shouldRefreshAuthToken = true;
}
Task<InstallationTokenResult> task = addGetAuthTokenListener();
public Task<InstallationTokenResult> getAuthToken(@AuthTokenOption int authTokenOption) {
Task<InstallationTokenResult> task = addGetAuthTokenListener(authTokenOption);
executor.execute(doRegistration());
return task;
}
Expand All @@ -173,12 +169,13 @@ private Task<String> addGetIdListener() {
return taskCompletionSource.getTask();
}

private Task<InstallationTokenResult> addGetAuthTokenListener() {
TaskCompletionSource<InstallationTokenResult> taskCompletionSource =
new TaskCompletionSource<>();
StateListener l = new GetAuthTokenListener(utils, taskCompletionSource);
private Task<InstallationTokenResult> addGetAuthTokenListener(@AuthTokenOption int authTokenOption) {
TaskCompletionSource<InstallationTokenResult> taskCompletionSource = new TaskCompletionSource<>();
synchronized (lock) {
listeners.add(l);
if (authTokenOption == FORCE_REFRESH) {
desiredRevisionId = currentRevisionId + 1;
}
listeners.add(new GetAuthTokenListener(utils, taskCompletionSource, desiredRevisionId););
}
return taskCompletionSource.getTask();
}
Expand All @@ -188,7 +185,8 @@ private void triggerOnStateReached(PersistedFidEntry persistedFidEntry) {
Iterator<StateListener> it = listeners.iterator();
while (it.hasNext()) {
StateListener l = it.next();
boolean doneListening = l.onStateReached(persistedFidEntry);
// TODO(ankitaj224): add this param to the `onStateReached` interface method
boolean doneListening = l.onStateReached(currentRevisionId, persistedFidEntry);
if (doneListening) {
it.remove();
}
Expand All @@ -208,25 +206,36 @@ private final Runnable doRegistration() {
persistedFidEntry = persistedFid.readPersistedFidEntryValue();
}

// If Auth Token needs refresh, notify the listeners only after force refresh
if (!shouldRefreshAuthToken) {
triggerOnStateReached(persistedFidEntry);
}
triggerOnStateReached(persistedFidEntry);

// FID needs to be registered
if (persistedFidEntry.isUnregistered()) {
registerAndSaveFid(persistedFidEntry);
persistedFidEntry = persistedFid.readPersistedFidEntryValue();
// we just registered, so any requests that were waiting on a fresher token will be satisfied, no refresh required
synchronized (lock) {
currentRevisionId = desiredRevisionId;
}
}

// Don't notify the listeners at this point; we might as well make ure the auth token is up
// to date before letting them know.

boolean needRefresh = utils.isAuthTokenExpired(persistedFidEntry);
if (!needRefresh) {
synchronized (lock) {
needRefresh = desiredRevisionId > currentRevisionId;
}
}

// Refresh Auth token if needed
if (shouldRefreshAuthToken || utils.isAuthTokenExpired(persistedFidEntry)) {
shouldRefreshAuthToken = false;
if (needRefresh) {
fetchAuthTokenFromServer(persistedFidEntry);
persistedFidEntry = persistedFid.readPersistedFidEntryValue();

synchronized (lock) {
currentRevisionId = desiredRevisionId;
}
}

triggerOnStateReached(persistedFidEntry);
Expand Down