Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client reset w/recovery (#4711) #5047

Merged
merged 4 commits into from
Nov 1, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix linting error. Fix compilation against JSI.
  • Loading branch information
kneth committed Nov 1, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 80c1548b0fa9f6e231eadc70aa1c042eb0c7fdd8
10 changes: 5 additions & 5 deletions integration-tests/tests/src/tests/sync/client-reset.ts
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ const FlexibleDogSchema = { ...DogSchema, properties: { ...DogSchema.properties,
*/
function addSubscriptions(realm: Realm): void {
const subs = realm.subscriptions;
subs.update((mutableSubs) => {
subs.update((mutableSubs: Realm.App.Sync.MutableSubscriptionSet) => {
mutableSubs.add(realm.objects(FlexiblePersonSchema.name));
mutableSubs.add(realm.objects(FlexibleDogSchema.name));
});
@@ -340,7 +340,7 @@ function getSchema(useFlexibleSync: boolean) {
it(`handles manual simulated client resets by callback with ${getPartialTestTitle(
useFlexibleSync,
)} sync enabled`, async function (this: RealmContext) {
return new Promise((resolve, _) => {
return new Promise<void>((resolve, _) => {
const config: Realm.Configuration = {
schema: getSchema(useFlexibleSync),
sync: {
@@ -349,7 +349,7 @@ function getSchema(useFlexibleSync: boolean) {
user: this.user,
clientReset: {
mode: ClientResetMode.Manual,
onManual: (session, path) => {
onManual: (session: Realm.App.Sync.Session, path: string) => {
expect(session).to.be.not.null;
expect(path).to.not.empty;
resolve();
@@ -380,7 +380,7 @@ function getSchema(useFlexibleSync: boolean) {
...(useFlexibleSync ? { flexible: true } : { partitionValue: getPartitionValue() }),
user: this.user,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
error: (_) => {
onError: (_) => {
reject();
},
clientReset: {
@@ -415,7 +415,7 @@ function getSchema(useFlexibleSync: boolean) {
sync: {
user: this.user,
...(useFlexibleSync ? { flexible: true } : { partitionValue: getPartitionValue() }),
error: () => {
onError: () => {
resolve();
},
clientReset: {
60 changes: 29 additions & 31 deletions src/js_sync.hpp
Original file line number Diff line number Diff line change
@@ -202,13 +202,13 @@ class ClientResetAfterFunctor {
void operator()(SharedRealm before_realm, ThreadSafeReference after_realm_ref, bool did_recover)
{
HANDLESCOPE(m_ctx);
typename T::Value arguments[2];

SharedRealm after_realm =
Realm::get_shared_realm(std::move(after_realm_ref), util::Scheduler::make_default());

arguments[0] = create_object<T, RealmClass<T>>(m_ctx, new SharedRealm(before_realm));
arguments[1] = create_object<T, RealmClass<T>>(m_ctx, new SharedRealm(after_realm));
typename T::Value arguments[] = {
create_object<T, RealmClass<T>>(m_ctx, new SharedRealm(before_realm)),
create_object<T, RealmClass<T>>(m_ctx, new SharedRealm(after_realm)),
};
Function<T>::callback(m_ctx, m_func, 2, arguments);
}

@@ -243,20 +243,23 @@ class ClientResetAfterRecoveryOrDiscardFunctor {
void operator()(SharedRealm before_realm, ThreadSafeReference after_realm_ref, bool did_recover)
{
HANDLESCOPE(m_ctx);
typename T::Value arguments[2];

SharedRealm after_realm =
Realm::get_shared_realm(std::move(after_realm_ref), util::Scheduler::make_default());


if (did_recover) {
arguments[0] = create_object<T, RealmClass<T>>(m_ctx, new SharedRealm(before_realm));
arguments[1] = create_object<T, RealmClass<T>>(m_ctx, new SharedRealm(after_realm));
typename T::Value arguments[] = {
create_object<T, RealmClass<T>>(m_ctx, new SharedRealm(before_realm)),
create_object<T, RealmClass<T>>(m_ctx, new SharedRealm(after_realm)),
};
Function<T>::callback(m_ctx, m_func, 2, arguments);
}
else {
arguments[0] = create_object<T, SessionClass<T>>(m_ctx, new WeakSession(before_realm->sync_session()));
arguments[1] = Value<T>::from_string(m_ctx, before_realm->config().path);
typename T::Value arguments[] = {
create_object<T, SessionClass<T>>(m_ctx, new WeakSession(before_realm->sync_session())),
Value<T>::from_string(m_ctx, before_realm->config().path),
};
Function<T>::callback(m_ctx, m_discard_func, 2, arguments);
}
}
@@ -305,10 +308,10 @@ class ClientResetBeforeFunctor {
template <typename T>
class SyncSessionErrorBase {
public:
virtual typename T::Function func()
{
return typename T::Function();
};
virtual typename T::Function func();
// {
// return typename T::Function();
// };
virtual void operator()(std::shared_ptr<SyncSession>, SyncError){};
};

@@ -326,12 +329,12 @@ class SyncSessionClientResetManualFunctor : public SyncSessionErrorBase<T> {
#endif
}

typename T::Function func() const
typename T::Function func() override
{
return m_client_reset_func;
}

void operator()(std::shared_ptr<SyncSession> session, SyncError error)
void operator()(std::shared_ptr<SyncSession> session, SyncError error) override
{
HANDLESCOPE(m_ctx);

@@ -366,12 +369,12 @@ class SyncSessionErrorAndClientResetManualFunctor : SyncSessionErrorBase<T> {
#endif
}

typename T::Function func() const
typename T::Function func() override
{
return m_func;
}

void operator()(std::shared_ptr<SyncSession> session, SyncError error)
void operator()(std::shared_ptr<SyncSession> session, SyncError error) override
{
HANDLESCOPE(m_ctx);

@@ -430,12 +433,12 @@ class SyncSessionErrorHandlerFunctor : SyncSessionErrorBase<T> {
#endif
}

typename T::Function func() const
typename T::Function func() override
{
return m_func;
}

void operator()(std::shared_ptr<SyncSession> session, SyncError error)
void operator()(std::shared_ptr<SyncSession> session, SyncError error) override
{
HANDLESCOPE(m_ctx);

@@ -1303,27 +1306,22 @@ void SyncClass<T>::populate_sync_config(ContextType ctx, ObjectType realm_constr
}

case realm::ClientResyncMode::RecoverOrDiscard: {
FunctionType client_reset_recovery_callback;
FunctionType client_reset_discard_callback;

ValueType client_reset_after_value = Object::get_property(ctx, client_reset_object, "onDiscard");
if (!Value::is_undefined(ctx, client_reset_after_value)) {
client_reset_discard_callback = Value::validated_to_function(ctx, client_reset_after_value);
}
else {
if (Value::is_undefined(ctx, client_reset_after_value)) {
throw std::invalid_argument("'onDiscard' is required");
}

ValueType client_reset_recovery_value =
Object::get_property(ctx, client_reset_object, "onRecovery");
if (!Value::is_undefined(ctx, client_reset_after_value)) {
client_reset_recovery_callback =
Value::validated_to_function(ctx, client_reset_recovery_value);
}
else {
if (Value::is_undefined(ctx, client_reset_after_value)) {
throw std::invalid_argument("'onRecovery' is required");
}

FunctionType client_reset_discard_callback =
Value::validated_to_function(ctx, client_reset_after_value);
FunctionType client_reset_recovery_callback =
Value::validated_to_function(ctx, client_reset_recovery_value);

auto client_reset_after_handler =
util::EventLoopDispatcher<void(SharedRealm, ThreadSafeReference, bool)>(
ClientResetAfterRecoveryOrDiscardFunctor<T>(ctx, client_reset_recovery_callback,