Skip to content

Commit 6a3b2a9

Browse files
committed
use co_await instead of completed
1 parent 0f8571b commit 6a3b2a9

File tree

1 file changed

+23
-179
lines changed

1 file changed

+23
-179
lines changed

windows/ReactNativeBlobUtil/ReactNativeBlobUtil.cpp

Lines changed: 23 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -978,50 +978,16 @@ winrt::fire_and_forget ReactNativeBlobUtil::ls(
978978
winrt::hstring directoryPath, fileName;
979979
splitPath(path, directoryPath, fileName);
980980

981-
Windows::Storage::StorageFolder::GetFolderFromPathAsync(directoryPath).Completed(
982-
[=, promise = std::move(promise)](auto folderOp, auto status)
983-
{
984-
if (status != Windows::Foundation::AsyncStatus::Completed)
985-
{
986-
promise.Reject(winrt::Microsoft::ReactNative::ReactError{ "ENOTDIR", "Failed to open directory: " + path });
987-
return;
988-
}
981+
auto folder = co_await Windows::Storage::StorageFolder::GetFolderFromPathAsync(directoryPath);
982+
auto items = co_await folder.GetItemsAsync();
989983

990-
try
991-
{
992-
auto folder = folderOp.GetResults();
993-
994-
folder.GetItemsAsync().Completed(
995-
[=, promise = std::move(promise)](auto itemsOp, auto status2)
996-
{
997-
if (status2 != Windows::Foundation::AsyncStatus::Completed)
998-
{
999-
promise.Reject(winrt::Microsoft::ReactNative::ReactError{ "ENOTDIR", "Failed to read directory items." });
1000-
return;
1001-
}
1002-
1003-
try
1004-
{
1005-
auto items = itemsOp.GetResults();
1006-
::React::JSValueArray results;
1007-
for (const auto& item : items)
1008-
{
1009-
results.push_back(::React::JSValue{ winrt::to_string(item.Name()) });
1010-
}
1011-
1012-
promise.Resolve(results);
1013-
}
1014-
catch (const winrt::hresult_error& ex)
1015-
{
1016-
promise.Reject(winrt::Microsoft::ReactNative::ReactError{ "EUNSPECIFIED", winrt::to_string(ex.message()) });
1017-
}
1018-
});
1019-
}
1020-
catch (const winrt::hresult_error& ex)
1021-
{
1022-
promise.Reject(winrt::Microsoft::ReactNative::ReactError{ "EUNSPECIFIED", winrt::to_string(ex.message()) });
1023-
}
1024-
});
984+
::React::JSValueArray results;
985+
for (const auto& item : items)
986+
{
987+
results.push_back(::React::JSValue{ winrt::to_string(item.Name()) });
988+
}
989+
990+
promise.Resolve(results);
1025991
}
1026992
catch (const winrt::hresult_error& ex)
1027993
{
@@ -1131,67 +1097,14 @@ winrt::fire_and_forget ReactNativeBlobUtil::cp(
11311097
winrt::hstring destDirectoryPath, destFileName;
11321098
splitPath(dest, destDirectoryPath, destFileName);
11331099

1134-
StorageFolder::GetFolderFromPathAsync(srcDirectoryPath).Completed(
1135-
[=](auto srcFolderOp, auto status) {
1136-
if (status != Windows::Foundation::AsyncStatus::Completed) {
1137-
::React::JSValueArray error{ "Source directory not found." };
1138-
callback(std::move(error));
1139-
return;
1140-
}
1100+
StorageFolder srcFolder = co_await StorageFolder::GetFolderFromPathAsync(srcDirectoryPath);
1101+
StorageFolder destFolder = co_await StorageFolder::GetFolderFromPathAsync(destDirectoryPath);
11411102

1142-
try {
1143-
StorageFolder srcFolder = srcFolderOp.GetResults();
1144-
1145-
StorageFolder::GetFolderFromPathAsync(destDirectoryPath).Completed(
1146-
[=](auto destFolderOp, auto status2) {
1147-
if (status2 != Windows::Foundation::AsyncStatus::Completed) {
1148-
::React::JSValueArray error{ "Destination directory not found." };
1149-
callback(std::move(error));
1150-
return;
1151-
}
1152-
1153-
try {
1154-
StorageFolder destFolder = destFolderOp.GetResults();
1155-
1156-
srcFolder.GetFileAsync(srcFileName).Completed(
1157-
[=](auto fileOp, auto status3) {
1158-
if (status3 != Windows::Foundation::AsyncStatus::Completed) {
1159-
::React::JSValueArray error{ "Source file not found." };
1160-
callback(std::move(error));
1161-
return;
1162-
}
1163-
1164-
try {
1165-
StorageFile file = fileOp.GetResults();
1166-
1167-
file.CopyAsync(destFolder, destFileName, NameCollisionOption::FailIfExists).Completed(
1168-
[=](auto copyOp, auto status4) {
1169-
if (status4 != Windows::Foundation::AsyncStatus::Completed) {
1170-
::React::JSValueArray error{ "Failed to copy file." };
1171-
callback(std::move(error));
1172-
} else {
1173-
::React::JSValueArray success{}; // success, empty array
1174-
callback(std::move(success));
1175-
}
1176-
});
1177-
}
1178-
catch (const winrt::hresult_error& ex) {
1179-
::React::JSValueArray error{ winrt::to_string(ex.message()) };
1180-
callback(std::move(error));
1181-
}
1182-
});
1183-
}
1184-
catch (const winrt::hresult_error& ex) {
1185-
::React::JSValueArray error{ winrt::to_string(ex.message()) };
1186-
callback(std::move(error));
1187-
}
1188-
});
1189-
}
1190-
catch (const winrt::hresult_error& ex) {
1191-
::React::JSValueArray error{ winrt::to_string(ex.message()) };
1192-
callback(std::move(error));
1193-
}
1194-
});
1103+
StorageFile file = co_await srcFolder.GetFileAsync(srcFileName);
1104+
co_await file.CopyAsync(destFolder, destFileName, NameCollisionOption::FailIfExists);
1105+
1106+
::React::JSValueArray success{};
1107+
callback(std::move(success));
11951108
}
11961109
catch (const winrt::hresult_error& ex)
11971110
{
@@ -1213,83 +1126,14 @@ winrt::fire_and_forget ReactNativeBlobUtil::mv(
12131126
winrt::hstring destDirectoryPath, destFileName;
12141127
splitPath(dest, destDirectoryPath, destFileName);
12151128

1216-
StorageFolder::GetFolderFromPathAsync(srcDirectoryPath).Completed(
1217-
[=](auto srcFolderOp, auto status)
1218-
{
1219-
if (status != Windows::Foundation::AsyncStatus::Completed)
1220-
{
1221-
::React::JSValueArray error{ "Source directory not found." };
1222-
callback(std::move(error));
1223-
return;
1224-
}
1129+
StorageFolder srcFolder = co_await StorageFolder::GetFolderFromPathAsync(srcDirectoryPath);
1130+
StorageFolder destFolder = co_await StorageFolder::GetFolderFromPathAsync(destDirectoryPath);
12251131

1226-
try
1227-
{
1228-
StorageFolder srcFolder = srcFolderOp.GetResults();
1229-
1230-
StorageFolder::GetFolderFromPathAsync(destDirectoryPath).Completed(
1231-
[=](auto destFolderOp, auto status2)
1232-
{
1233-
if (status2 != Windows::Foundation::AsyncStatus::Completed)
1234-
{
1235-
::React::JSValueArray error{ "Destination directory not found." };
1236-
callback(std::move(error));
1237-
return;
1238-
}
1239-
1240-
try
1241-
{
1242-
StorageFolder destFolder = destFolderOp.GetResults();
1243-
1244-
srcFolder.GetFileAsync(srcFileName).Completed(
1245-
[=](auto fileOp, auto status3)
1246-
{
1247-
if (status3 != Windows::Foundation::AsyncStatus::Completed)
1248-
{
1249-
::React::JSValueArray error{ "Source file not found." };
1250-
callback(std::move(error));
1251-
return;
1252-
}
1253-
1254-
try
1255-
{
1256-
StorageFile file = fileOp.GetResults();
1257-
1258-
file.MoveAsync(destFolder, destFileName, NameCollisionOption::ReplaceExisting).Completed(
1259-
[=](auto moveOp, auto status4)
1260-
{
1261-
if (status4 != Windows::Foundation::AsyncStatus::Completed)
1262-
{
1263-
::React::JSValueArray error{ "Failed to move file." };
1264-
callback(std::move(error));
1265-
}
1266-
else
1267-
{
1268-
::React::JSValueArray success{ }; // Empty array implies success
1269-
callback(std::move(success));
1270-
}
1271-
});
1272-
}
1273-
catch (const winrt::hresult_error& ex)
1274-
{
1275-
::React::JSValueArray error{ winrt::to_string(ex.message()) };
1276-
callback(std::move(error));
1277-
}
1278-
});
1279-
}
1280-
catch (const winrt::hresult_error& ex)
1281-
{
1282-
::React::JSValueArray error{ winrt::to_string(ex.message()) };
1283-
callback(std::move(error));
1284-
}
1285-
});
1286-
}
1287-
catch (const winrt::hresult_error& ex)
1288-
{
1289-
::React::JSValueArray error{ winrt::to_string(ex.message()) };
1290-
callback(std::move(error));
1291-
}
1292-
});
1132+
StorageFile file = co_await srcFolder.GetFileAsync(srcFileName);
1133+
co_await file.MoveAsync(destFolder, destFileName, NameCollisionOption::ReplaceExisting);
1134+
1135+
::React::JSValueArray success{}; // Success: empty array
1136+
callback(std::move(success));
12931137
}
12941138
catch (const winrt::hresult_error& ex)
12951139
{

0 commit comments

Comments
 (0)