Skip to content

Commit 1d34e78

Browse files
committed
Merge pull request #61 from realm/tg/notifications
Add fine-grained notification stuff
2 parents deea1e8 + db55770 commit 1d34e78

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+7793
-1016
lines changed

src/CMakeLists.txt

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,40 @@
11
set(SOURCES
2+
collection_notifications.cpp
23
index_set.cpp
34
list.cpp
45
object_schema.cpp
56
object_store.cpp
67
results.cpp
78
schema.cpp
89
shared_realm.cpp
9-
impl/async_query.cpp
10+
impl/collection_change_builder.cpp
11+
impl/collection_notifier.cpp
12+
impl/list_notifier.cpp
1013
impl/realm_coordinator.cpp
14+
impl/results_notifier.cpp
1115
impl/transact_log_handler.cpp
1216
parser/parser.cpp
1317
parser/query_builder.cpp)
1418

1519
set(HEADERS
20+
collection_notifications.hpp
1621
index_set.hpp
1722
list.hpp
1823
object_schema.hpp
1924
object_store.hpp
25+
property.hpp
2026
results.hpp
2127
schema.hpp
2228
shared_realm.hpp
23-
impl/weak_realm_notifier.hpp
24-
impl/weak_realm_notifier_base.hpp
29+
impl/collection_change_builder.hpp
30+
impl/collection_notifier.hpp
2531
impl/external_commit_helper.hpp
32+
impl/list_notifier.hpp
33+
impl/realm_coordinator.hpp
34+
impl/results_notifier.hpp
2635
impl/transact_log_handler.hpp
36+
impl/weak_realm_notifier.hpp
37+
impl/weak_realm_notifier_base.hpp
2738
parser/parser.hpp
2839
parser/query_builder.hpp
2940
util/atomic_shared_ptr.hpp)

src/collection_notifications.cpp

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
////////////////////////////////////////////////////////////////////////////
2+
//
3+
// Copyright 2016 Realm Inc.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
//
17+
////////////////////////////////////////////////////////////////////////////
18+
19+
#include "collection_notifications.hpp"
20+
21+
#include "impl/collection_notifier.hpp"
22+
23+
using namespace realm;
24+
using namespace realm::_impl;
25+
26+
NotificationToken::NotificationToken(std::shared_ptr<_impl::CollectionNotifier> notifier, size_t token)
27+
: m_notifier(std::move(notifier)), m_token(token)
28+
{
29+
}
30+
31+
NotificationToken::~NotificationToken()
32+
{
33+
// m_notifier itself (and not just the pointed-to thing) needs to be accessed
34+
// atomically to ensure that there are no data races when the token is
35+
// destroyed after being modified on a different thread.
36+
// This is needed despite the token not being thread-safe in general as
37+
// users find it very surprising for obj-c objects to care about what
38+
// thread they are deallocated on.
39+
if (auto notifier = m_notifier.exchange({})) {
40+
notifier->remove_callback(m_token);
41+
}
42+
}
43+
44+
NotificationToken::NotificationToken(NotificationToken&& rgt) = default;
45+
46+
NotificationToken& NotificationToken::operator=(realm::NotificationToken&& rgt)
47+
{
48+
if (this != &rgt) {
49+
if (auto notifier = m_notifier.exchange({})) {
50+
notifier->remove_callback(m_token);
51+
}
52+
m_notifier = std::move(rgt.m_notifier);
53+
m_token = rgt.m_token;
54+
}
55+
return *this;
56+
}

src/collection_notifications.hpp

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
////////////////////////////////////////////////////////////////////////////
2+
//
3+
// Copyright 2016 Realm Inc.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
//
17+
////////////////////////////////////////////////////////////////////////////
18+
19+
#ifndef REALM_COLLECTION_NOTIFICATIONS_HPP
20+
#define REALM_COLLECTION_NOTIFICATIONS_HPP
21+
22+
#include "index_set.hpp"
23+
#include "util/atomic_shared_ptr.hpp"
24+
25+
#include <exception>
26+
#include <functional>
27+
#include <memory>
28+
#include <vector>
29+
30+
namespace realm {
31+
namespace _impl {
32+
class CollectionNotifier;
33+
}
34+
35+
// A token which keeps an asynchronous query alive
36+
struct NotificationToken {
37+
NotificationToken() = default;
38+
NotificationToken(std::shared_ptr<_impl::CollectionNotifier> notifier, size_t token);
39+
~NotificationToken();
40+
41+
NotificationToken(NotificationToken&&);
42+
NotificationToken& operator=(NotificationToken&&);
43+
44+
NotificationToken(NotificationToken const&) = delete;
45+
NotificationToken& operator=(NotificationToken const&) = delete;
46+
47+
private:
48+
util::AtomicSharedPtr<_impl::CollectionNotifier> m_notifier;
49+
size_t m_token;
50+
};
51+
52+
struct CollectionChangeSet {
53+
struct Move {
54+
size_t from;
55+
size_t to;
56+
57+
bool operator==(Move m) const { return from == m.from && to == m.to; }
58+
};
59+
60+
IndexSet deletions;
61+
IndexSet insertions;
62+
IndexSet modifications;
63+
std::vector<Move> moves;
64+
65+
bool empty() const { return deletions.empty() && insertions.empty() && modifications.empty() && moves.empty(); }
66+
};
67+
68+
using CollectionChangeCallback = std::function<void (CollectionChangeSet, std::exception_ptr)>;
69+
} // namespace realm
70+
71+
#endif // REALM_COLLECTION_NOTIFICATIONS_HPP

0 commit comments

Comments
 (0)