Skip to content

Commit 6815e02

Browse files
Googlera-maurice
authored andcommitted
Cleanup listener callbacks on DocumentReference and Query.
This CL is the last in a series described in cl/312713181, cl/317397413, and resolves b/156024690. In this CL the new pattern for managing listener callbacks implemented in `FirebaseFirestore.ListenForSnapshotsInSync()` is being applied to all other places where we expose listeners (i.e. `Query` and `DocumentReference`). PiperOrigin-RevId: 319089024
1 parent 492dffb commit 6815e02

File tree

5 files changed

+39
-125
lines changed

5 files changed

+39
-125
lines changed

firestore/src/include/firebase/csharp/document_event_listener.cc

Lines changed: 8 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,66 +7,27 @@ namespace firebase {
77
namespace firestore {
88
namespace csharp {
99

10-
::firebase::Mutex DocumentEventListener::g_mutex;
11-
DocumentEventListenerCallback
12-
DocumentEventListener::g_document_snapshot_event_listener_callback =
13-
nullptr;
14-
1510
void DocumentEventListener::OnEvent(const DocumentSnapshot& value,
1611
Error error) {
17-
MutexLock lock(g_mutex);
18-
if (g_document_snapshot_event_listener_callback) {
19-
firebase::callback::AddCallback(firebase::callback::NewCallback(
20-
DocumentSnapshotEvent, callback_id_, value, error));
21-
}
22-
}
23-
24-
/* static */
25-
void DocumentEventListener::SetCallback(
26-
DocumentEventListenerCallback callback) {
27-
MutexLock lock(g_mutex);
28-
if (!callback) {
29-
g_document_snapshot_event_listener_callback = nullptr;
30-
return;
31-
}
12+
// Ownership of this pointer is passed into the C# handler
13+
auto* copy = new DocumentSnapshot(value);
3214

33-
if (g_document_snapshot_event_listener_callback) {
34-
FIREBASE_ASSERT(g_document_snapshot_event_listener_callback == callback);
35-
} else {
36-
g_document_snapshot_event_listener_callback = callback;
37-
}
15+
callback_(callback_id_, copy, error);
3816
}
3917

4018
/* static */
4119
ListenerRegistration DocumentEventListener::AddListenerTo(
42-
int32_t callback_id, DocumentReference reference,
43-
MetadataChanges metadata_changes) {
44-
DocumentEventListener listener(callback_id);
45-
// TODO(zxu): For now, we call the one with lambda instead of EventListener
46-
// pointer so we do not have to manage the ownership of the listener. We
47-
// might want to extend the design to manage listeners and call the one with
48-
// EventListener parameter e.g. adding extra parameter in the API to specify
49-
// whether ownership is transferred.
50-
return reference.AddSnapshotListener(
20+
DocumentReference* reference, MetadataChanges metadata_changes,
21+
int32_t callback_id, DocumentEventListenerCallback callback) {
22+
DocumentEventListener listener(callback_id, callback);
23+
24+
return reference->AddSnapshotListener(
5125
metadata_changes,
5226
[listener](const DocumentSnapshot& value, Error error) mutable {
5327
listener.OnEvent(value, error);
5428
});
5529
}
5630

57-
/* static */
58-
void DocumentEventListener::DocumentSnapshotEvent(int callback_id,
59-
DocumentSnapshot value,
60-
Error error) {
61-
MutexLock lock(g_mutex);
62-
if (g_document_snapshot_event_listener_callback == nullptr) {
63-
return;
64-
}
65-
// The ownership is passed through the call to C# handler.
66-
DocumentSnapshot* copy = new DocumentSnapshot(value);
67-
g_document_snapshot_event_listener_callback(callback_id, copy, error);
68-
}
69-
7031
} // namespace csharp
7132
} // namespace firestore
7233
} // namespace firebase

firestore/src/include/firebase/csharp/document_event_listener.h

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,23 @@ typedef void(SWIGSTDCALL* DocumentEventListenerCallback)(int callback_id,
3434
// can forward the calls back to the C# delegates.
3535
class DocumentEventListener : public EventListener<DocumentSnapshot> {
3636
public:
37-
explicit DocumentEventListener(int32_t callback_id)
38-
: callback_id_(callback_id) {}
37+
explicit DocumentEventListener(int32_t callback_id,
38+
DocumentEventListenerCallback callback)
39+
: callback_id_(callback_id), callback_(callback) {}
3940

4041
void OnEvent(const DocumentSnapshot& value, Error error) override;
4142

42-
static void SetCallback(DocumentEventListenerCallback callback);
43-
44-
static ListenerRegistration AddListenerTo(int32_t callback_id,
45-
DocumentReference reference,
46-
MetadataChanges metadataChanges);
43+
// This method is a proxy to DocumentReference::AddSnapshotListener()
44+
// that can be easily called from C#. It allows our C# wrapper to
45+
// track user callbacks in a dictionary keyed off of a unique int
46+
// for each user callback and then raise the correct one later.
47+
static ListenerRegistration AddListenerTo(
48+
DocumentReference* reference, MetadataChanges metadata_changes,
49+
int32_t callback_id, DocumentEventListenerCallback callback);
4750

4851
private:
49-
static void DocumentSnapshotEvent(int callback_id, DocumentSnapshot value,
50-
Error error);
51-
5252
int32_t callback_id_;
53-
54-
// These static variables are named as global variable instead of private
55-
// class member.
56-
static Mutex g_mutex;
57-
static DocumentEventListenerCallback
58-
g_document_snapshot_event_listener_callback;
53+
DocumentEventListenerCallback callback_;
5954
};
6055

6156
} // namespace csharp

firestore/src/include/firebase/csharp/query_event_listener.cc

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,61 +7,26 @@ namespace firebase {
77
namespace firestore {
88
namespace csharp {
99

10-
::firebase::Mutex QueryEventListener::g_mutex;
11-
QueryEventListenerCallback
12-
QueryEventListener::g_query_snapshot_event_listener_callback = nullptr;
13-
1410
void QueryEventListener::OnEvent(const QuerySnapshot& value, Error error) {
15-
MutexLock lock(g_mutex);
16-
if (g_query_snapshot_event_listener_callback) {
17-
firebase::callback::AddCallback(firebase::callback::NewCallback(
18-
QuerySnapshotEvent, callback_id_, value, error));
19-
}
20-
}
21-
22-
/* static */
23-
void QueryEventListener::SetCallback(QueryEventListenerCallback callback) {
24-
MutexLock lock(g_mutex);
25-
if (!callback) {
26-
g_query_snapshot_event_listener_callback = nullptr;
27-
return;
28-
}
11+
// Ownership of this pointer is passed into the C# handler
12+
auto* copy = new QuerySnapshot(value);
2913

30-
if (g_query_snapshot_event_listener_callback) {
31-
FIREBASE_ASSERT(g_query_snapshot_event_listener_callback == callback);
32-
} else {
33-
g_query_snapshot_event_listener_callback = callback;
34-
}
14+
callback_(callback_id_, copy, error);
3515
}
3616

3717
/* static */
3818
ListenerRegistration QueryEventListener::AddListenerTo(
39-
int32_t callback_id, Query query, MetadataChanges metadata_changes) {
40-
QueryEventListener listener(callback_id);
41-
// TODO(zxu): For now, we call the one with lambda instead of EventListener
42-
// pointer so we do not have to manage the ownership of the listener. We
43-
// might want to extend the design to manage listeners and call the one with
44-
// EventListener parameter e.g. adding extra parameter in the API to specify
45-
// whether ownership is transferred.
46-
return query.AddSnapshotListener(
19+
Query* query, MetadataChanges metadata_changes, int32_t callback_id,
20+
QueryEventListenerCallback callback) {
21+
QueryEventListener listener(callback_id, callback);
22+
23+
return query->AddSnapshotListener(
4724
metadata_changes,
4825
[listener](const QuerySnapshot& value, Error error) mutable {
4926
listener.OnEvent(value, error);
5027
});
5128
}
5229

53-
/* static */
54-
void QueryEventListener::QuerySnapshotEvent(int callback_id,
55-
QuerySnapshot value, Error error) {
56-
MutexLock lock(g_mutex);
57-
if (g_query_snapshot_event_listener_callback == nullptr) {
58-
return;
59-
}
60-
// The ownership is passed through the call to C# handler.
61-
QuerySnapshot* copy = new QuerySnapshot(value);
62-
g_query_snapshot_event_listener_callback(callback_id, copy, error);
63-
}
64-
6530
} // namespace csharp
6631
} // namespace firestore
6732
} // namespace firebase

firestore/src/include/firebase/csharp/query_event_listener.h

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,23 @@ typedef void(SWIGSTDCALL* QueryEventListenerCallback)(int callback_id,
3333
// can forward the calls back to the C# delegates.
3434
class QueryEventListener : public EventListener<QuerySnapshot> {
3535
public:
36-
QueryEventListener(int32_t callback_id) : callback_id_(callback_id) {}
37-
38-
~QueryEventListener() override {}
36+
explicit QueryEventListener(int32_t callback_id,
37+
QueryEventListenerCallback callback)
38+
: callback_id_(callback_id), callback_(callback) {}
3939

4040
void OnEvent(const QuerySnapshot& value, Error error) override;
4141

42-
static void SetCallback(QueryEventListenerCallback callback);
43-
44-
static ListenerRegistration AddListenerTo(int32_t callback_id, Query query,
45-
MetadataChanges metadataChanges);
42+
// This method is a proxy to Query::AddSnapshotsListener()
43+
// that can be easily called from C#. It allows our C# wrapper to
44+
// track user callbacks in a dictionary keyed off of a unique int
45+
// for each user callback and then raise the correct one later.
46+
static ListenerRegistration AddListenerTo(
47+
Query* query, MetadataChanges metadata_changes, int32_t callback_id,
48+
QueryEventListenerCallback callback);
4649

4750
private:
48-
static void QuerySnapshotEvent(int callback_id, QuerySnapshot value,
49-
Error error);
50-
5151
int32_t callback_id_;
52-
53-
// These static variables are named as global variable instead of private
54-
// class member.
55-
static Mutex g_mutex;
56-
static QueryEventListenerCallback g_query_snapshot_event_listener_callback;
52+
QueryEventListenerCallback callback_;
5753
};
5854

5955
} // namespace csharp

firestore/src/include/firebase/csharp/snapshots_in_sync_listener.cc

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ namespace firebase {
66
namespace firestore {
77
namespace csharp {
88

9-
void SnapshotsInSyncListener::OnEvent(Error error) {
10-
firebase::callback::AddCallback(
11-
firebase::callback::NewCallback(callback_, callback_id_));
12-
}
9+
void SnapshotsInSyncListener::OnEvent(Error error) { callback_(callback_id_); }
1310

1411
/* static */
1512
ListenerRegistration SnapshotsInSyncListener::AddListenerTo(

0 commit comments

Comments
 (0)