Fix parallel invocations of repeat action (#4480)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Oxan van Leeuwen 2023-02-26 20:53:53 +01:00 committed by GitHub
parent 62459a8ae1
commit 7c1888a728
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 10 deletions

View File

@ -254,7 +254,11 @@ async def repeat_action_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg) var = cg.new_Pvariable(action_id, template_arg)
count_template = await cg.templatable(config[CONF_COUNT], args, cg.uint32) count_template = await cg.templatable(config[CONF_COUNT], args, cg.uint32)
cg.add(var.set_count(count_template)) cg.add(var.set_count(count_template))
actions = await build_action_list(config[CONF_THEN], template_arg, args) actions = await build_action_list(
config[CONF_THEN],
cg.TemplateArguments(cg.uint32, *template_arg.args),
[(cg.uint32, "iteration"), *args],
)
cg.add(var.add_then(actions)) cg.add(var.add_then(actions))
return var return var

View File

@ -235,22 +235,21 @@ template<typename... Ts> class RepeatAction : public Action<Ts...> {
public: public:
TEMPLATABLE_VALUE(uint32_t, count) TEMPLATABLE_VALUE(uint32_t, count)
void add_then(const std::vector<Action<Ts...> *> &actions) { void add_then(const std::vector<Action<uint32_t, Ts...> *> &actions) {
this->then_.add_actions(actions); this->then_.add_actions(actions);
this->then_.add_action(new LambdaAction<Ts...>([this](Ts... x) { this->then_.add_action(new LambdaAction<uint32_t, Ts...>([this](uint32_t iteration, Ts... x) {
this->iteration_++; iteration++;
if (this->iteration_ == this->count_.value(x...)) if (iteration >= this->count_.value(x...))
this->play_next_tuple_(this->var_); this->play_next_tuple_(this->var_);
else else
this->then_.play_tuple(this->var_); this->then_.play(iteration, x...);
})); }));
} }
void play_complex(Ts... x) override { void play_complex(Ts... x) override {
this->num_running_++; this->num_running_++;
this->var_ = std::make_tuple(x...); this->var_ = std::make_tuple(x...);
this->iteration_ = 0; this->then_.play(0, x...);
this->then_.play_tuple(this->var_);
} }
void play(Ts... x) override { /* ignore - see play_complex */ void play(Ts... x) override { /* ignore - see play_complex */
@ -259,8 +258,7 @@ template<typename... Ts> class RepeatAction : public Action<Ts...> {
void stop() override { this->then_.stop(); } void stop() override { this->then_.stop(); }
protected: protected:
uint32_t iteration_; ActionList<uint32_t, Ts...> then_;
ActionList<Ts...> then_;
std::tuple<Ts...> var_; std::tuple<Ts...> var_;
}; };