VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
PendingCommit.hpp
Go to the documentation of this file.
1#pragma once
2
6#include <cstdint>
7#include <functional>
8
9namespace VoltMod
10{
11
12/**
13 * @brief One held-back row commit per player: what a stepped row was left showing, applied once
14 * the stepping stops.
15 *
16 * A stepped row shows its new value the moment A/D or a stepper is pressed, but running its
17 * action on every press would make five taps five actions and five broadcasts. @ref Hold keeps the
18 * commit back for @ref DelayMs and replaces whatever the slot was holding, so a burst commits once.
19 *
20 * Every other way out of the row runs what is held rather than dropping it - activating the row,
21 * closing the menu, moving the cursor off it - because a value the player picked and watched
22 * appear is a value they asked for. A slot changing hands is the one case that cancels: nobody is
23 * left to have asked for it.
24 *
25 * SDK-free. The delay arrives as a @ref Timer, which is `Scheduler::Delay` in the framework and a
26 * hand-fired one in the tests, so the policy is unit-tested without an engine or a real clock.
27 */
29{
30public:
31 /** Run @p callback after @p delayMs; dropping the returned subscription cancels it. */
32 using Timer = std::function<Subscription(int64_t delayMs, std::function<void()> callback)>;
33
34 /** How long a stepped value waits before it is applied. */
35 static constexpr int64_t DelayMs = 400;
36
37 /** Store the timer used by @ref Hold. A slot's pending commit is dropped unrun when the slot
38 * changes hands; @p slots must outlive this. */
40
41 /** Hold @p commit for row @p index of @p slot. Holding the same row again restarts the delay;
42 * holding a different one applies what the previous row was holding first. */
43 void Hold(int slot, int index, std::function<void()> commit);
44
45 /** Pending row for @p slot, or -1. */
46 int Index(int slot) const;
47
48 /** True while @p slot's pending commit belongs to row @p index. */
49 bool IsPending(int slot, int index) const;
50
51 /** Apply @p slot's pending commit now, if it has one, and stop its timer. */
52 void Apply(int slot);
53
54 /** Drop @p slot's pending commit without applying it. */
55 void Drop(int slot);
56
57private:
58 struct Entry
59 {
60 int Index = -1;
61 std::function<void()> Commit;
62 /** Declared last: dropping it stops the timer that would have run @ref Commit. */
64 };
65
67 PerSlot<Entry> _entries;
68};
69
70} // namespace VoltMod
One held-back row commit per player: what a stepped row was left showing, applied once the stepping s...
bool IsPending(int slot, int index) const
static constexpr int64_t DelayMs
void Hold(int slot, int index, std::function< void()> commit)
int Index(int slot) const
std::function< Subscription(int64_t delayMs, std::function< void()> callback)> Timer
Per-player-slot value store that never leaks state across occupants.
Definition PerSlot.hpp:25
"The occupant of this slot changed" - raised by PlayerManager, consumed below it.
Owns one registration and releases it on destruction.
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56