mirror of
https://github.com/Microsoft/calculator.git
synced 2025-08-22 14:13:30 -07:00
Migrate from Windows concurrency to std::future and std::async in UnitConverter
This commit is contained in:
parent
57b6820738
commit
e484b3cf0b
3 changed files with 22 additions and 18 deletions
|
@ -5,7 +5,6 @@
|
|||
#include "Command.h"
|
||||
#include "UnitConverter.h"
|
||||
|
||||
using namespace concurrency;
|
||||
using namespace std;
|
||||
using namespace UnitConversionManager;
|
||||
|
||||
|
@ -662,21 +661,25 @@ void UnitConverter::SetViewModelCurrencyCallback(_In_ const shared_ptr<IViewMode
|
|||
}
|
||||
}
|
||||
|
||||
task<pair<bool, wstring>> UnitConverter::RefreshCurrencyRatios()
|
||||
future<pair<bool, wstring>> UnitConverter::RefreshCurrencyRatios()
|
||||
{
|
||||
shared_ptr<ICurrencyConverterDataLoader> currencyDataLoader = GetCurrencyConverterDataLoader();
|
||||
return create_task([this, currencyDataLoader]()
|
||||
future<bool> loadDataResult;
|
||||
|
||||
if (currencyDataLoader != nullptr)
|
||||
{
|
||||
if (currencyDataLoader != nullptr)
|
||||
{
|
||||
return currencyDataLoader->TryLoadDataFromWebOverrideAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
return task_from_result(false);
|
||||
}
|
||||
}).then([this, currencyDataLoader](bool didLoad)
|
||||
loadDataResult = currencyDataLoader->TryLoadDataFromWebOverrideAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
loadDataResult = async([] { return false; });
|
||||
}
|
||||
|
||||
shared_future<bool> sharedLoadResult = loadDataResult.share();
|
||||
return async([this, currencyDataLoader, sharedLoadResult]()
|
||||
{
|
||||
sharedLoadResult.wait();
|
||||
bool didLoad = sharedLoadResult.get();
|
||||
wstring timestamp = L"";
|
||||
if (currencyDataLoader != nullptr)
|
||||
{
|
||||
|
@ -684,7 +687,7 @@ task<pair<bool, wstring>> UnitConverter::RefreshCurrencyRatios()
|
|||
}
|
||||
|
||||
return make_pair(didLoad, timestamp);
|
||||
}, task_continuation_context::use_default());
|
||||
});
|
||||
}
|
||||
|
||||
shared_ptr<ICurrencyConverterDataLoader> UnitConverter::GetCurrencyConverterDataLoader()
|
||||
|
|
|
@ -163,9 +163,9 @@ namespace UnitConversionManager
|
|||
virtual std::pair<std::wstring, std::wstring> GetCurrencyRatioEquality(_In_ const UnitConversionManager::Unit& unit1, _In_ const UnitConversionManager::Unit& unit2) = 0;
|
||||
virtual std::wstring GetCurrencyTimestamp() = 0;
|
||||
|
||||
virtual concurrency::task<bool> TryLoadDataFromCacheAsync() = 0;
|
||||
virtual concurrency::task<bool> TryLoadDataFromWebAsync() = 0;
|
||||
virtual concurrency::task<bool> TryLoadDataFromWebOverrideAsync() = 0;
|
||||
virtual std::future<bool> TryLoadDataFromCacheAsync() = 0;
|
||||
virtual std::future<bool> TryLoadDataFromWebAsync() = 0;
|
||||
virtual std::future<bool> TryLoadDataFromWebOverrideAsync() = 0;
|
||||
};
|
||||
|
||||
class IUnitConverterVMCallback
|
||||
|
@ -194,7 +194,7 @@ namespace UnitConversionManager
|
|||
virtual void SendCommand(Command command) = 0;
|
||||
virtual void SetViewModelCallback(_In_ const std::shared_ptr<IUnitConverterVMCallback>& newCallback) = 0;
|
||||
virtual void SetViewModelCurrencyCallback(_In_ const std::shared_ptr<IViewModelCurrencyCallback>& newCallback) = 0;
|
||||
virtual concurrency::task<std::pair<bool, std::wstring>> RefreshCurrencyRatios() = 0;
|
||||
virtual std::future<std::pair<bool, std::wstring>> RefreshCurrencyRatios() = 0;
|
||||
};
|
||||
|
||||
class UnitConverter : public IUnitConverter, public std::enable_shared_from_this<UnitConverter>
|
||||
|
@ -217,7 +217,7 @@ namespace UnitConversionManager
|
|||
void SendCommand(Command command) override;
|
||||
void SetViewModelCallback(_In_ const std::shared_ptr<IUnitConverterVMCallback>& newCallback) override;
|
||||
void SetViewModelCurrencyCallback(_In_ const std::shared_ptr<IViewModelCurrencyCallback>& newCallback) override;
|
||||
concurrency::task<std::pair<bool, std::wstring>> RefreshCurrencyRatios() override;
|
||||
std::future<std::pair<bool, std::wstring>> RefreshCurrencyRatios() override;
|
||||
// IUnitConverter
|
||||
|
||||
static std::vector<std::wstring> StringToVector(const std::wstring& w, const wchar_t * delimiter, bool addRemainder = false);
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
#include <unordered_map>
|
||||
#include <array>
|
||||
#include <string_view>
|
||||
#include <future>
|
||||
|
||||
#include "win_data_types_cross_platform.h"
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue