Skip to content

Commit 820b73e

Browse files
committed
Implemented enableProgressReport and enableUploadProgressReport
1 parent cb618cd commit 820b73e

File tree

2 files changed

+115
-64
lines changed

2 files changed

+115
-64
lines changed

RNFetchBlobWin/windows/RNFetchBlobWin/RNFetchBlob.cpp

Lines changed: 94 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,21 @@ void TaskCancellationManager::Cancel(TaskId taskId) noexcept
109109
}
110110
}
111111

112-
112+
/*
113+
struct RNFetchBlobProgressConfig {
114+
public:
115+
RNFetchBlobProgressConfig();
116+
RNFetchBlobProgressConfig(int32_t count_, int32_t interval_);
117+
118+
int32_t lastTick;
119+
int32_t tick;
120+
int32_t count;
121+
int32_t interval;
122+
};
123+
*/
124+
125+
RNFetchBlobProgressConfig::RNFetchBlobProgressConfig(int32_t count_, int32_t interval_) : count(count_), interval(interval_) {
126+
}
113127

114128
void RNFetchBlob::Initialize(winrt::Microsoft::ReactNative::ReactContext const& reactContext) noexcept
115129
{
@@ -1055,26 +1069,26 @@ winrt::fire_and_forget RNFetchBlob::fetchBlob(
10551069

10561070
RNFetchBlobConfig config;
10571071

1058-
if (!options["Progress"].IsNull()) {
1059-
auto& downloadOptions{ options["Progress"].AsObject() };
1060-
if (!downloadOptions["count"].IsNull()) {
1061-
int32_t downloadCount{ downloadOptions["count"].AsInt32()};
1062-
bool lol = true;
1063-
}
1064-
if (!downloadOptions["interval"].IsNull()) {
1065-
int32_t downloadInterval{ downloadOptions["interval"].AsInt32() };
1066-
bool lol = true;
1067-
}
1068-
}
1069-
if (!options["UploadProgress"].IsNull()) {
1070-
auto& uploadOptions{ options["UploadProgress"].AsObject() };
1071-
if (!uploadOptions["count"].IsNull()) {
1072-
int32_t uploadCount{ uploadOptions["count"].AsInt32() };
1073-
}
1074-
if (!uploadOptions["interval"].IsNull()) {
1075-
int32_t uploadInterval{ uploadOptions["interval"].AsInt32() };
1076-
}
1077-
}
1072+
//if (!options["Progress"].IsNull()) {
1073+
// auto& downloadOptions{ options["Progress"].AsObject() };
1074+
// if (!downloadOptions["count"].IsNull()) {
1075+
// int32_t downloadCount{ downloadOptions["count"].AsInt32()};
1076+
// bool lol = true;
1077+
// }
1078+
// if (!downloadOptions["interval"].IsNull()) {
1079+
// int32_t downloadInterval{ downloadOptions["interval"].AsInt32() };
1080+
// bool lol = true;
1081+
// }
1082+
//}
1083+
//if (!options["UploadProgress"].IsNull()) {
1084+
// auto& uploadOptions{ options["UploadProgress"].AsObject() };
1085+
// if (!uploadOptions["count"].IsNull()) {
1086+
// int32_t uploadCount{ uploadOptions["count"].AsInt32() };
1087+
// }
1088+
// if (!uploadOptions["interval"].IsNull()) {
1089+
// int32_t uploadInterval{ uploadOptions["interval"].AsInt32() };
1090+
// }
1091+
//}
10781092

10791093
if (options["appendExt"].IsNull() == true)
10801094
{
@@ -1129,7 +1143,7 @@ winrt::fire_and_forget RNFetchBlob::fetchBlob(
11291143
std::string methodUpperCase{ method };
11301144
for (auto& c : methodUpperCase)
11311145
{
1132-
toupper(c);
1146+
putchar(toupper(c));
11331147
}
11341148

11351149
// Delete, Patch, Post, Put, Get, Options, Head
@@ -1177,7 +1191,7 @@ winrt::fire_and_forget RNFetchBlob::fetchBlob(
11771191
}
11781192

11791193
requestMessage.Content(requestContent);
1180-
co_await m_tasks.Add(taskId, ProcessRequestAsync(filter, requestMessage, config, callback));
1194+
co_await m_tasks.Add(taskId, ProcessRequestAsync(taskId, filter, requestMessage, config, callback));
11811195

11821196
}
11831197

@@ -1198,26 +1212,26 @@ winrt::fire_and_forget RNFetchBlob::fetchBlobForm(
11981212

11991213
RNFetchBlobConfig config;
12001214

1201-
if (!options["Progress"].IsNull()) {
1202-
auto& downloadOptions{ options["Progress"].AsObject() };
1203-
if (!downloadOptions["count"].IsNull()) {
1204-
int32_t downloadCount{ downloadOptions["count"].AsInt32() };
1205-
bool lol = true;
1206-
}
1207-
if (!downloadOptions["interval"].IsNull()) {
1208-
int32_t downloadInterval{ downloadOptions["interval"].AsInt32() };
1209-
bool lol = true;
1210-
}
1211-
}
1212-
if (!options["UploadProgress"].IsNull()) {
1213-
auto& uploadOptions{ options["UploadProgress"].AsObject() };
1214-
if (!uploadOptions["count"].IsNull()) {
1215-
int32_t uploadCount{ uploadOptions["count"].AsInt32() };
1216-
}
1217-
if (!uploadOptions["interval"].IsNull()) {
1218-
int32_t uploadInterval{ uploadOptions["interval"].AsInt32() };
1219-
}
1220-
}
1215+
//if (!options["Progress"].IsNull()) {
1216+
// auto& downloadOptions{ options["Progress"].AsObject() };
1217+
// if (!downloadOptions["count"].IsNull()) {
1218+
// int32_t downloadCount{ downloadOptions["count"].AsInt32() };
1219+
// bool lol = true;
1220+
// }
1221+
// if (!downloadOptions["interval"].IsNull()) {
1222+
// int32_t downloadInterval{ downloadOptions["interval"].AsInt32() };
1223+
// bool lol = true;
1224+
// }
1225+
//}
1226+
//if (!options["UploadProgress"].IsNull()) {
1227+
// auto& uploadOptions{ options["UploadProgress"].AsObject() };
1228+
// if (!uploadOptions["count"].IsNull()) {
1229+
// int32_t uploadCount{ uploadOptions["count"].AsInt32() };
1230+
// }
1231+
// if (!uploadOptions["interval"].IsNull()) {
1232+
// int32_t uploadInterval{ uploadOptions["interval"].AsInt32() };
1233+
// }
1234+
//}
12211235

12221236
if (options["appendExt"].IsNull() == true)
12231237
{
@@ -1354,7 +1368,7 @@ winrt::fire_and_forget RNFetchBlob::fetchBlobForm(
13541368
// // TODO: Add in multipart aspects
13551369
//}
13561370
requestMessage.Content(requestContent);
1357-
co_await m_tasks.Add(taskId, ProcessRequestAsync(filter, requestMessage, config, callback));
1371+
co_await m_tasks.Add(taskId, ProcessRequestAsync(taskId, filter, requestMessage, config, callback));
13581372
}
13591373

13601374
//winrt::fire_and_forget RNFetchBlob::createBlobForm(
@@ -1374,18 +1388,20 @@ winrt::fire_and_forget RNFetchBlob::fetchBlobForm(
13741388
void RNFetchBlob::enableProgressReport(
13751389
std::string taskId,
13761390
int interval,
1377-
int count) noexcept
1378-
{
1379-
return;
1391+
int count) noexcept {
1392+
RNFetchBlobProgressConfig config{interval, count};
1393+
std::scoped_lock lock{ m_mutex };
1394+
downloadProgressMap.try_emplace(taskId, config);
13801395
}
13811396

13821397
// enableUploadProgressReport
13831398
void RNFetchBlob::enableUploadProgressReport(
13841399
std::string taskId,
13851400
int interval,
1386-
int count) noexcept
1387-
{
1388-
return;
1401+
int count) noexcept {
1402+
RNFetchBlobProgressConfig config{ interval, count };
1403+
std::scoped_lock lock{ m_mutex };
1404+
uploadProgressMap.try_emplace(taskId, config);
13891405
}
13901406

13911407
// cancelRequest
@@ -1456,6 +1472,7 @@ void RNFetchBlob::splitPath(const std::wstring& fullPath, winrt::hstring& direct
14561472
}
14571473

14581474
winrt::Windows::Foundation::IAsyncAction RNFetchBlob::ProcessRequestAsync(
1475+
const std::string& taskId,
14591476
const winrt::Windows::Web::Http::Filters::HttpBaseProtocolFilter& filter,
14601477
winrt::Windows::Web::Http::HttpRequestMessage& httpRequestMessage,
14611478
RNFetchBlobConfig& config,
@@ -1491,32 +1508,45 @@ try
14911508
for (;;)
14921509
{
14931510
buffer.Length(0);
1494-
auto readBuffer = contentStream.ReadAsync(buffer, buffer.Capacity(), InputStreamOptions::None).get();
1511+
auto readBuffer = co_await contentStream.ReadAsync(buffer, buffer.Capacity(), InputStreamOptions::None);
14951512
totalRead += readBuffer.Length();
14961513
if (readBuffer.Length() == 0)
14971514
{
14981515
break;
14991516
}
15001517
co_await outputStream.WriteAsync(readBuffer);
15011518

1502-
m_reactContext.CallJSFunction(L"RCTDeviceEventEmitter", L"emit", L"RNFetchBlobProgress",
1503-
Microsoft::ReactNative::JSValueObject{
1504-
{ "recieved", totalRead },
1505-
{ "total", contentLength.Type() == PropertyType::UInt64 ?
1506-
Microsoft::ReactNative::JSValue(contentLength.Value()) :
1507-
Microsoft::ReactNative::JSValue{nullptr} },
1508-
});
1509-
1519+
// condition if taskId in a table where I need to report, then emit this
1520+
// to do that, I need a map that with a mutex
1521+
// mutex for all rn fetch blob main class
1522+
// std::scoped_lock lock {m_mutex}
1523+
auto it{ downloadProgressMap.find(taskId) };
1524+
if (it != downloadProgressMap.end()) {
1525+
auto var{ downloadProgressMap[taskId] };
1526+
m_reactContext.CallJSFunction(L"RCTDeviceEventEmitter", L"emit", L"RNFetchBlobProgress",
1527+
Microsoft::ReactNative::JSValueObject{
1528+
{ "recieved", totalRead },
1529+
{ "total", contentLength.Type() == PropertyType::UInt64 ?
1530+
Microsoft::ReactNative::JSValue(contentLength.Value()) :
1531+
Microsoft::ReactNative::JSValue{nullptr} },
1532+
});
1533+
}
15101534
}
15111535
callback("", "path", config.path);
15121536

15131537
}
15141538
else {
1515-
m_reactContext.CallJSFunction(L"RCTDeviceEventEmitter", L"emit", L"RNFetchBlobProgress",
1516-
Microsoft::ReactNative::JSValueObject{
1517-
{ "recieved", 0 },
1518-
{ "total", 0},
1519-
});
1539+
auto it{ downloadProgressMap.find(taskId) };
1540+
if (it != downloadProgressMap.end()) {
1541+
1542+
auto var{ downloadProgressMap[taskId] };
1543+
1544+
m_reactContext.CallJSFunction(L"RCTDeviceEventEmitter", L"emit", L"RNFetchBlobProgress",
1545+
Microsoft::ReactNative::JSValueObject{
1546+
{ "recieved", 0 },
1547+
{ "total", 0},
1548+
});
1549+
}
15201550
callback(winrt::to_string(co_await response.Content().ReadAsStringAsync()), "result", config.path);
15211551
}
15221552
co_return;

RNFetchBlobWin/windows/RNFetchBlobWin/RNFetchBlob.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@ struct RNFetchBlobConfig
7373
};
7474

7575

76+
struct RNFetchBlobProgressConfig {
77+
public:
78+
RNFetchBlobProgressConfig() = default;
79+
RNFetchBlobProgressConfig(int32_t count_, int32_t interval_);
80+
81+
private:
82+
int32_t lastTick{ 0 };
83+
int32_t tick{ 0 };
84+
int32_t count{ -1 };
85+
int32_t interval{ -1 };
86+
};
87+
88+
7689
REACT_MODULE(RNFetchBlob, L"RNFetchBlob");
7790
struct RNFetchBlob
7891
{
@@ -302,6 +315,7 @@ struct RNFetchBlob
302315
TaskCancellationManager m_tasks;
303316

304317
winrt::Windows::Foundation::IAsyncAction ProcessRequestAsync(
318+
const std::string& taskId,
305319
const winrt::Windows::Web::Http::Filters::HttpBaseProtocolFilter& filter,
306320
winrt::Windows::Web::Http::HttpRequestMessage& httpRequestMessage,
307321
RNFetchBlobConfig& config,
@@ -334,4 +348,11 @@ struct RNFetchBlob
334348
winrt::hstring& fileName) noexcept;
335349

336350
const std::string prefix{ "RNFetchBlob-file://" };
351+
352+
std::mutex m_mutex;
353+
std::map<std::string, RNFetchBlobProgressConfig> downloadProgressMap;
354+
std::map<std::string, RNFetchBlobProgressConfig> uploadProgressMap;
337355
};
356+
357+
358+

0 commit comments

Comments
 (0)