From b60fbc2692e16c868616aa8a76c4524b945d6881 Mon Sep 17 00:00:00 2001 From: Ari Lazier Date: Fri, 27 May 2016 15:29:06 -0700 Subject: [PATCH] get basics workings --- examples/ReactExample/components/todo-app.js | 15 ++++---- .../src/impl/collection_notifier.cpp | 36 +++---------------- .../src/impl/collection_notifier.hpp | 14 +------- 3 files changed, 13 insertions(+), 52 deletions(-) diff --git a/examples/ReactExample/components/todo-app.js b/examples/ReactExample/components/todo-app.js index 5e497e4e..b75184b0 100644 --- a/examples/ReactExample/components/todo-app.js +++ b/examples/ReactExample/components/todo-app.js @@ -41,17 +41,19 @@ export default class TodoApp extends Component { // This is a Results object, which will live-update. this.todoLists = realm.objects('TodoList'); - this.todoLists.addListener(function(name, changes) { - console.log("changed: " + JSON.stringify(changes)); - }); - console.log("registered listener"); - if (this.todoLists.length < 1) { realm.write(() => { realm.create('TodoList', {name: 'Todo List'}); }); } + this.anotherList = realm.objects('TodoList'); + this.anotherList.addListener(function(name, changes) { + console.log("changed: " + JSON.stringify(changes)); + }); + console.log("registered listener"); + + // Bind all the methods that we will be passing as props. this.renderScene = this.renderScene.bind(this); this._addNewTodoList = this._addNewTodoList.bind(this); @@ -83,7 +85,6 @@ export default class TodoApp extends Component { component: TodoListView, passProps: { ref: 'listView', - items: this.todoLists, extraItems: extraItems, onPressItem: this._onPressTodoList, }, @@ -109,7 +110,7 @@ export default class TodoApp extends Component { } renderScene(route) { - return + return } _addNewTodoItem(list) { diff --git a/src/object-store/src/impl/collection_notifier.cpp b/src/object-store/src/impl/collection_notifier.cpp index 817f329e..ae2d074c 100644 --- a/src/object-store/src/impl/collection_notifier.cpp +++ b/src/object-store/src/impl/collection_notifier.cpp @@ -178,10 +178,8 @@ size_t CollectionNotifier::add_callback(CollectionChangeCallback callback) } m_callbacks.push_back({std::move(callback), token, false}); - if (m_callback_index == npos) { // Don't need to wake up if we're already sending notifications - Realm::Internal::get_coordinator(*m_realm).send_commit_notifications(); - m_have_callbacks = true; - } + Realm::Internal::get_coordinator(*m_realm).send_commit_notifications(); + return token; } @@ -200,22 +198,13 @@ void CollectionNotifier::remove_callback(size_t token) return; } - size_t idx = distance(begin(m_callbacks), it); - if (m_callback_index != npos && m_callback_index >= idx) { - --m_callback_index; - } - - old = std::move(*it); m_callbacks.erase(it); - - m_have_callbacks = !m_callbacks.empty(); } } void CollectionNotifier::remove_all_callbacks() { m_callbacks.clear(); - m_have_callbacks = false; } void CollectionNotifier::unregister() noexcept @@ -301,8 +290,8 @@ bool CollectionNotifier::deliver(Realm& realm, SharedGroup& sg, std::exception_p void CollectionNotifier::call_callbacks() { - while (auto fn = next_callback()) { - fn(m_changes_to_deliver, m_error); + for (auto &callback : m_callbacks) { + callback.fn(m_changes_to_deliver, m_error); } if (m_error) { @@ -313,23 +302,6 @@ void CollectionNotifier::call_callbacks() } } -CollectionChangeCallback CollectionNotifier::next_callback() -{ - std::lock_guard callback_lock(m_callback_mutex); - - for (++m_callback_index; m_callback_index < m_callbacks.size(); ++m_callback_index) { - auto& callback = m_callbacks[m_callback_index]; - if (!m_error && callback.initial_delivered && m_changes_to_deliver.empty()) { - continue; - } - callback.initial_delivered = true; - return callback.fn; - } - - m_callback_index = npos; - return nullptr; -} - void CollectionNotifier::attach_to(SharedGroup& sg) { REALM_ASSERT(!m_sg); diff --git a/src/object-store/src/impl/collection_notifier.hpp b/src/object-store/src/impl/collection_notifier.hpp index 97699cdb..29988eef 100644 --- a/src/object-store/src/impl/collection_notifier.hpp +++ b/src/object-store/src/impl/collection_notifier.hpp @@ -152,7 +152,7 @@ public: bool deliver(Realm&, SharedGroup&, std::exception_ptr); protected: - bool have_callbacks() const noexcept { return m_have_callbacks; } + bool have_callbacks() const noexcept { return m_callbacks.size() > 0; } void add_changes(CollectionChangeBuilder change) { m_accumulated_changes.merge(std::move(change)); } void set_table(Table const& table); std::unique_lock lock_target(); @@ -188,18 +188,6 @@ private: // while doing anything with them or m_callback_index std::mutex m_callback_mutex; std::vector m_callbacks; - - // Cached value for if m_callbacks is empty, needed to avoid deadlocks in - // run() due to lock-order inversion between m_callback_mutex and m_target_mutex - // It's okay if this value is stale as at worst it'll result in us doing - // some extra work. - std::atomic m_have_callbacks = {false}; - - // Iteration variable for looping over callbacks - // remove_callback() updates this when needed - size_t m_callback_index = npos; - - CollectionChangeCallback next_callback(); }; } // namespace _impl