opentui icon indicating copy to clipboard operation
opentui copied to clipboard

feat: improve lifecycle management

Open limwa opened this issue 4 weeks ago • 5 comments

Fixes #248.

Changes

Core

  • Adds new exitSignals option to renderer, which specifies a list of signals that the renderer should destroy itself upon receiving.
    • The handlers are mounted when the renderer is created or resumed, and are unmounted when the renderer is suspended.
  • The exit handler is now run on the beforeExit event. The exit event does not allow for more tasks to be added to the event loop, and thus the Bun.sleep(200) call would be effectively ignored, preventing the captured logs from being dumped.

React

  • Deduplicates repeated logic between the createRoot and render methods.
  • Exposes the renderer on the render methods to make it easier to implement external lifecycle control.

Solid

  • Exposes the renderer on the render methods to make it easier to implement external lifecycle control.
  • Applies the changes from #270 to the @opentui/solid package as well. This is helpful for lifecycle control because it inherently exposes the renderer (the user has to create one beforehand).
    • A createRoot method already exists in the solid-js package, which could create some confusion. Should I rename it? Open to suggestions!
    • Also updated all @opentui/solid examples and README.md to reflect the new API.

limwa avatar Nov 11 '25 20:11 limwa

Thanks for looking into this. This makes sense. Only thing I need to work with this is the engine for the Timeline. We could just let the renderer have an engine getter (engine was just for the lack of better naming, can be renamed), which lazily creates an engine instance and attaches it to the renderer and the useTimeline hook can use renderer.engine instead of the global one. Then it should work. I don't think that is test covered currently I am afraid.

kommander avatar Nov 11 '25 21:11 kommander

Also changed the exitHandler to run on beforeExit instead of exit. On exit, the capture dumping would not run because the runtime does not allow for more tasks to be scheduled on the event loop at that point. Using the beforeExit event fixes that problem!

limwa avatar Nov 11 '25 23:11 limwa

Nice, I'll take it for a quick spin in the morning and check what I need to do for opencode, but this should be fine.

kommander avatar Nov 12 '25 01:11 kommander

Sorry I've broken this

kommander avatar Nov 12 '25 22:11 kommander

Sorry I've broken this

I'll take a look at it tomorrow, no worries!

limwa avatar Nov 12 '25 23:11 limwa