Skip to content

Commit d12c38d

Browse files
authored
expose sync user subscribe in the c-api (#7302)
* expose sync user subscribe in the c-api
1 parent 1a8c84c commit d12c38d

File tree

6 files changed

+54
-0
lines changed

6 files changed

+54
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### Enhancements
44
* <New feature description> (PR [#????](https://github.com/realm/realm-core/pull/????))
5+
* Add support in the C API for receiving a notification when sync user state changes. ([#7302](https://github.com/realm/realm-core/pull/7302))
56
* Allow the query builder to construct >, >=, <, <= queries for string constants. This is a case sensitive lexicographical comparison. Improved performance of RQL (parsed) queries on a non-linked string property using: >, >=, <, <=, operators and fixed behaviour that a null string should be evaulated as less than everything, previously nulls were not matched. ([#3939](https://github.com/realm/realm-core/issues/3939), this is a prerequisite for https://github.com/realm/realm-swift/issues/8008).
67

78
### Fixed

src/realm.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3495,9 +3495,13 @@ 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_sync_on_user_state_changed_t)(realm_userdata_t userdata, realm_user_state_e 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 realm_sync_user_subscription_token_t;
35013505

35023506
/**
35033507
* Callback function invoked by the async open task once the realm is open and fully synchronized.
@@ -3894,6 +3898,15 @@ RLM_API realm_sync_session_connection_state_notification_token_t* realm_sync_ses
38943898
realm_sync_session_t*, realm_sync_progress_func_t, realm_sync_progress_direction_e, bool is_streaming,
38953899
realm_userdata_t userdata, realm_free_userdata_func_t userdata_free) RLM_API_NOEXCEPT;
38963900

3901+
3902+
/**
3903+
* @return a notification token object. Dispose it to stop receiving notifications.
3904+
*/
3905+
RLM_API realm_sync_user_subscription_token_t*
3906+
realm_sync_user_on_state_change_register_callback(realm_user_t*, realm_sync_on_user_state_changed_t,
3907+
realm_userdata_t userdata,
3908+
realm_free_userdata_func_t userdata_free);
3909+
38973910
/**
38983911
* Register a callback that will be invoked when all pending downloads have completed.
38993912
*/

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_on_state_change_register_callback(realm_user_t* user, realm_sync_on_user_state_changed_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, std::move(token)};
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(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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,19 @@ 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+
using Token = realm::Subscribable<realm::SyncUser>::Token;
575+
realm_sync_user_subscription_token(std::shared_ptr<realm::SyncUser> user, Token&& token)
576+
: user(user)
577+
, token(std::move(token))
578+
{
579+
}
580+
~realm_sync_user_subscription_token();
581+
std::shared_ptr<realm::SyncUser> user;
582+
Token token;
583+
};
584+
572585
struct realm_async_open_task_progress_notification_token : realm::c_api::WrapC {
573586
realm_async_open_task_progress_notification_token(std::shared_ptr<realm::AsyncOpenTask> task, uint64_t token)
574587
: task(task)

test/object-store/c_api/c_api.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,12 @@ TEST_CASE("C API (non-database)", "[c_api]") {
615615
},
616616
&sync_user, user_data_free);
617617

618+
auto user_state = [](realm_userdata_t, realm_user_state_e state) {
619+
CHECK(state == RLM_USER_STATE_LOGGED_IN);
620+
};
621+
auto token =
622+
realm_sync_user_on_state_change_register_callback(sync_user, user_state, nullptr, user_data_free);
623+
618624
auto check_base_url = [&](std::string expected) {
619625
CHECK(transport->get_location_called());
620626
auto app_base_url = realm_app_get_base_url(test_app.get());
@@ -653,6 +659,7 @@ TEST_CASE("C API (non-database)", "[c_api]") {
653659
update_and_check_base_url("", default_base_url);
654660

655661
realm_release(sync_user);
662+
realm_release(token);
656663
}
657664
#endif // REALM_ENABLE_SYNC
658665
}

0 commit comments

Comments
 (0)