diff --git a/Telegram/SourceFiles/core/observer.h b/Telegram/SourceFiles/core/observer.h index 97665a294..3272d9466 100644 --- a/Telegram/SourceFiles/core/observer.h +++ b/Telegram/SourceFiles/core/observer.h @@ -271,10 +271,10 @@ using SubscriptionHandler = typename SubscriptionHandlerHelper::type; class BaseObservableData { }; -template +template class CommonObservableData; -template +template class ObservableData; } // namespace internal @@ -317,43 +317,41 @@ private: Node *_node = nullptr; RemoveMethod _removeMethod; - template + template friend class internal::CommonObservableData; - template + template friend class internal::ObservableData; }; -template +template class Observable; namespace internal { -template +template class CommonObservable { public: - using Handler = typename CommonObservableData::Handler; - Subscription subscribe(Handler &&handler) { if (!_data) { - _data = MakeShared>(this); + _data = MakeShared>(this); } return _data->append(std_::forward(handler)); } private: - QSharedPointer> _data; + QSharedPointer> _data; - friend class CommonObservableData; - friend class Observable; + friend class CommonObservableData; + friend class Observable; }; } // namespace internal -template -class Observable : public internal::CommonObservable { +template > +class Observable : public internal::CommonObservable { public: void notify(EventType &&event, bool sync = false) { if (this->_data) { @@ -365,12 +363,10 @@ public: namespace internal { -template +template class CommonObservableData : public BaseObservableData { public: - using Handler = SubscriptionHandler; - - CommonObservableData(CommonObservable *observable) : _observable(observable) { + CommonObservableData(CommonObservable *observable) : _observable(observable) { } Subscription append(Handler &&handler) { @@ -441,20 +437,20 @@ private: } } - CommonObservable *_observable = nullptr; + CommonObservable *_observable = nullptr; Node *_begin = nullptr; Node *_current = nullptr; Node *_end = nullptr; ObservableCallHandlers _callHandlers; - friend class ObservableData; + friend class ObservableData; }; -template -class ObservableData : public CommonObservableData { +template +class ObservableData : public CommonObservableData { public: - using CommonObservableData::CommonObservableData; + using CommonObservableData::CommonObservableData; void notify(EventType &&event, bool sync) { if (_handling) { @@ -498,10 +494,10 @@ private: }; -template <> -class ObservableData : public CommonObservableData { +template +class ObservableData : public CommonObservableData { public: - using CommonObservableData::CommonObservableData; + using CommonObservableData::CommonObservableData; void notify(bool sync) { if (_handling) { @@ -547,8 +543,8 @@ private: } // namespace internal -template <> -class Observable : public internal::CommonObservable { +template +class Observable : public internal::CommonObservable { public: void notify(bool sync = false) { if (_data) { @@ -560,14 +556,14 @@ public: class Subscriber { protected: - template - int subscribe(base::Observable &observable, Lambda &&handler) { + template + int subscribe(base::Observable &observable, Lambda &&handler) { _subscriptions.push_back(observable.subscribe(std_::forward(handler))); return _subscriptions.size() - 1; } - template - int subscribe(base::Observable *observable, Lambda &&handler) { + template + int subscribe(base::Observable *observable, Lambda &&handler) { return subscribe(*observable, std_::forward(handler)); } diff --git a/Telegram/SourceFiles/facades.h b/Telegram/SourceFiles/facades.h index cbe01c49a..9c3e21f15 100644 --- a/Telegram/SourceFiles/facades.h +++ b/Telegram/SourceFiles/facades.h @@ -22,7 +22,7 @@ Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org class LayerWidget; namespace base { -template +template class Observable; } // namespace base namespace InlineBots { diff --git a/Telegram/SourceFiles/observer_peer.cpp b/Telegram/SourceFiles/observer_peer.cpp index 13bfe074a..8347b8973 100644 --- a/Telegram/SourceFiles/observer_peer.cpp +++ b/Telegram/SourceFiles/observer_peer.cpp @@ -48,6 +48,8 @@ void FinishCallback() { } ObservedEventRegistrator creator(StartCallback, FinishCallback); +base::Observable PeerUpdatedObservable; + } // namespace namespace internal { @@ -116,10 +118,20 @@ void peerUpdatedSendDelayed() { for_const (auto &update, allList) { creator.notify(update.flags, update); } + for (auto &update : smallList) { + PeerUpdated().notify(std_::move(update), true); + } + for (auto &update : allList) { + PeerUpdated().notify(std_::move(update), true); + } if (SmallUpdates->isEmpty()) { std::swap(smallList, *SmallUpdates); SmallUpdates->resize(0); } } +base::Observable &PeerUpdated() { + return PeerUpdatedObservable; +} + } // namespace Notify diff --git a/Telegram/SourceFiles/observer_peer.h b/Telegram/SourceFiles/observer_peer.h index 53955b68c..d56299c9d 100644 --- a/Telegram/SourceFiles/observer_peer.h +++ b/Telegram/SourceFiles/observer_peer.h @@ -102,4 +102,22 @@ void registerPeerObserver(PeerUpdate::Flags events, ObserverType *observer, Lamb observerRegistered(observer, connection); } +class PeerUpdatedHandler { +public: + template + PeerUpdatedHandler(PeerUpdate::Flags events, Lambda &&handler) : _events(events), _handler(std_::move(handler)) { + } + void operator()(const PeerUpdate &update) const { + if (update.flags & _events) { + _handler(update); + } + } + +private: + PeerUpdate::Flags _events; + base::lambda_unique _handler; + +}; +base::Observable &PeerUpdated(); + } // namespace Notify diff --git a/Telegram/SourceFiles/settings/settings_info_widget.cpp b/Telegram/SourceFiles/settings/settings_info_widget.cpp index e6b83ca0a..a814317d9 100644 --- a/Telegram/SourceFiles/settings/settings_info_widget.cpp +++ b/Telegram/SourceFiles/settings/settings_info_widget.cpp @@ -34,9 +34,9 @@ using UpdateFlag = Notify::PeerUpdate::Flag; InfoWidget::InfoWidget(QWidget *parent, UserData *self) : BlockWidget(parent, self, lang(lng_settings_section_info)) { auto observeEvents = UpdateFlag::UsernameChanged | UpdateFlag::UserPhoneChanged; - Notify::registerPeerObserver(observeEvents, this, [this](const Notify::PeerUpdate &update) { + subscribe(Notify::PeerUpdated(), Notify::PeerUpdatedHandler(observeEvents, [this](const Notify::PeerUpdate &update) { notifyPeerUpdated(update); - }); + })); createControls(); }