package expo.modules.google.signin;

import org.unimodules.core.Promise;

import static expo.modules.google.signin.GoogleSignInModule.ERROR_CONCURRENT_TASK_IN_PROGRESS;

public class AuthTask {
    private Promise mPromise;
    private String mTag;

    public boolean update(Promise promise, String tag) {
        if (mPromise == null) {
            mPromise = promise;
            mTag = tag;
            return true;
        } else {
            promise.reject(ERROR_CONCURRENT_TASK_IN_PROGRESS, "cannot set promise - some async operation is still in progress");
            return false;
        }
    }

    public void resolve(Object value) {
        if (mPromise == null) return;
        mPromise.resolve(value);
        clear();
    }

    public void reject(String code, String message) {
        if (mPromise == null) return;
        mPromise.reject(code, "GoogleSignIn." + mTag + ": " + message);
        clear();
    }

    private void clear() {
        mPromise = null;
        mTag = null;
    }
}

@@ -654,85 +654,122 @@
     
-if (mPromise == null) return;%0A        mPromise.resolve(value);%0A        clear(
+Promise resolver = mPromise;%0A        if (resolver == null) return;%0A        clear();%0A        resolver.resolve(value
 );%0A 
@@ -832,36 +832,73 @@
 ) %7B%0A        
-if (m
 Promise
+ rejecter = mPromise;%0A        if (rejecter
  == null) re
@@ -911,24 +911,41 @@
         
-mPromise
+clear();%0A        rejecter
 .reject(
@@ -992,33 +992,16 @@
 ssage);%0A
-        clear();%0A
     %7D%0A%0A 

fix race condition in AuthTask (#5872)