brainboxdotcc / DPP

C++ Discord API Bot Library - D++ is Lightweight and scalable for small and huge bots!
https://dpp.dev/
Apache License 2.0
1.01k stars 155 forks source link

when_all method #1153

Open tarolling opened 1 month ago

tarolling commented 1 month ago

Is your feature request related to a problem? Please describe. No problems, just inspiration from the implemented when_any with coroutines.

Describe the solution you'd like Stated above, it would be nice to have a when_all (analogous to a Promise.allSettled from JS) method that works very similarly to when_any, except on the return condition. So all coroutines in the proposed when_all call would have to be fulfilled before resuming execution. In other words, it would resume when all coroutines complete.

Describe alternatives you've considered Instead of alternatives, I'll give a use case. Say you want to send a DM to multiple users and want to wait until all of them have responded/interacted/etc. with the message. You could use this when_all to do this very easily.

Additional context This may already be a feature idk, but if not, it would be a logical pairing with when_any.

Mishura4 commented 1 month ago

Thanks for the suggestion! I've been putting this off mostly because it's very easy to do it without a dedicated library feature:

dpp::task<void> tasks[4];

for (auto& task : tasks) {
  task = bot.co_do_something();
}
for (auto& task : tasks) {
  co_await task;
}

Though maybe a when_all could do something this can't, I'm not sure. It could also just exist and do that, I just haven't had much time lately so it's been low priority.

Jaskowicz1 commented 1 month ago

Thanks for the suggestion! I've been putting this off mostly because it's very easy to do it without a dedicated library feature:

dpp::task<void> tasks[4];

for (auto& task : tasks) {
  task = bot.co_do_something();
}
for (auto& task : tasks) {
  co_await task;
}

Though maybe a when_all could do something this can't, I'm not sure. It could also just exist and do that, I just haven't had much time lately so it's been low priority.

Tbh I would take a when_all as a "fire in any order and be await them all to complete", but I guess you could just let them fire in order.

I do wonder if there's any specific case to needing it to be randomly fired.

tarolling commented 1 month ago

Could jobs be launched in parallel perhaps? I'm not sure if when_any is implemented like this, or if this is feasible. Would be something interesting to explore if it's possible though.

Mishura4 commented 1 month ago

Could jobs be launched in parallel perhaps? I'm not sure if when_any is implemented like this, or if this is feasible. Would be something interesting to explore if it's possible though.

This happens to begin with with dpp::task and dpp::async