Skip to content

Commit

Permalink
fix(promise): resolve all promise when empty
Browse files Browse the repository at this point in the history
  • Loading branch information
ArcticLampyrid committed May 24, 2024
1 parent 4223b70 commit f969987
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions src/include/promise/outer.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,29 +68,37 @@ class EventoPromise {
auto remaining = std::make_shared<size_t>(count);
auto mtx = std::make_shared<std::mutex>();
if constexpr (std::is_void_v<R>) {
for (auto it = first; it != last; ++it) {
(*it).then([remaining, resolver, mtx]() {
std::unique_lock<std::mutex> lock(*mtx);
if (--*remaining == 0) {
lock.unlock();
resolver.resolve();
}
});
if (count == 0) {
resolver.resolve();
} else {
for (auto it = first; it != last; ++it) {
(*it).then([remaining, resolver, mtx]() {
std::unique_lock<std::mutex> lock(*mtx);
if (--*remaining == 0) {
lock.unlock();
resolver.resolve();
}
});
}
}
} else {
size_t index = 0;
auto result = std::make_shared<std::vector<R>>();
result->resize(count);
for (auto it = first; it != last; ++it) {
(*it).then([result, resolver, remaining, mtx, index](R value) {
std::unique_lock<std::mutex> lock(*mtx);
(*result)[index] = std::move(value);
if (--*remaining == 0) {
lock.unlock();
resolver.resolve(std::move(*result));
}
});
index++;
if (count == 0) {
resolver.resolve(std::move(*result));
} else {
result->resize(count);
for (auto it = first; it != last; ++it) {
(*it).then([result, resolver, remaining, mtx, index](R value) {
std::unique_lock<std::mutex> lock(*mtx);
(*result)[index] = std::move(value);
if (--*remaining == 0) {
lock.unlock();
resolver.resolve(std::move(*result));
}
});
index++;
}
}
}
return promise;
Expand Down

0 comments on commit f969987

Please sign in to comment.