diff --git a/src/impl/node/weak_realm_notifier.cpp b/src/impl/node/weak_realm_notifier.cpp new file mode 100644 index 00000000..ebc188a6 --- /dev/null +++ b/src/impl/node/weak_realm_notifier.cpp @@ -0,0 +1,74 @@ +//////////////////////////////////////////////////////////////////////////// +// +// Copyright 2016 Realm Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//////////////////////////////////////////////////////////////////////////// + +#include "impl/weak_realm_notifier.hpp" + +#include "shared_realm.hpp" + +using namespace realm; +using namespace realm::_impl; + +WeakRealmNotifier::WeakRealmNotifier(const std::shared_ptr& realm, bool cache) +: WeakRealmNotifierBase(realm, cache) +, m_handle(new uv_async_t) +{ + m_handle->data = new std::weak_ptr(realm); + + // This assumes that only one thread matters: the main thread (default loop). + uv_async_init(uv_default_loop(), m_handle, [](uv_async_t* handle) { + auto realm_weak_ptr = static_cast*>(handle->data); + auto realm = realm_weak_ptr->lock(); + + if (realm) { + realm->notify(); + } + }); +} + +WeakRealmNotifier::WeakRealmNotifier(WeakRealmNotifier&& rgt) +: WeakRealmNotifierBase(std::move(rgt)) +{ + rgt.m_handle = nullptr; +} + +WeakRealmNotifier& WeakRealmNotifier::operator=(WeakRealmNotifier&& rgt) +{ + WeakRealmNotifierBase::operator=(std::move(rgt)); + m_handle = rgt.m_handle; + rgt.m_handle = nullptr; + + return *this; +} + +WeakRealmNotifier::~WeakRealmNotifier() +{ + if (m_handle) { + uv_close((uv_handle_t*)m_handle, [](uv_handle_t* handle) { + auto realm_weak_ptr = static_cast*>(handle->data); + delete realm_weak_ptr; + delete handle; + }); + } +} + +void WeakRealmNotifier::notify() +{ + if (m_handle) { + uv_async_send(m_handle); + } +} diff --git a/src/impl/node/weak_realm_notifier.hpp b/src/impl/node/weak_realm_notifier.hpp new file mode 100644 index 00000000..eab6b2a3 --- /dev/null +++ b/src/impl/node/weak_realm_notifier.hpp @@ -0,0 +1,47 @@ +//////////////////////////////////////////////////////////////////////////// +// +// Copyright 2016 Realm Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//////////////////////////////////////////////////////////////////////////// + +#include + +#include "impl/weak_realm_notifier_base.hpp" + +namespace realm { +class Realm; + +namespace _impl { + +class WeakRealmNotifier : public WeakRealmNotifierBase { +public: + WeakRealmNotifier(const std::shared_ptr& realm, bool cache); + ~WeakRealmNotifier(); + + WeakRealmNotifier(WeakRealmNotifier&&); + WeakRealmNotifier& operator=(WeakRealmNotifier&&); + + WeakRealmNotifier(const WeakRealmNotifier&) = delete; + WeakRealmNotifier& operator=(const WeakRealmNotifier&) = delete; + + // Asynchronously call notify() on the Realm on the main thread. + void notify(); + +private: + uv_async_t* m_handle; +}; + +} // namespace _impl +} // namespace realm diff --git a/src/impl/weak_realm_notifier.hpp b/src/impl/weak_realm_notifier.hpp index b0e4f595..138bf153 100644 --- a/src/impl/weak_realm_notifier.hpp +++ b/src/impl/weak_realm_notifier.hpp @@ -21,7 +21,9 @@ #include -#if REALM_PLATFORM_APPLE +#if REALM_PLATFORM_NODE +#include "impl/node/weak_realm_notifier.hpp" +#elif REALM_PLATFORM_APPLE #include "impl/apple/weak_realm_notifier.hpp" #else #include "impl/generic/weak_realm_notifier.hpp"