Skip to content

Commit

Permalink
Merge pull request #586 from revtel/fix/android-duplicated-callback-i…
Browse files Browse the repository at this point in the history
…nvoke

fix(android): avoid possible duplicated JS callback invokation
  • Loading branch information
whitedogg13 authored Nov 15, 2022
2 parents ebb4d97 + 2eaf59f commit d0b23f1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
14 changes: 5 additions & 9 deletions android/src/main/java/community/revteltech/nfc/NfcManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void cancelTechnologyRequest(Callback callback) {
if (techRequest != null) {
techRequest.close();
try {
techRequest.getPendingCallback().invoke(ERR_CANCEL);
techRequest.invokePendingCallbackWithError(ERR_CANCEL);
} catch (RuntimeException ex) {
// the pending callback might already been invoked when there is an ongoing
// connected tag, bypass this case explicitly
Expand Down Expand Up @@ -1195,10 +1195,10 @@ public void onTagDiscovered(Tag tag) {
if (techRequest!= null && !techRequest.isConnected()) {
boolean result = techRequest.connect(tag);
if (result) {
techRequest.getPendingCallback().invoke(null, techRequest.getTechType());
techRequest.invokePendingCallback(techRequest.getTechType());
} else {
// this indicates that we get a NFC tag, but none of the user required tech is matched
techRequest.getPendingCallback().invoke(null, null);
techRequest.invokePendingCallback(null);
}
}
}
Expand Down Expand Up @@ -1297,10 +1297,6 @@ public void onNewIntent(Intent intent) {
WritableMap nfcTag = parseNfcIntent(intent);
if (nfcTag != null) {
if (isForegroundEnabled) {
Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Intent intentNew=new Intent("TagFound");
intentNew.putExtra("tag",tagFromIntent);
context.sendBroadcast(intentNew);
sendEvent("NfcManagerDiscoverTag", nfcTag);
} else {
sendEvent("NfcManagerDiscoverBackgroundTag", nfcTag);
Expand Down Expand Up @@ -1335,10 +1331,10 @@ private WritableMap parseNfcIntent(Intent intent) {
if (!techRequest.isConnected()) {
boolean result = techRequest.connect(tag);
if (result) {
techRequest.getPendingCallback().invoke(null, techRequest.getTechType());
techRequest.invokePendingCallback(techRequest.getTechType());
} else {
// this indicates that we get a NFC tag, but none of the user required tech is matched
techRequest.getPendingCallback().invoke(null, null);
techRequest.invokePendingCallback(null);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,18 @@ String getTechType() {
return mTechType;
}

Callback getPendingCallback() {
return mJsCallback;
void invokePendingCallbackWithError(String err) {
if (mJsCallback != null) {
mJsCallback.invoke(err);
mJsCallback = null;
}
}

void invokePendingCallback(String connectedTech) {
if (mJsCallback != null) {
mJsCallback.invoke(null, connectedTech);
mJsCallback = null;
}
}

TagTechnology getTechHandle() {
Expand Down

0 comments on commit d0b23f1

Please sign in to comment.