get basics workings
This commit is contained in:
parent
03dad1ae90
commit
b60fbc2692
|
@ -41,17 +41,19 @@ export default class TodoApp extends Component {
|
||||||
|
|
||||||
// This is a Results object, which will live-update.
|
// This is a Results object, which will live-update.
|
||||||
this.todoLists = realm.objects('TodoList');
|
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) {
|
if (this.todoLists.length < 1) {
|
||||||
realm.write(() => {
|
realm.write(() => {
|
||||||
realm.create('TodoList', {name: 'Todo List'});
|
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.
|
// Bind all the methods that we will be passing as props.
|
||||||
this.renderScene = this.renderScene.bind(this);
|
this.renderScene = this.renderScene.bind(this);
|
||||||
this._addNewTodoList = this._addNewTodoList.bind(this);
|
this._addNewTodoList = this._addNewTodoList.bind(this);
|
||||||
|
@ -83,7 +85,6 @@ export default class TodoApp extends Component {
|
||||||
component: TodoListView,
|
component: TodoListView,
|
||||||
passProps: {
|
passProps: {
|
||||||
ref: 'listView',
|
ref: 'listView',
|
||||||
items: this.todoLists,
|
|
||||||
extraItems: extraItems,
|
extraItems: extraItems,
|
||||||
onPressItem: this._onPressTodoList,
|
onPressItem: this._onPressTodoList,
|
||||||
},
|
},
|
||||||
|
@ -109,7 +110,7 @@ export default class TodoApp extends Component {
|
||||||
}
|
}
|
||||||
|
|
||||||
renderScene(route) {
|
renderScene(route) {
|
||||||
return <route.component {...route.passProps} />
|
return <route.component items={this.todoLists} {...route.passProps} />
|
||||||
}
|
}
|
||||||
|
|
||||||
_addNewTodoItem(list) {
|
_addNewTodoItem(list) {
|
||||||
|
|
|
@ -178,10 +178,8 @@ size_t CollectionNotifier::add_callback(CollectionChangeCallback callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
m_callbacks.push_back({std::move(callback), token, false});
|
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();
|
||||||
Realm::Internal::get_coordinator(*m_realm).send_commit_notifications();
|
|
||||||
m_have_callbacks = true;
|
|
||||||
}
|
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -200,22 +198,13 @@ void CollectionNotifier::remove_callback(size_t token)
|
||||||
return;
|
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_callbacks.erase(it);
|
||||||
|
|
||||||
m_have_callbacks = !m_callbacks.empty();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CollectionNotifier::remove_all_callbacks()
|
void CollectionNotifier::remove_all_callbacks()
|
||||||
{
|
{
|
||||||
m_callbacks.clear();
|
m_callbacks.clear();
|
||||||
m_have_callbacks = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CollectionNotifier::unregister() noexcept
|
void CollectionNotifier::unregister() noexcept
|
||||||
|
@ -301,8 +290,8 @@ bool CollectionNotifier::deliver(Realm& realm, SharedGroup& sg, std::exception_p
|
||||||
|
|
||||||
void CollectionNotifier::call_callbacks()
|
void CollectionNotifier::call_callbacks()
|
||||||
{
|
{
|
||||||
while (auto fn = next_callback()) {
|
for (auto &callback : m_callbacks) {
|
||||||
fn(m_changes_to_deliver, m_error);
|
callback.fn(m_changes_to_deliver, m_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_error) {
|
if (m_error) {
|
||||||
|
@ -313,23 +302,6 @@ void CollectionNotifier::call_callbacks()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CollectionChangeCallback CollectionNotifier::next_callback()
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> 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)
|
void CollectionNotifier::attach_to(SharedGroup& sg)
|
||||||
{
|
{
|
||||||
REALM_ASSERT(!m_sg);
|
REALM_ASSERT(!m_sg);
|
||||||
|
|
|
@ -152,7 +152,7 @@ public:
|
||||||
bool deliver(Realm&, SharedGroup&, std::exception_ptr);
|
bool deliver(Realm&, SharedGroup&, std::exception_ptr);
|
||||||
|
|
||||||
protected:
|
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 add_changes(CollectionChangeBuilder change) { m_accumulated_changes.merge(std::move(change)); }
|
||||||
void set_table(Table const& table);
|
void set_table(Table const& table);
|
||||||
std::unique_lock<std::mutex> lock_target();
|
std::unique_lock<std::mutex> lock_target();
|
||||||
|
@ -188,18 +188,6 @@ private:
|
||||||
// while doing anything with them or m_callback_index
|
// while doing anything with them or m_callback_index
|
||||||
std::mutex m_callback_mutex;
|
std::mutex m_callback_mutex;
|
||||||
std::vector<Callback> m_callbacks;
|
std::vector<Callback> 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<bool> 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
|
} // namespace _impl
|
||||||
|
|
Loading…
Reference in New Issue