@@ -251,9 +251,9 @@ namespace winrt::ReactNativeBlobUtil
251
251
std::string contentPath = data.substr (strlen (" file://" ));
252
252
winrt::hstring directoryPath, fileName;
253
253
splitPath (contentPath, directoryPath, fileName);
254
- auto folder = winrt::Windows::Storage::StorageFolder::GetFolderFromPathAsync (directoryPath). get ( );
255
- auto storageFile = folder.GetFileAsync (fileName). get ( );
256
- auto requestBuffer = winrt::Windows::Storage::FileIO::ReadBufferAsync (storageFile). get ( );
254
+ auto folder = co_await winrt::Windows::Storage::StorageFolder::GetFolderFromPathAsync (directoryPath);
255
+ auto storageFile = co_await folder.GetFileAsync (fileName);
256
+ auto requestBuffer = co_await winrt::Windows::Storage::FileIO::ReadBufferAsync (storageFile);
257
257
258
258
winrt::Windows::Web::Http::HttpBufferContent requestBufferContent{ requestBuffer };
259
259
if (!items[" type" ].IsNull ())
@@ -306,12 +306,12 @@ namespace winrt::ReactNativeBlobUtil
306
306
requestMessage.Content (requestContent);
307
307
308
308
winrt::Windows::Web::Http::HttpClient httpClient{ filter };
309
- auto response = httpClient.SendRequestAsync (requestMessage). get ( );
309
+ auto response = co_await httpClient.SendRequestAsync (requestMessage);
310
310
311
311
std::string responseBody;
312
312
if (response.Content () != nullptr )
313
313
{
314
- responseBody = winrt::to_string (response.Content ().ReadAsStringAsync (). get ());
314
+ responseBody = winrt::to_string (co_await response.Content ().ReadAsStringAsync ());
315
315
}
316
316
317
317
::React::JSValueArray resultArray;
@@ -395,9 +395,9 @@ namespace winrt::ReactNativeBlobUtil
395
395
bool hasTrailingSlash = contentPath[fileLength - 1 ] == ' \\ ' || contentPath[fileLength - 1 ] == ' /' ;
396
396
winrt::hstring directoryPath, fileName;
397
397
splitPath (hasTrailingSlash ? contentPath.substr (0 , fileLength - 1 ) : contentPath, directoryPath, fileName);
398
- auto folder = winrt::Windows::Storage::StorageFolder::GetFolderFromPathAsync (directoryPath). get ( );
399
- auto storageFile = folder.GetFileAsync (fileName). get ( );
400
- auto requestBuffer = winrt::Windows::Storage::FileIO::ReadBufferAsync (storageFile). get ( );
398
+ auto folder = co_await winrt::Windows::Storage::StorageFolder::GetFolderFromPathAsync (directoryPath);
399
+ auto storageFile = co_await folder.GetFileAsync (fileName);
400
+ auto requestBuffer = co_await winrt::Windows::Storage::FileIO::ReadBufferAsync (storageFile);
401
401
402
402
winrt::Windows::Web::Http::HttpBufferContent requestContent{ requestBuffer };
403
403
@@ -430,12 +430,12 @@ namespace winrt::ReactNativeBlobUtil
430
430
}
431
431
432
432
// Send the request
433
- auto response = httpClient.SendRequestAsync (requestMessage). get ( );
433
+ auto response = co_await httpClient.SendRequestAsync (requestMessage);
434
434
435
435
std::string responseBody;
436
436
if (response.Content () != nullptr )
437
437
{
438
- responseBody = winrt::to_string (response.Content ().ReadAsStringAsync (). get ());
438
+ responseBody = winrt::to_string (co_await response.Content ().ReadAsStringAsync ());
439
439
}
440
440
441
441
::React::JSValueArray resultArray;
@@ -728,9 +728,9 @@ winrt::fire_and_forget ReactNativeBlobUtil::writeStream(
728
728
{
729
729
winrt::hstring directoryPath, fileName;
730
730
splitPath (path, directoryPath, fileName);
731
- auto folder = StorageFolder::GetFolderFromPathAsync (directoryPath). get ( );
732
- auto file = folder.CreateFileAsync (fileName, CreationCollisionOption::OpenIfExists). get ( );
733
- auto stream = file.OpenAsync (FileAccessMode::ReadWrite). get ( );
731
+ auto folder = co_await StorageFolder::GetFolderFromPathAsync (directoryPath);
732
+ auto file = co_await folder.CreateFileAsync (fileName, CreationCollisionOption::OpenIfExists);
733
+ auto stream = co_await file.OpenAsync (FileAccessMode::ReadWrite);
734
734
if (appendData)
735
735
{
736
736
stream.Seek (stream.Size ());
@@ -901,25 +901,23 @@ winrt::fire_and_forget ReactNativeBlobUtil::unlink(
901
901
std::string path,
902
902
std::function<void (::React::JSValueArray)> callback) noexcept
903
903
{
904
- winrt::Windows::System::Threading::ThreadPool::RunAsync ([this , path, callback](auto &&)
905
- {
906
- try
904
+ try
907
905
{
908
906
if (std::filesystem::is_directory (path))
909
907
{
910
908
std::filesystem::path unlinkPath (path);
911
909
unlinkPath.make_preferred ();
912
910
auto folderOp = winrt::Windows::Storage::StorageFolder::GetFolderFromPathAsync (
913
911
winrt::to_hstring (unlinkPath.c_str ()));
914
- folderOp.get ().DeleteAsync (). get ();
912
+ co_await folderOp.get ().DeleteAsync ();
915
913
}
916
914
else
917
915
{
918
916
winrt::hstring directoryPath, fileName;
919
917
splitPath (path, directoryPath, fileName);
920
- auto folder = winrt::Windows::Storage::StorageFolder::GetFolderFromPathAsync (directoryPath). get ( );
921
- auto item = folder.GetItemAsync (fileName). get ( );
922
- item.DeleteAsync (). get ();
918
+ auto folder = co_await winrt::Windows::Storage::StorageFolder::GetFolderFromPathAsync (directoryPath);
919
+ auto item = co_await folder.GetItemAsync (fileName);
920
+ co_await item.DeleteAsync ();
923
921
}
924
922
925
923
::React::JSValueArray result;
@@ -933,36 +931,42 @@ winrt::fire_and_forget ReactNativeBlobUtil::unlink(
933
931
errorResult.push_back (::React::JSValue (winrt::to_string (ex.message ())));
934
932
callback (std::move (errorResult));
935
933
}
936
- });
934
+
937
935
}
938
936
939
- void ReactNativeBlobUtil::removeSession (
940
- ::React::JSValueArray&& paths,
941
- std::function<void (::React::JSValueArray const &)> const & callback) noexcept
937
+ winrt::fire_and_forget ReactNativeBlobUtil::removeSession (::React::JSValueArray paths, std::function<void (::React::JSValueArray)> callback) noexcept
942
938
{
943
- winrt::Windows::System::Threading::ThreadPool::RunAsync ([paths = std::move (paths), callback]( auto &&)
939
+ try
944
940
{
945
- ::React::JSValueArray resultArray;
946
- try
941
+ for (const auto & pathValue : paths)
947
942
{
948
- for ( const auto & path : paths )
943
+ if (pathValue )
949
944
{
950
- std::filesystem::path toDelete{ path.AsString () };
951
- toDelete.make_preferred ();
952
- auto fileOp = winrt::Windows::Storage::StorageFile::GetFileFromPathAsync (winrt::to_hstring (toDelete.c_str ()));
953
- auto file = fileOp.get ();
954
- file.DeleteAsync ().get ();
945
+ std::string path = pathValue.AsString ();
946
+ auto folder = co_await winrt::Windows::Storage::StorageFolder::GetFolderFromPathAsync (winrt::to_hstring (path));
947
+ auto file = co_await folder.GetFileAsync (winrt::to_hstring (path));
948
+ co_await file.DeleteAsync ();
955
949
}
956
- // Success: return empty array
957
- callback (resultArray);
958
- }
959
- catch (const winrt::hresult_error& ex)
960
- {
961
- resultArray.push_back (" EUNSPECIFIED" );
962
- resultArray.push_back (winrt::to_string (ex.message ()));
963
- callback (resultArray);
964
950
}
965
- });
951
+
952
+ ::React::JSValueArray resultArray;
953
+ resultArray.push_back (" SUCCESS" );
954
+ callback (std::move (resultArray));
955
+ }
956
+ catch (const winrt::hresult_error& ex)
957
+ {
958
+ ::React::JSValueArray errorArray;
959
+ errorArray.push_back (" ERROR" );
960
+ errorArray.push_back (winrt::to_string (ex.message ()));
961
+ callback (std::move (errorArray));
962
+ }
963
+ catch (...)
964
+ {
965
+ ::React::JSValueArray errorArray;
966
+ errorArray.push_back (" ERROR" );
967
+ errorArray.push_back (" Unknown error in removeSession" );
968
+ callback (std::move (errorArray));
969
+ }
966
970
}
967
971
968
972
winrt::fire_and_forget ReactNativeBlobUtil::ls (
@@ -1332,10 +1336,9 @@ winrt::fire_and_forget ReactNativeBlobUtil::readFile(
1332
1336
winrt::hstring directoryPath, fileName;
1333
1337
splitPath (path, directoryPath, fileName);
1334
1338
1335
- // Use .get() to synchronously wait for async operations
1336
- auto folder = StorageFolder::GetFolderFromPathAsync (directoryPath).get ();
1337
- auto file = folder.GetFileAsync (fileName).get ();
1338
- auto buffer = FileIO::ReadBufferAsync (file).get ();
1339
+ auto folder = co_await StorageFolder::GetFolderFromPathAsync (directoryPath);
1340
+ auto file = co_await folder.GetFileAsync (fileName);
1341
+ auto buffer = co_await FileIO::ReadBufferAsync (file);
1339
1342
1340
1343
::React::JSValueArray resultArray;
1341
1344
if (encoding == " base64" )
@@ -1431,7 +1434,7 @@ winrt::fire_and_forget ReactNativeBlobUtil::hash(
1431
1434
}
1432
1435
}
1433
1436
1434
- void ReactNativeBlobUtil::readStream (
1437
+ winrt::fire_and_forget ReactNativeBlobUtil::readStream (
1435
1438
std::string path,
1436
1439
std::string encoding,
1437
1440
double bufferSize,
@@ -1461,7 +1464,7 @@ void ReactNativeBlobUtil::readStream(
1461
1464
{" event" , " error" },
1462
1465
{" EINVAL" , " Unsupported encoding: " + encoding}
1463
1466
});
1464
- return ;
1467
+ co_return ;
1465
1468
}
1466
1469
1467
1470
uint32_t chunkSize = (usedEncoding == EncodingOptions::BASE64) ? 4095 : 4096 ;
@@ -1473,15 +1476,15 @@ void ReactNativeBlobUtil::readStream(
1473
1476
winrt::hstring directoryPath, fileName;
1474
1477
splitPath (path, directoryPath, fileName);
1475
1478
1476
- StorageFolder folder = StorageFolder::GetFolderFromPathAsync (directoryPath). get ( );
1477
- StorageFile file = folder.GetFileAsync (fileName). get ( );
1478
- Streams::IRandomAccessStream stream = file.OpenAsync (FileAccessMode::Read). get ( );
1479
+ StorageFolder folder = co_await StorageFolder::GetFolderFromPathAsync (directoryPath);
1480
+ StorageFile file = co_await folder.GetFileAsync (fileName);
1481
+ Streams::IRandomAccessStream stream = co_await file.OpenAsync (FileAccessMode::Read);
1479
1482
1480
1483
Buffer buffer{ chunkSize };
1481
1484
1482
1485
for (;;)
1483
1486
{
1484
- auto readBuffer = stream.ReadAsync (buffer, buffer.Capacity (), InputStreamOptions::None). get ( );
1487
+ auto readBuffer = co_await stream.ReadAsync (buffer, buffer.Capacity (), InputStreamOptions::None);
1485
1488
if (readBuffer.Length () == 0 )
1486
1489
{
1487
1490
m_reactContext.CallJSFunction (L" RCTDeviceEventEmitter" , L" emit" , streamId,
0 commit comments