Skip to content

Remove useSingletons, app.singletons, only instantiate kclManager within routeLoaders#10410

Draft
franknoirot wants to merge 8 commits intomainfrom
franknoirot/9956/optional-kclmanager
Draft

Remove useSingletons, app.singletons, only instantiate kclManager within routeLoaders#10410
franknoirot wants to merge 8 commits intomainfrom
franknoirot/9956/optional-kclmanager

Conversation

@franknoirot
Copy link
Contributor

Marching towards #9956 and #6836. This makes the app not instantiate kclManager as a permanent "singleton", and in fact removes the buildSingletons method entirely. This is a refactor that should have no impact on user experience.

To keep the diff down and allow React components to continue to treat project and executingEditor as non-optional, two new components have been introduced: <ProjectProvider /> and <ExecutingEditorProvider />, with corresponding use* hooks. These providers allow use to section off parts of the component tree which shouldn't render if there is no project or executing editor, respectively. In upcoming PRs, I will make these components able to take a fallback component, so that we can provide empty states where needed. Since the router boundary will define whether a project is present or not, that provider won't likely move anytime soon. But there are several parts of the app that need to handle the "no executing editor" state, so that will likely be pushed down the component tree and duplicated in a few places.

Some places—notably above the /file route—need access to the current project or executing editor if they are available. For these, I use the useSignals() hook from Preact, and peel off app.projectSignal.value and app.projectSignal.value?.executingEditor.value respectively. There are a few CommandBar input components that require this, as well as Router itself so that the engineCommandManager can be peeled off to power the network health context.

@franknoirot franknoirot requested a review from a team as a code owner March 11, 2026 21:46
@vercel
Copy link

vercel bot commented Mar 11, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
modeling-app Ready Ready Preview, Comment Mar 13, 2026 1:14pm

Request Review

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These components and hooks are how I kept the diff on this PR down

* Until we update these dependents of the settings to take settings
* as a dependency input, we must subscribe to updates from the outside.
*/
onSettingsUpdate = (snapshot: SnapshotFrom<typeof this.settings.actor>) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was moved into KclManager here since everything in this handler deals with an executing editor and its subsystems.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ding dong useSingletons is dead!

Comment on lines +74 to +76
<ProjectProvider>
{/** TODO: Decouple opening project from opening an executing editor and move this provider down! */}
<ExecutingEditorProvider>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is where I employ those two new components for now. In future PRs, ExecutingEditorProviders will be further down in the component tree, but I need to isolate things a bit more.

// Cancel all KCL executions while on the home page
useEffect(() => {
markOnce('code/didLoadHome')
kclManager.cancelAllExecutions()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved into the close method of KclManager

Comment on lines +196 to +195
currentFilePath || PROJECT_ENTRYPOINT,
app.singletons.kclManager,
// If persistCode in localStorage is present, it'll persist that code
// through *anything*. INTENDED FOR TESTS.
window.electron?.process.env.NODE_ENV === 'test'
? kclManager.localStoragePersistCode()
: undefined
currentFilePath || PROJECT_ENTRYPOINT
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We no longer provide a KclManager here, we for real instantiate it anew!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant