integration of IO libs that require polling
hi,
for example, boost::asios io_context can be used as main event loop or as independent event loop.
for the second case simply calling poll_one()/run_one() periodically is sufficient.
so my question is: how can I implement a periodic function call preferably with the ability to change the frequency?
many thanks!
Hello @niXman
You can run the ScreenInteractive::Loop() from one thread.
It will draw new frames only after receiving new events. Events are coming from the terminal. If you need to emit other from different kind of sources, you can submit custom ones in order to produce new frames.
screen_interactive::Post(Event::Custom);
Post() can send Task or Event. The function is thread safe.
The main loop will return shortly after someone calls the function returned by:
screen_interactive::ExitClosure();
Calling the returned function is also thread safe.
Does this resolve your issue, or do you want to suggest something more?
Hello @ArthurSonzogni
thanks for your reply!
I think we did not understand each other... does FTXUI provide any timers I can just run and subscribe to its timeout handler/event?
best!
If you can explain what you want to achieve, I might be able to provide the right solution.
Some replies:
subscribe to its event
- Your component can subscribe to ComponentBase::OnEvent.
- Alternatively, in a more functional way you wrap your component within CatchEvent function.
FTXUI provide any timers
I would suggest you spawn a thread and make it loop with a timer.
hello again!
just for explanation for io_context's poll_one() and run_one():
poll_one() will return immediately (non-blocking) in case there is no event to process.
run_one() will block the calling thread until one event is ready to process.
so for example in Qt, the simplest way to integrate asio - is to just use the QTimer whis way:
int main() {
...
io_context ioctx;
QTimer timer;
connect(&timer, &QTimer::timeout, [&ioctx](){ ioctx.poll_one(); });
timer.start(1);
...
}
It's enough.
I want to point out that I would like to avoid using an extra thread because it's redundant.
best!
I see! This could be added.
What about an API like this:
auto screen = ScreenInteractive::FitComponent();
auto component = ...;
auto loop = screen.Start(component);
external_library.MainLoop([&] {
// [...]
loop.Next();
// [...]
});
Alternatively, I also wanted to support delayed tasks. This might be an alternative?
Would you like having the ability to post delayed task:
auto screen = ScreenInteractive::FitComponent();
// Get called periodically:
std::function<void()> task;
task = [&] {
// [...]
screen.PostTask(task, 200ms);
};
screen.PostTask(task, 200ms);
auto component = ...;
screen.Loop(component);
Which could then easily be simplified into:
auto screen = ScreenInteractive::FitComponent();
// Get called periodically:
screen.PostPeriodicTask([&] {
// [...]
return true; // Continue
}, 200ms);
auto component = ...;
screen.Loop(component);
hello!
sorry for the delay.
I think the last option is the best! thank you!
@ArthurSonzogni hello!
im sorry for disturb, but could you let me know if it makes sense for me to wait for PostPeriodicTask() implementation?
thanks!
Posting delayed and periodic tasks is not going to happen soon. I would go with the new Loop
got it, many thanks!