Skip to content

Commit 584cf5d

Browse files
Googlera-maurice
authored andcommitted
Add FirebaseFirestore.ListenForSnapshotsInSync()
This is the first in a series that implements a new pattern for managing listener callbacks. Previously listeners needed to register a callback with C++ and maintain a dictionary of callbacks keyed off of a unique id and cleaned up once listening stopped or the firestore instance was disposed. The pattern wasn't implemented properly and had to be redone. This CL reimplements the pattern by: 1. Introducing a helper class, `ListenerRegistrationMap`, to keep a dictionary of all the callbacks. A static instance of this will get introduced for each `Listen*` method. 1. Updating `ListenerRegistration` to unregister callbacks when `Stop()` is called. 1. The C++ listener class that accepts the callback from C# will receive it in the native listen method instead of requiring a global callback registration. 1. In a future CL all the callbacks attached to a specific firestore instance will be pruned from its `ListenerRegistrationMap` instance. Once this pattern has been approved in this CL it will get applied to other `Listen*` methods in the API. PiperOrigin-RevId: 312713181
1 parent 9e4545c commit 584cf5d

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "firebase/csharp/snapshots_in_sync_listener.h"
2+
3+
#include "app/src/assert.h"
4+
5+
namespace firebase {
6+
namespace firestore {
7+
namespace csharp {
8+
9+
void SnapshotsInSyncListener::OnEvent(Error error) {
10+
firebase::callback::AddCallback(
11+
firebase::callback::NewCallback(callback_, callback_id_));
12+
}
13+
14+
/* static */
15+
ListenerRegistration SnapshotsInSyncListener::AddListenerTo(
16+
Firestore* firestore, int32_t callback_id,
17+
SnapshotsInSyncCallback callback) {
18+
SnapshotsInSyncListener listener(callback_id, callback);
19+
20+
return firestore->AddSnapshotsInSyncListener(
21+
[listener]() mutable { listener.OnEvent(Error::kOk); });
22+
}
23+
24+
} // namespace csharp
25+
} // namespace firestore
26+
} // namespace firebase
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#ifndef FIREBASE_FIRESTORE_CLIENT_CPP_SRC_INCLUDE_FIREBASE_CSHARP_SNAPSHOTS_IN_SYNC_LISTENER_H_
2+
#define FIREBASE_FIRESTORE_CLIENT_CPP_SRC_INCLUDE_FIREBASE_CSHARP_SNAPSHOTS_IN_SYNC_LISTENER_H_
3+
4+
#include <cstdint>
5+
6+
#include "app/src/callback.h"
7+
#include "firebase/firestore.h"
8+
#include "firebase/firestore/event_listener.h"
9+
#include "firebase/firestore/listener_registration.h"
10+
#include "firebase/firestore/firestore_errors.h"
11+
12+
namespace firebase {
13+
namespace firestore {
14+
namespace csharp {
15+
16+
// Add this to make this header compile when SWIG is not involved.
17+
#ifndef SWIGSTDCALL
18+
#if !defined(SWIG) && \
19+
(defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__))
20+
#define SWIGSTDCALL __stdcall
21+
#else
22+
#define SWIGSTDCALL
23+
#endif
24+
#endif
25+
26+
// The callbacks that are used by the listener, that need to reach back to C#
27+
// callbacks.
28+
typedef void(SWIGSTDCALL* SnapshotsInSyncCallback)(int callback_id);
29+
30+
// Provides a C++ implementation of the EventListener for
31+
// ListenForSnapshotsInSync that can forward the calls back to the C# delegates.
32+
class SnapshotsInSyncListener : public EventListener<void> {
33+
public:
34+
SnapshotsInSyncListener(int32_t callback_id,
35+
SnapshotsInSyncCallback callback)
36+
: callback_id_(callback_id), callback_(callback) {}
37+
38+
void OnEvent(Error error) override;
39+
40+
// This method is a proxy to Firestore::AddSnapshotsInSyncListener()
41+
// that can be easily called from C#. It allows our C# wrapper to
42+
// track user callbacks in a dictionary keyed off of a unique int
43+
// for each user callback and then raise the correct one later.
44+
static ListenerRegistration AddListenerTo(Firestore* firestore,
45+
int32_t callback_id,
46+
SnapshotsInSyncCallback callback);
47+
48+
private:
49+
int32_t callback_id_;
50+
SnapshotsInSyncCallback callback_;
51+
};
52+
53+
} // namespace csharp
54+
} // namespace firestore
55+
} // namespace firebase
56+
57+
#endif // FIREBASE_FIRESTORE_CLIENT_CPP_SRC_INCLUDE_FIREBASE_CSHARP_SNAPSHOTS_IN_SYNC_LISTENER_H_

0 commit comments

Comments
 (0)