From c2fa149ffda466f4ff09e15a0c330a80fa981e92 Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Thu, 7 Jun 2018 21:04:51 +0300
Subject: [PATCH] Replace InvokeQueued with on_main for has_weak_ptr.

---
 Telegram/SourceFiles/base/weak_ptr.h         | 21 --------------------
 Telegram/SourceFiles/calls/calls_call.cpp    | 17 +++++++++++-----
 Telegram/SourceFiles/core/update_checker.cpp |  2 +-
 3 files changed, 13 insertions(+), 27 deletions(-)

diff --git a/Telegram/SourceFiles/base/weak_ptr.h b/Telegram/SourceFiles/base/weak_ptr.h
index e4884bc25..6bf381d6a 100644
--- a/Telegram/SourceFiles/base/weak_ptr.h
+++ b/Telegram/SourceFiles/base/weak_ptr.h
@@ -315,24 +315,3 @@ struct guard_traits<
 };
 
 } // namespace crl
-
-#ifdef QT_VERSION
-template <typename Lambda>
-inline void InvokeQueued(const base::has_weak_ptr *context, Lambda &&lambda) {
-	auto callback = [
-		guard = base::make_weak(context),
-		lambda = std::forward<Lambda>(lambda)
-	] {
-		if (guard) {
-			lambda();
-		}
-	};
-	QObject proxy;
-	QObject::connect(
-		&proxy,
-		&QObject::destroyed,
-		QCoreApplication::instance(),
-		std::move(callback),
-		Qt::QueuedConnection);
-}
-#endif // QT_VERSION
diff --git a/Telegram/SourceFiles/calls/calls_call.cpp b/Telegram/SourceFiles/calls/calls_call.cpp
index bf79686aa..642570bdc 100644
--- a/Telegram/SourceFiles/calls/calls_call.cpp
+++ b/Telegram/SourceFiles/calls/calls_call.cpp
@@ -632,7 +632,7 @@ void Call::handleControllerBarCountChange(
 	// Expects(controller == _controller.get());
 	Expects(controller->implData == static_cast<void*>(this));
 
-	InvokeQueued(this, [=] {
+	crl::on_main(this, [=] {
 		setSignalBarCount(count);
 	});
 }
@@ -763,10 +763,17 @@ void Call::finish(FinishType type, const MTPPhoneCallDiscardReason &reason) {
 	auto duration = getDurationMs() / 1000;
 	auto connectionId = _controller ? _controller->GetPreferredRelayID() : 0;
 	_finishByTimeoutTimer.call(kHangupTimeoutMs, [this, finalState] { setState(finalState); });
-	request(MTPphone_DiscardCall(MTP_inputPhoneCall(MTP_long(_id), MTP_long(_accessHash)), MTP_int(duration), reason, MTP_long(connectionId))).done([this, finalState](const MTPUpdates &result) {
+	request(MTPphone_DiscardCall(
+		MTP_inputPhoneCall(
+			MTP_long(_id),
+			MTP_long(_accessHash)),
+		MTP_int(duration),
+		reason,
+		MTP_long(connectionId)
+	)).done([=](const MTPUpdates &result) {
 		// This could be destroyed by updates, so we set Ended after
 		// updates being handled, but in a guarded way.
-		InvokeQueued(this, [this, finalState] { setState(finalState); });
+		crl::on_main(this, [=] { setState(finalState); });
 		App::main()->sentUpdatesReceived(result);
 	}).fail([this, finalState](const RPCError &error) {
 		setState(finalState);
@@ -774,13 +781,13 @@ void Call::finish(FinishType type, const MTPPhoneCallDiscardReason &reason) {
 }
 
 void Call::setStateQueued(State state) {
-	InvokeQueued(this, [=] {
+	crl::on_main(this, [=] {
 		setState(state);
 	});
 }
 
 void Call::setFailedQueued(int error) {
-	InvokeQueued(this, [=] {
+	crl::on_main(this, [=] {
 		handleControllerError(error);
 	});
 }
diff --git a/Telegram/SourceFiles/core/update_checker.cpp b/Telegram/SourceFiles/core/update_checker.cpp
index e60635c06..e8236c472 100644
--- a/Telegram/SourceFiles/core/update_checker.cpp
+++ b/Telegram/SourceFiles/core/update_checker.cpp
@@ -1254,7 +1254,7 @@ MtpChecker::MtpChecker(QPointer<MTP::Instance> instance, bool testing)
 void MtpChecker::start() {
 	if (!_mtp.valid()) {
 		LOG(("Update Info: MTP is unavailable."));
-		InvokeQueued(this, [=] { fail(); });
+		crl::on_main(this, [=] { fail(); });
 		return;
 	}
 	constexpr auto kFeedUsername = "tdhbcfeed";