From ed40a224e4256867d5f8c9f7c4c3ea998d4b88ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sun, 8 Jan 2023 18:39:31 +0100 Subject: Test: fix shared state watcher reset --- src/main.cpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index de4ffbc..12f46ac 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -323,8 +323,15 @@ void sharedStateGet(const vector &) void sharedStateWait(const vector & args) { - static optional>> watched; - watched = h->behavior().lens>().watch([args, watched = watched] (const vector & refs) mutable { + struct SharedStateWait + { + mutex lock; + bool done { false }; + optional>> watched; + }; + auto watchedPtr = make_shared(); + + auto watched = h->behavior().lens>().watch([args, watchedPtr] (const vector & refs) { vector> objs; objs.reserve(refs.size()); for (const auto & r : refs) @@ -345,9 +352,15 @@ void sharedStateWait(const vector & args) ss << " " << a; printLine(ss.str()); - watched = std::nullopt; + scoped_lock lock(watchedPtr->lock); + watchedPtr->done = true; + watchedPtr->watched = std::nullopt; } }); + + scoped_lock lock(watchedPtr->lock); + if (!watchedPtr->done) + watchedPtr->watched = move(watched); } void watchLocalIdentity(const vector &) -- cgit v1.2.3