Skip to content

Commit d743f57

Browse files
committed
expose sync user subscribe in the c-api
1 parent 5533505 commit d743f57

File tree

5 files changed

+47
-0
lines changed

5 files changed

+47
-0
lines changed

src/realm.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3495,9 +3495,14 @@ typedef enum realm_flx_sync_subscription_set_state {
34953495
typedef void (*realm_sync_on_subscription_state_changed_t)(realm_userdata_t userdata,
34963496
realm_flx_sync_subscription_set_state_e state);
34973497

3498+
typedef void (*realm_user_changed_callback_t)(realm_userdata_t userdata, realm_user_state s);
3499+
3500+
34983501
typedef struct realm_async_open_task_progress_notification_token realm_async_open_task_progress_notification_token_t;
34993502
typedef struct realm_sync_session_connection_state_notification_token
35003503
realm_sync_session_connection_state_notification_token_t;
3504+
typedef struct realm_sync_user_subscription_token
3505+
realm_sync_user_subscription_token_t;
35013506

35023507
/**
35033508
* Callback function invoked by the async open task once the realm is open and fully synchronized.
@@ -3894,6 +3899,13 @@ RLM_API realm_sync_session_connection_state_notification_token_t* realm_sync_ses
38943899
realm_sync_session_t*, realm_sync_progress_func_t, realm_sync_progress_direction_e, bool is_streaming,
38953900
realm_userdata_t userdata, realm_free_userdata_func_t userdata_free) RLM_API_NOEXCEPT;
38963901

3902+
3903+
//register callback for tracking user status.
3904+
RLM_API realm_sync_user_subscription_token_t*
3905+
realm_sync_user_state_change_register_callback(
3906+
realm_user_t*, realm_user_changed_callback_t, realm_userdata_t userdata,
3907+
realm_free_userdata_func_t userdata_free);
3908+
38973909
/**
38983910
* Register a callback that will be invoked when all pending downloads have completed.
38993911
*/

src/realm/object-store/c_api/app.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,21 @@ RLM_API realm_app_t* realm_user_get_app(const realm_user_t* user) noexcept
750750
return nullptr;
751751
}
752752

753+
754+
RLM_API realm_sync_user_subscription_token_t*
755+
realm_sync_user_state_change_register_callback(realm_user_t* user, realm_user_changed_callback_t callback,
756+
realm_userdata_t userdata, realm_free_userdata_func_t userdata_free)
757+
{
758+
return wrap_err([&] {
759+
auto cb = [callback,
760+
userdata = SharedUserdata{userdata, FreeUserdata(userdata_free)}](const SyncUser& sync_user) {
761+
callback(userdata.get(), realm_user_state_e(sync_user.state()););
762+
};
763+
auto token = (*user)->subscribe(std::move(cb));
764+
return new realm_sync_user_subscription_token_t{user, token.value()};
765+
};
766+
}
767+
753768
template <typename T>
754769
inline util::Optional<T> convert_to_optional(T data)
755770
{

src/realm/object-store/c_api/sync.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ realm_sync_session_connection_state_notification_token::~realm_sync_session_conn
4040
session->unregister_connection_change_callback(token);
4141
}
4242

43+
realm_sync_user_subscription_token::~realm_sync_user_subscription_token()
44+
{
45+
user->unsubscribe({user.get(), token});
46+
}
47+
4348
namespace realm::c_api {
4449

4550
static_assert(realm_sync_client_metadata_mode_e(SyncClientConfig::MetadataMode::NoEncryption) ==

src/realm/object-store/c_api/types.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,18 @@ struct realm_results : realm::c_api::WrapC, realm::Results {
569569
};
570570

571571
#if REALM_ENABLE_SYNC
572+
573+
struct realm_sync_user_subscription_token : realm::c_api::WrapC {
574+
realm_sync_user_subscription_token(std::shared_ptr<realm::SyncUser> user, uint64_t token)
575+
: user(user)
576+
, token(token)
577+
{
578+
}
579+
~realm_sync_user_subscription_token();
580+
std::shared_ptr<realm::SyncUser> user;
581+
uint64_t token;
582+
};
583+
572584
struct realm_async_open_task_progress_notification_token : realm::c_api::WrapC {
573585
realm_async_open_task_progress_notification_token(std::shared_ptr<realm::AsyncOpenTask> task, uint64_t token)
574586
: task(task)

test/object-store/c_api/c_api.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,9 @@ TEST_CASE("C API (non-database)", "[c_api]") {
602602
realm_user_t* sync_user;
603603
auto user_data_free = [](realm_userdata_t) {};
604604

605+
realm_sync_user_state_change_register_callback(
606+
sync_user, [](realm_user_state_e state) {}, nullptr, nullptr);
607+
605608
// Verify the values above are included in the login request
606609
auto credentials = cptr(realm_app_credentials_new_anonymous(true));
607610
realm_app_log_in_with_credentials(

0 commit comments

Comments
 (0)