diff --git a/packages/dear-imgui/cpp/app/include/xframes.h b/packages/dear-imgui/cpp/app/include/xframes.h index 81e3245b..bb0563f6 100644 --- a/packages/dear-imgui/cpp/app/include/xframes.h +++ b/packages/dear-imgui/cpp/app/include/xframes.h @@ -60,6 +60,7 @@ class XFrames { std::mutex m_commitMutex; uint64_t m_nativeSequence = 0; uint64_t m_nativeRevision = 0; + std::atomic m_visibleRevision{0}; std::unordered_set m_publicationOwnedIds; std::atomic m_surfaceQuarantined{false}; std::atomic m_runtimeDisposed{false}; @@ -197,6 +198,10 @@ class XFrames { // Synchronous structural API. uint64 counters use decimal strings on the wire. xframes::CommitResult ApplyCommit(std::string_view serializedCommit); json GetCommitState(); + // The native revision of the tree that frames show: stored where the revision + // advances, under the visibility locks. An event raised while a frame is built + // belongs to this tree. Safe to read from any thread. + uint64_t VisibleRevision() const { return m_visibleRevision.load(std::memory_order_acquire); } diff --git a/packages/dear-imgui/cpp/app/src/xframes_commit.cpp b/packages/dear-imgui/cpp/app/src/xframes_commit.cpp index 27d03694..d3751cd6 100644 --- a/packages/dear-imgui/cpp/app/src/xframes_commit.cpp +++ b/packages/dear-imgui/cpp/app/src/xframes_commit.cpp @@ -124,6 +124,7 @@ CommitResult XFrames::ApplyCommitOperations(CommitBatch& batch) { if (!m_frameScheduler.Invalidate(FrameReason::Publication)) throw std::overflow_error("Invalidation generation exhausted during publication"); result.nativeRevision = ++m_nativeRevision; + m_visibleRevision.store(m_nativeRevision, std::memory_order_release); result.status = "applied"; } catch (const std::exception& error) { // Quarantine is established under the visibility locks even if diff --git a/packages/dear-imgui/cpp/tests/xframes_test.cpp b/packages/dear-imgui/cpp/tests/xframes_test.cpp index 4fd97420..1ca02a88 100644 --- a/packages/dear-imgui/cpp/tests/xframes_test.cpp +++ b/packages/dear-imgui/cpp/tests/xframes_test.cpp @@ -85,7 +85,9 @@ class XFramesTest : public ::testing::Test { }; } void Internal(int id, const json& operation) { auto payload = operation.dump(); xf->QueueElementInternalOp(id, payload); } - void SetCounters(uint64_t sequence, uint64_t revision) { xf->m_nativeSequence = sequence; xf->m_nativeRevision = revision; } + void SetCounters(uint64_t sequence, uint64_t revision) { + xf->m_nativeSequence = sequence; xf->m_nativeRevision = revision; xf->m_visibleRevision = revision; + } bool LastRequestExpired() { bool expired = false; auto subscription = rpp::composite_disposable_wrapper::make(); @@ -317,6 +319,19 @@ TEST_F(XFramesTest, PublicationCompetingWritersCannotOverwriteAStaleTree) { EXPECT_EQ(xf->GetCommitState()["nativeRevision"], "2"); } +// Events are stamped with the visible revision: the tree every frame drawn since +// the last applied publication shows. It moves only when a publication applies. +TEST_F(XFramesTest, VisibleRevisionFollowsAppliedPublicationsOnly) { + EXPECT_EQ(xf->VisibleRevision(), 0u); + Publish({{0, {1}}, {1, {}}}, {Create(1)}); + EXPECT_EQ(xf->VisibleRevision(), 1u); + Reject(Wire({{0, {1}}, {1, {}}}, {Create(1)}), "duplicate_id", 0); + EXPECT_EQ(xf->VisibleRevision(), 1u); + Publish(); + EXPECT_EQ(xf->VisibleRevision(), 2u); + EXPECT_EQ(std::to_string(xf->VisibleRevision()), xf->GetCommitState()["nativeRevision"].get()); +} + // "childLists": "changed" carries only the child lists that changed. An omitted // list is the previous one; a created node without an assignment has none. namespace {