Skip to content

Commit

Permalink
fix(firestore, android): event handling
Browse files Browse the repository at this point in the history
  • Loading branch information
russellwheatley committed Aug 6, 2024
1 parent bd852a7 commit bad8f09
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ Task<Void> waitForPendingWrites(String appName, String databaseId) {

Task<Void> terminate(String appName, String databaseId) {
FirebaseFirestore firebaseFirestore = getFirestoreForApp(appName, databaseId);

if (instanceCache.get(appName) != null) {
instanceCache.get(appName).clear();
instanceCache.remove(appName);
String firestoreKey = createFirestoreKey(appName, databaseId);
if (instanceCache.get(firestoreKey) != null) {
instanceCache.get(firestoreKey).clear();
instanceCache.remove(firestoreKey);
}

return firebaseFirestore.terminate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ public void namedQueryOnSnapshot(
if (task.isSuccessful()) {
Query query = task.getResult();
if (query == null) {
sendOnSnapshotError(appName, listenerId, new NullPointerException());
sendOnSnapshotError(appName, databaseId, listenerId, new NullPointerException());
} else {
ReactNativeFirebaseFirestoreQuery firestoreQuery =
new ReactNativeFirebaseFirestoreQuery(
appName, query, filters, orders, options);
handleQueryOnSnapshot(firestoreQuery, appName, listenerId, listenerOptions);
handleQueryOnSnapshot(firestoreQuery, appName, databaseId, listenerId, listenerOptions);
}
} else {
sendOnSnapshotError(appName, listenerId, task.getException());
sendOnSnapshotError(appName, databaseId, listenerId, task.getException());
}
});
}
Expand All @@ -106,7 +106,7 @@ public void collectionOnSnapshot(
new ReactNativeFirebaseFirestoreQuery(
appName, getQueryForFirestore(firebaseFirestore, path, type), filters, orders, options);

handleQueryOnSnapshot(firestoreQuery, appName, listenerId, listenerOptions);
handleQueryOnSnapshot(firestoreQuery, appName, databaseId, listenerId, listenerOptions);
}

@ReactMethod
Expand Down Expand Up @@ -203,6 +203,7 @@ public void collectionGet(
private void handleQueryOnSnapshot(
ReactNativeFirebaseFirestoreQuery firestoreQuery,
String appName,
String databaseId,
int listenerId,
ReadableMap listenerOptions) {
MetadataChanges metadataChanges;
Expand All @@ -223,9 +224,9 @@ private void handleQueryOnSnapshot(
listenerRegistration.remove();
collectionSnapshotListeners.remove(listenerId);
}
sendOnSnapshotError(appName, listenerId, exception);
sendOnSnapshotError(appName, databaseId, listenerId, exception);
} else {
sendOnSnapshotEvent(appName, listenerId, querySnapshot, metadataChanges);
sendOnSnapshotEvent(appName, databaseId, listenerId, querySnapshot, metadataChanges);
}
};

Expand All @@ -251,6 +252,7 @@ private void handleQueryGet(

private void sendOnSnapshotEvent(
String appName,
String databaseId,
int listenerId,
QuerySnapshot querySnapshot,
MetadataChanges metadataChanges) {
Expand All @@ -271,14 +273,15 @@ private void sendOnSnapshotEvent(
ReactNativeFirebaseFirestoreEvent.COLLECTION_EVENT_SYNC,
body,
appName,
databaseId,
listenerId));
} else {
sendOnSnapshotError(appName, listenerId, task.getException());
sendOnSnapshotError(appName, databaseId, listenerId, task.getException());
}
});
}

private void sendOnSnapshotError(String appName, int listenerId, Exception exception) {
private void sendOnSnapshotError(String appName, String databaseId, int listenerId, Exception exception) {
WritableMap body = Arguments.createMap();
WritableMap error = Arguments.createMap();

Expand All @@ -298,7 +301,7 @@ private void sendOnSnapshotError(String appName, int listenerId, Exception excep

emitter.sendEvent(
new ReactNativeFirebaseFirestoreEvent(
ReactNativeFirebaseFirestoreEvent.COLLECTION_EVENT_SYNC, body, appName, listenerId));
ReactNativeFirebaseFirestoreEvent.COLLECTION_EVENT_SYNC, body, appName, databaseId, listenerId));
}

private Source getSource(ReadableMap getOptions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ public void documentOnSnapshot(
listenerRegistration.remove();
documentSnapshotListeners.remove(listenerId);
}
sendOnSnapshotError(appName, listenerId, exception);
sendOnSnapshotError(appName, databaseId, listenerId, exception);
} else {
sendOnSnapshotEvent(appName, listenerId, documentSnapshot);
sendOnSnapshotEvent(appName, databaseId, listenerId, documentSnapshot);
}
};

Expand Down Expand Up @@ -282,7 +282,7 @@ public void documentBatch(String appName, String databaseId, ReadableArray write
}

private void sendOnSnapshotEvent(
String appName, int listenerId, DocumentSnapshot documentSnapshot) {
String appName,String databaseId, int listenerId, DocumentSnapshot documentSnapshot) {
Tasks.call(getExecutor(), () -> snapshotToWritableMap(appName, documentSnapshot))
.addOnCompleteListener(
task -> {
Expand All @@ -298,14 +298,15 @@ private void sendOnSnapshotEvent(
ReactNativeFirebaseFirestoreEvent.DOCUMENT_EVENT_SYNC,
body,
appName,
databaseId,
listenerId));
} else {
sendOnSnapshotError(appName, listenerId, task.getException());
sendOnSnapshotError(appName, databaseId, listenerId, task.getException());
}
});
}

private void sendOnSnapshotError(String appName, int listenerId, Exception exception) {
private void sendOnSnapshotError(String appName, String databaseId, int listenerId, Exception exception) {
WritableMap body = Arguments.createMap();
WritableMap error = Arguments.createMap();

Expand All @@ -325,6 +326,6 @@ private void sendOnSnapshotError(String appName, int listenerId, Exception excep

emitter.sendEvent(
new ReactNativeFirebaseFirestoreEvent(
ReactNativeFirebaseFirestoreEvent.DOCUMENT_EVENT_SYNC, body, appName, listenerId));
ReactNativeFirebaseFirestoreEvent.DOCUMENT_EVENT_SYNC, body, appName, databaseId, listenerId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,19 @@ public class ReactNativeFirebaseFirestoreEvent implements NativeEvent {
private static final String KEY_BODY = "body";
private static final String KEY_APP_NAME = "appName";
private static final String KEY_EVENT_NAME = "eventName";
private static final String DATABASE_ID = "databaseId";
private String eventName;
private WritableMap eventBody;
private String appName;
private String databaseId;
private int listenerId;

ReactNativeFirebaseFirestoreEvent(
String eventName, WritableMap eventBody, String appName, int listenerId) {
String eventName, WritableMap eventBody, String appName, String databaseId, int listenerId) {
this.eventName = eventName;
this.eventBody = eventBody;
this.appName = appName;
this.databaseId = databaseId;
this.listenerId = listenerId;
}

Expand All @@ -54,6 +57,7 @@ public WritableMap getEventBody() {
event.putInt(KEY_ID, listenerId);
event.putMap(KEY_BODY, eventBody);
event.putString(KEY_APP_NAME, appName);
event.putString(DATABASE_ID, databaseId);
event.putString(KEY_EVENT_NAME, eventName);
return event;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public void transactionBegin(String appName, String databaseId, int transactionI
ReactNativeFirebaseFirestoreEvent.TRANSACTION_EVENT_SYNC,
eventMap,
transactionHandler.getAppName(),
databaseId,
transactionHandler.getTransactionId()));
});

Expand Down Expand Up @@ -227,6 +228,7 @@ public void transactionBegin(String appName, String databaseId, int transactionI
ReactNativeFirebaseFirestoreEvent.TRANSACTION_EVENT_SYNC,
eventMap,
transactionHandler.getAppName(),
databaseId,
transactionHandler.getTransactionId()));
} else {
eventMap.putString("type", "error");
Expand All @@ -247,6 +249,7 @@ public void transactionBegin(String appName, String databaseId, int transactionI
ReactNativeFirebaseFirestoreEvent.TRANSACTION_EVENT_SYNC,
eventMap,
transactionHandler.getAppName(),
databaseId,
transactionHandler.getTransactionId()));
}
});
Expand Down

0 comments on commit bad8f09

Please sign in to comment.