-
Notifications
You must be signed in to change notification settings - Fork 577
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from realm/tg/notifications
Add fine-grained notification stuff
- Loading branch information
Showing
55 changed files
with
7,793 additions
and
1,016 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//////////////////////////////////////////////////////////////////////////// | ||
// | ||
// 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 "collection_notifications.hpp" | ||
|
||
#include "impl/collection_notifier.hpp" | ||
|
||
using namespace realm; | ||
using namespace realm::_impl; | ||
|
||
NotificationToken::NotificationToken(std::shared_ptr<_impl::CollectionNotifier> notifier, size_t token) | ||
: m_notifier(std::move(notifier)), m_token(token) | ||
{ | ||
} | ||
|
||
NotificationToken::~NotificationToken() | ||
{ | ||
// m_notifier itself (and not just the pointed-to thing) needs to be accessed | ||
// atomically to ensure that there are no data races when the token is | ||
// destroyed after being modified on a different thread. | ||
// This is needed despite the token not being thread-safe in general as | ||
// users find it very surprising for obj-c objects to care about what | ||
// thread they are deallocated on. | ||
if (auto notifier = m_notifier.exchange({})) { | ||
notifier->remove_callback(m_token); | ||
} | ||
} | ||
|
||
NotificationToken::NotificationToken(NotificationToken&& rgt) = default; | ||
|
||
NotificationToken& NotificationToken::operator=(realm::NotificationToken&& rgt) | ||
{ | ||
if (this != &rgt) { | ||
if (auto notifier = m_notifier.exchange({})) { | ||
notifier->remove_callback(m_token); | ||
} | ||
m_notifier = std::move(rgt.m_notifier); | ||
m_token = rgt.m_token; | ||
} | ||
return *this; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
//////////////////////////////////////////////////////////////////////////// | ||
// | ||
// 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. | ||
// | ||
//////////////////////////////////////////////////////////////////////////// | ||
|
||
#ifndef REALM_COLLECTION_NOTIFICATIONS_HPP | ||
#define REALM_COLLECTION_NOTIFICATIONS_HPP | ||
|
||
#include "index_set.hpp" | ||
#include "util/atomic_shared_ptr.hpp" | ||
|
||
#include <exception> | ||
#include <functional> | ||
#include <memory> | ||
#include <vector> | ||
|
||
namespace realm { | ||
namespace _impl { | ||
class CollectionNotifier; | ||
} | ||
|
||
// A token which keeps an asynchronous query alive | ||
struct NotificationToken { | ||
NotificationToken() = default; | ||
NotificationToken(std::shared_ptr<_impl::CollectionNotifier> notifier, size_t token); | ||
~NotificationToken(); | ||
|
||
NotificationToken(NotificationToken&&); | ||
NotificationToken& operator=(NotificationToken&&); | ||
|
||
NotificationToken(NotificationToken const&) = delete; | ||
NotificationToken& operator=(NotificationToken const&) = delete; | ||
|
||
private: | ||
util::AtomicSharedPtr<_impl::CollectionNotifier> m_notifier; | ||
size_t m_token; | ||
}; | ||
|
||
struct CollectionChangeSet { | ||
struct Move { | ||
size_t from; | ||
size_t to; | ||
|
||
bool operator==(Move m) const { return from == m.from && to == m.to; } | ||
}; | ||
|
||
IndexSet deletions; | ||
IndexSet insertions; | ||
IndexSet modifications; | ||
std::vector<Move> moves; | ||
|
||
bool empty() const { return deletions.empty() && insertions.empty() && modifications.empty() && moves.empty(); } | ||
}; | ||
|
||
using CollectionChangeCallback = std::function<void (CollectionChangeSet, std::exception_ptr)>; | ||
} // namespace realm | ||
|
||
#endif // REALM_COLLECTION_NOTIFICATIONS_HPP |
Oops, something went wrong.