summaryrefslogtreecommitdiffstats
path: root/crates/core/tedge_lib/src/mainloop/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/core/tedge_lib/src/mainloop/mod.rs')
-rw-r--r--crates/core/tedge_lib/src/mainloop/mod.rs45
1 files changed, 45 insertions, 0 deletions
diff --git a/crates/core/tedge_lib/src/mainloop/mod.rs b/crates/core/tedge_lib/src/mainloop/mod.rs
new file mode 100644
index 00000000..68427e93
--- /dev/null
+++ b/crates/core/tedge_lib/src/mainloop/mod.rs
@@ -0,0 +1,45 @@
+//! Utility functionality for building a mainloop for a plugin
+//!
+
+mod ticking;
+pub use ticking::MainloopTick;
+
+mod stopper;
+pub use stopper::MainloopStopper;
+
+mod detach;
+pub use detach::MainloopDetach;
+
+pub struct Mainloop;
+
+impl Mainloop {
+ pub fn ticking_every<State>(
+ duration: std::time::Duration,
+ state: State,
+ ) -> (MainloopStopper, MainloopTick<State>)
+ where
+ State: Sized,
+ {
+ let token = tedge_api::CancellationToken::new();
+ let mainloop = MainloopTick {
+ state,
+ logging: false,
+ stopper: token.clone(),
+ duration,
+ };
+ let stopper = MainloopStopper(token);
+
+ (stopper, mainloop)
+ }
+
+ pub fn detach<State>(state: State) -> (MainloopStopper, MainloopDetach<State>) {
+ let token = tedge_api::CancellationToken::new();
+ let mainloop = MainloopDetach {
+ state,
+ stopper: token.clone(),
+ };
+ let stopper = MainloopStopper(token);
+
+ (stopper, mainloop)
+ }
+}