forked from telegramdesktop/tdesktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobserver.cpp
85 lines (70 loc) · 2.01 KB
/
observer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "base/observer.h"
namespace base {
namespace internal {
namespace {
bool CantUseObservables = false;
void (*HandleDelayedMethod)() = nullptr;
struct ObservableListWrap {
~ObservableListWrap() {
CantUseObservables = true;
}
OrderedSet<ObservableCallHandlers*> list;
};
ObservableListWrap &PendingObservables() {
static ObservableListWrap result;
return result;
}
ObservableListWrap &ActiveObservables() {
static ObservableListWrap result;
return result;
}
} // namespace
void RegisterPendingObservable(ObservableCallHandlers *handlers) {
if (CantUseObservables) return;
PendingObservables().list.insert(handlers);
if (HandleDelayedMethod) {
HandleDelayedMethod();
}
}
void UnregisterActiveObservable(ObservableCallHandlers *handlers) {
if (CantUseObservables) return;
ActiveObservables().list.remove(handlers);
}
void UnregisterObservable(ObservableCallHandlers *handlers) {
if (CantUseObservables) return;
PendingObservables().list.remove(handlers);
ActiveObservables().list.remove(handlers);
}
} // namespace internal
void InitObservables(void(*HandleDelayed)()) {
internal::HandleDelayedMethod = HandleDelayed;
}
void HandleObservables() {
if (internal::CantUseObservables) return;
auto &active = internal::ActiveObservables().list;
qSwap(active, internal::PendingObservables().list);
while (!active.empty()) {
auto first = *active.begin();
(*first)();
if (!active.empty() && *active.begin() == first) {
active.erase(active.begin());
}
}
}
rpl::producer<> ObservableViewer(base::Observable<void> &observable) {
return [&observable](const auto &consumer) {
auto lifetime = rpl::lifetime();
lifetime.make_state<base::Subscription>(
observable.add_subscription([consumer]() {
consumer.put_next({});
}));
return lifetime;
};
}
} // namespace base