Skip to content

Conversation

elbunuelo
Copy link

  • Prevent double initialization by adding an initializing state and running the initialization code only when uninitialized

…ning the initialization code only when uninitialized
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR prevents double initialization of MVC controllers by introducing a state machine with three states: uninitialized, initializing, and initialized. The change replaces the simple boolean initialized flag with a more robust state tracking system.

  • Replaced boolean initialized flag with InitializationState enum
  • Added protection against re-initialization attempts during the initialization process
  • Maintained backward compatibility with a getter for the initialized property

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +160 to +162
throw new Error(
'Attempted to reinitialize controller before initialization finished.'
);
Copy link

Choose a reason for hiding this comment

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

Maybe just logging a warning would be more appropriate? Depends how this was happening I suppose but I worry throwing an error will cause any later/random other code to not be executed?

Copy link
Contributor

@gregplaysguitar gregplaysguitar left a comment

Choose a reason for hiding this comment

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

I'm curious what would cause double initialization in a way that is prevented by this change, given internalInitialize is sync, so there's no async window where a double initialisation might happen? Is it that the controller's initialize method might synchronously call internalInitialize?

@@ -138,8 +146,8 @@ class ApplicationController<

this.state = store(cloneDeep(this.initialState));
if (this.initialize) this.initialize(initialArgs);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this line is at least partially related to the problem. It's possible for the initialize method to throw an error, and I think in that case, we might get into a bad state.

But also, I think the problem is if initialize makes changes that cause a re-render during the initial render? Can/should we run the internalInitialize in a different way perhaps?

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.

4 participants