From f8a6be49bdbd98a0449bfde15b6558416394109b Mon Sep 17 00:00:00 2001 From: Kenneth Geisshirt Date: Mon, 6 Feb 2023 15:15:34 +0100 Subject: [PATCH] Fix compilation errors --- CHANGELOG.md | 1 + src/js_sync.hpp | 43 ++++++++++++++++--------------------------- 2 files changed, 17 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad50d7585e7..b1fdc971cf1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Enhancements * Converting flexible sync Realms to bundled and local realms is now supported. ([realm/realm-core#6076](https://github.com/realm/realm-core/pull/6076)) +* For client reset mode `onRecoveryOrDiscard`, the `onDiscard` and `onRecovery` callbacks now have simple default values. ([#5288](https://github.com/realm/realm-js/pull/5288), since v11.1.0) ### Fixed * Fixed possible segfault in sync client where async callback was using object after being deallocated. ([realm/realm-core#6053](https://github.com/realm/realm-core/issues/6053), since v10.11.0) diff --git a/src/js_sync.hpp b/src/js_sync.hpp index c0c935fe2ba..89bf0a7d225 100644 --- a/src/js_sync.hpp +++ b/src/js_sync.hpp @@ -1303,34 +1303,23 @@ void SyncClass::populate_sync_config(ContextType ctx, ObjectType realm_constr } case realm::ClientResyncMode::RecoverOrDiscard: { - FunctionType client_reset_discard_callback; - ValueType client_reset_discard_value = - Object::get_property(ctx, client_reset_object, "onDiscard"); - if (Value::is_undefined(ctx, client_reset_discard_value)) { - auto realm = Value::validated_to_object(ctx, Object::get_global(ctx, "Realm")); - auto default_on_discard_callback = Value::validated_to_object( - ctx, Object::get_property(ctx, realm, "_defaultOnDiscardCallback")); - client_reset_discard_callback = - Value::validated_to_function(ctx, default_on_discard_callback); - } - else { - client_reset_discard_callback = Value::validated_to_function(ctx, client_reset_discard_value); - } + auto get_callback = [=](std::string callback_name, std::string default_callback_name) { + ValueType callback_value = Object::get_property(ctx, client_reset_object, callback_name); + if (Value::is_undefined(ctx, callback_value)) { + auto realm = Value::validated_to_object(ctx, Object::get_global(ctx, "Realm")); + auto default_callback_value = Value::validated_to_object( + ctx, Object::get_property(ctx, realm, default_callback_name)); + return Value::validated_to_function(ctx, default_callback_value); + } + else { + return Value::validated_to_function(ctx, callback_value); + } + }; - FunctionType client_reset_recovery_callback; - ValueType client_reset_recovery_value = - Object::get_property(ctx, client_reset_object, "onRecovery"); - if (Value::is_undefined(ctx, client_reset_recovery_value)) { - auto realm = Value::validated_to_object(ctx, Object::get_global(ctx, "Realm")); - auto default_on_recovery_callback = Value::validated_to_object( - ctx, Object::get_property(ctx, realm, "_defaultOnRecoveryCallback")); - client_reset_recovery_callback = - Value::validated_to_function(ctx, default_on_recovery_callback); - } - else { - client_reset_recovery_callback = - Value::validated_to_function(ctx, client_reset_recovery_value); - } + FunctionType client_reset_discard_callback = + get_callback("onDiscard", "_defaultOnDiscardCallback"); + FunctionType client_reset_recovery_callback = + get_callback("onRecovery", "_defaultOnRecoveryCallback"); auto client_reset_after_handler = util::EventLoopDispatcher(