FTXUI icon indicating copy to clipboard operation
FTXUI copied to clipboard

integration of IO libs that require polling

Open niXman opened this issue 3 years ago • 9 comments

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!

niXman avatar Aug 31 '22 07:08 niXman

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?

ArthurSonzogni avatar Aug 31 '22 20:08 ArthurSonzogni

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!

niXman avatar Sep 01 '22 09:09 niXman

If you can explain what you want to achieve, I might be able to provide the right solution.

Some replies:

subscribe to its event

FTXUI provide any timers

I would suggest you spawn a thread and make it loop with a timer.

ArthurSonzogni avatar Sep 02 '22 10:09 ArthurSonzogni

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!

niXman avatar Sep 03 '22 00:09 niXman

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);

ArthurSonzogni avatar Sep 03 '22 11:09 ArthurSonzogni

hello!

sorry for the delay.

I think the last option is the best! thank you!

niXman avatar Sep 05 '22 08:09 niXman

@ArthurSonzogni hello!

im sorry for disturb, but could you let me know if it makes sense for me to wait for PostPeriodicTask() implementation?

thanks!

niXman avatar Sep 06 '22 12:09 niXman

Posting delayed and periodic tasks is not going to happen soon. I would go with the new Loop

ArthurSonzogni avatar Sep 07 '22 06:09 ArthurSonzogni

got it, many thanks!

niXman avatar Sep 07 '22 06:09 niXman