Revert "refactor(updating): replace std::future with IAsyncOperation for async methods"

This reverts commit 595fef617b.
This commit is contained in:
Gordon Lam (SH)
2026-02-05 15:13:53 -08:00
parent 45f3e1debb
commit a16ff575cc
9 changed files with 41 additions and 35 deletions

View File

@@ -57,7 +57,7 @@ std::optional<fs::path> ObtainInstaller(bool& isUpToDate)
auto state = UpdateState::read();
const auto new_version_info = get_github_version_info_async();
const auto new_version_info = get_github_version_info_async().get();
if (std::holds_alternative<version_up_to_date>(*new_version_info))
{
isUpToDate = true;
@@ -76,7 +76,7 @@ std::optional<fs::path> ObtainInstaller(bool& isUpToDate)
// Cleanup old updates before downloading the latest
updating::cleanup_updates();
auto downloaded_installer = download_new_version(std::get<new_version_download_info>(*new_version_info));
auto downloaded_installer = download_new_version(std::get<new_version_download_info>(*new_version_info)).get();
if (!downloaded_installer)
{
Logger::error("Couldn't download new installer");

View File

@@ -18,7 +18,7 @@ namespace // Strings in this namespace should not be localized
namespace updating
{
winrt::Windows::Foundation::IAsyncOperation<bool> uninstall_previous_msix_version_async()
std::future<bool> uninstall_previous_msix_version_async()
{
winrt::Windows::Management::Deployment::PackageManager package_manager;

View File

@@ -2,11 +2,11 @@
#include <string>
#include <optional>
#include <future>
#include <winrt/Windows.Foundation.h>
#include <common/version/helper.h>
namespace updating
{
winrt::Windows::Foundation::IAsyncOperation<bool> uninstall_previous_msix_version_async();
std::future<bool> uninstall_previous_msix_version_async();
}

View File

@@ -83,18 +83,18 @@ namespace updating
#pragma warning(push)
#pragma warning(disable : 4702)
#if USE_STD_EXPECTED
std::expected<github_version_info, std::wstring> get_github_version_info_async(const bool prerelease)
std::future<std::expected<github_version_info, std::wstring>> get_github_version_info_async(const bool prerelease)
#else
nonstd::expected<github_version_info, std::wstring> get_github_version_info_async(const bool prerelease)
std::future<nonstd::expected<github_version_info, std::wstring>> get_github_version_info_async(const bool prerelease)
#endif
{
// If the current version starts with 0.0.*, it means we're on a local build from a farm and shouldn't check for updates.
if constexpr (VERSION_MAJOR == 0 && VERSION_MINOR == 0)
{
#if USE_STD_EXPECTED
return std::unexpected(LOCAL_BUILD_ERROR);
co_return std::unexpected(LOCAL_BUILD_ERROR);
#else
return nonstd::make_unexpected(LOCAL_BUILD_ERROR);
co_return nonstd::make_unexpected(LOCAL_BUILD_ERROR);
#endif
}
@@ -107,7 +107,7 @@ namespace updating
if (prerelease)
{
const auto body = client.request(Uri{ ALL_RELEASES_ENDPOINT }).get();
const auto body = co_await client.request(Uri{ ALL_RELEASES_ENDPOINT });
for (const auto& json : json::JsonValue::Parse(body).GetArray())
{
auto potential_release_object = json.GetObjectW();
@@ -125,7 +125,7 @@ namespace updating
}
else
{
const auto body = client.request(Uri{ LATEST_RELEASE_ENDPOINT }).get();
const auto body = co_await client.request(Uri{ LATEST_RELEASE_ENDPOINT });
release_object = json::JsonValue::Parse(body).GetObjectW();
if (auto extracted_version = extract_version_from_release_object(release_object))
{
@@ -135,11 +135,11 @@ namespace updating
if (github_version <= current_version)
{
return version_up_to_date{};
co_return version_up_to_date{};
}
auto [installer_download_url, installer_filename] = extract_installer_asset_download_info(release_object);
return new_version_download_info{ extract_release_page_url(release_object),
co_return new_version_download_info{ extract_release_page_url(release_object),
std::move(github_version),
std::move(installer_download_url),
std::move(installer_filename) };
@@ -148,9 +148,9 @@ namespace updating
{
}
#if USE_STD_EXPECTED
return std::unexpected(NETWORK_ERROR);
co_return std::unexpected(NETWORK_ERROR);
#else
return nonstd::make_unexpected(NETWORK_ERROR);
co_return nonstd::make_unexpected(NETWORK_ERROR);
#endif
}
#pragma warning(pop)
@@ -170,12 +170,12 @@ namespace updating
return !ec ? std::optional{ installer_download_path } : std::nullopt;
}
std::optional<std::filesystem::path> download_new_version(const new_version_download_info& new_version)
std::future<std::optional<std::filesystem::path>> download_new_version(const new_version_download_info& new_version)
{
auto installer_download_path = create_download_path();
if (!installer_download_path)
{
return std::nullopt;
co_return std::nullopt;
}
*installer_download_path /= new_version.installer_filename;
@@ -186,7 +186,7 @@ namespace updating
try
{
http::HttpClient client;
client.download(new_version.installer_download_url, *installer_download_path).get();
co_await client.download(new_version.installer_download_url, *installer_download_path);
download_success = true;
break;
}
@@ -195,7 +195,7 @@ namespace updating
// reattempt to download or do nothing
}
}
return download_success ? installer_download_path : std::nullopt;
co_return download_success ? installer_download_path : std::nullopt;
}
void cleanup_updates()

View File

@@ -2,6 +2,7 @@
#include <optional>
#include <string>
#include <future>
#include <filesystem>
#include <variant>
#include <winrt/Windows.Foundation.h>
@@ -31,12 +32,12 @@ namespace updating
};
using github_version_info = std::variant<new_version_download_info, version_up_to_date>;
std::optional<std::filesystem::path> download_new_version(const new_version_download_info& new_version);
std::future<std::optional<std::filesystem::path>> download_new_version(const new_version_download_info& new_version);
std::filesystem::path get_pending_updates_path();
#if USE_STD_EXPECTED
std::expected<github_version_info, std::wstring> get_github_version_info_async(const bool prerelease = false);
std::future<std::expected<github_version_info, std::wstring>> get_github_version_info_async(const bool prerelease = false);
#else
nonstd::expected<github_version_info, std::wstring> get_github_version_info_async(const bool prerelease = false);
std::future<nonstd::expected<github_version_info, std::wstring>> get_github_version_info_async(const bool prerelease = false);
#endif
void cleanup_updates();

View File

@@ -28,6 +28,8 @@
<ClCompile>
<AdditionalIncludeDirectories>..\;..\..\;$(RepoRoot)src\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<!-- Required for std::future coroutines in HttpClient.h -->
<AdditionalOptions>/await %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Lib>
<AdditionalDependencies>Version.lib</AdditionalDependencies>

View File

@@ -1,6 +1,6 @@
#pragma once
#include <functional>
#include <future>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Storage.Streams.h>
#include <winrt/Windows.Web.Http.h>
@@ -21,15 +21,15 @@ namespace http
headers.UserAgent().TryParseAdd(USER_AGENT);
}
winrt::Windows::Foundation::IAsyncOperation<winrt::hstring> request(const winrt::Windows::Foundation::Uri& url)
std::future<std::wstring> request(const winrt::Windows::Foundation::Uri& url)
{
auto response = co_await m_client.GetAsync(url);
(void)response.EnsureSuccessStatusCode();
auto body = co_await response.Content().ReadAsStringAsync();
co_return body;
co_return std::wstring(body);
}
winrt::Windows::Foundation::IAsyncAction download(const winrt::Windows::Foundation::Uri& url, const std::wstring& dstFilePath)
std::future<void> download(const winrt::Windows::Foundation::Uri& url, const std::wstring& dstFilePath)
{
auto response = co_await m_client.GetAsync(url);
(void)response.EnsureSuccessStatusCode();
@@ -38,7 +38,7 @@ namespace http
file_stream.Close();
}
winrt::Windows::Foundation::IAsyncAction download(const winrt::Windows::Foundation::Uri& url, const std::wstring& dstFilePath, const std::function<void(float)>& progressUpdateCallback)
std::future<void> download(const winrt::Windows::Foundation::Uri& url, const std::wstring& dstFilePath, const std::function<void(float)>& progressUpdateCallback)
{
auto response = co_await m_client.GetAsync(url, HttpCompletionOption::ResponseHeadersRead);
response.EnsureSuccessStatusCode();

View File

@@ -5140,7 +5140,7 @@ bool IsPenInverted( WPARAM wParam )
// Captures the specified screen using the capture APIs
//
//----------------------------------------------------------------------------
winrt::Windows::Foundation::IAsyncAction CaptureScreenshotAsync(winrt::IDirect3DDevice const& device, winrt::GraphicsCaptureItem const& item, winrt::DirectXPixelFormat const& pixelFormat, winrt::com_ptr<ID3D11Texture2D>& result)
std::future<winrt::com_ptr<ID3D11Texture2D>> CaptureScreenshotAsync(winrt::IDirect3DDevice const& device, winrt::GraphicsCaptureItem const& item, winrt::DirectXPixelFormat const& pixelFormat)
{
auto d3dDevice = GetDXGIInterfaceFromObject<ID3D11Device>(device);
winrt::com_ptr<ID3D11DeviceContext> d3dContext;
@@ -5176,7 +5176,9 @@ winrt::Windows::Foundation::IAsyncAction CaptureScreenshotAsync(winrt::IDirect3D
framePool.Close();
auto texture = GetDXGIInterfaceFromObject<ID3D11Texture2D>(frame.Surface());
result = util::CopyD3DTexture(d3dDevice, texture, true);
auto result = util::CopyD3DTexture(d3dDevice, texture, true);
co_return result;
}
//----------------------------------------------------------------------------
@@ -5203,9 +5205,10 @@ winrt::com_ptr<ID3D11Texture2D>CaptureScreenshot(winrt::DirectXPixelFormat const
auto item = util::CreateCaptureItemForMonitor(hMon);
winrt::com_ptr<ID3D11Texture2D> result;
CaptureScreenshotAsync(device, item, pixelFormat, result).get();
return result;
auto capture = CaptureScreenshotAsync(device, item, pixelFormat);
capture.wait();
return capture.get();
}

View File

@@ -173,7 +173,7 @@ void ProcessNewVersionInfo(const github_version_info& version_info,
// Cleanup old updates before downloading the latest
updating::cleanup_updates();
if (download_new_version(new_version_info))
if (download_new_version(new_version_info).get())
{
state.state = UpdateState::readyToInstall;
state.downloadedInstallerFilename = new_version_info.installer_filename;
@@ -232,7 +232,7 @@ void PeriodicUpdateWorker()
bool version_info_obtained = false;
try
{
const auto new_version_info = get_github_version_info_async();
const auto new_version_info = get_github_version_info_async().get();
if (new_version_info.has_value())
{
version_info_obtained = true;
@@ -272,7 +272,7 @@ void CheckForUpdatesCallback()
auto state = UpdateState::read();
try
{
auto new_version_info = get_github_version_info_async();
auto new_version_info = get_github_version_info_async().get();
if (!new_version_info)
{
// We couldn't get a new version from github for some reason, log error