-
Notifications
You must be signed in to change notification settings - Fork 2
ACT-2120: Prevent double initialization of mvc controllers #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,13 @@ type GetControllerConstructor<T> = { new (): T }; | |
type GetControllerProps<T extends ApplicationControllerConstructor<any>> = | ||
T extends ApplicationControllerConstructor<infer P> ? P : never; | ||
|
||
type InitializationState = 'uninitialized' | 'initializing' | 'initialized'; | ||
const InitializationStates: { [k: string]: InitializationStates } = { | ||
INITIALIZING: 'initializing', | ||
UNINITIALIZED: 'uninitialized', | ||
INITIALIZED: 'initialized', | ||
} as const; | ||
|
||
/** | ||
* General rules to follow for using controllers: | ||
* | ||
|
@@ -51,7 +58,7 @@ class ApplicationController< | |
Parent extends ApplicationController<any, any, any> = any, | ||
> { | ||
id: string; | ||
initialized: boolean; | ||
initializationState: InitializationState; | ||
parent: Parent; | ||
state: State; | ||
proxiedThis: any; | ||
|
@@ -62,7 +69,7 @@ class ApplicationController< | |
|
||
constructor() { | ||
this.id = randomId(); | ||
this.initialized = false; | ||
this.initializationState = InitializationStates.UNINITIALIZED; | ||
this.parent = null; | ||
this.state = undefined; | ||
this.runOnDestroy = []; | ||
|
@@ -124,7 +131,8 @@ class ApplicationController< | |
* @hidden | ||
*/ | ||
internalInitialize(parentController: Parent, initialArgs: Props) { | ||
if (!this.initialized) { | ||
if (this.initializationState === InitializationStates.UNINITIALIZED) { | ||
this.initializationState = InitializationStates.INITIALIZING; | ||
this.parent = parentController; | ||
|
||
debug( | ||
|
@@ -138,8 +146,8 @@ class ApplicationController< | |
|
||
this.state = store(cloneDeep(this.initialState)); | ||
if (this.initialize) this.initialize(initialArgs); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 But also, I think the problem is if |
||
this.initialized = true; | ||
} else { | ||
this.initializationState = InitializationStates.INITIALIZED; | ||
} else if (this.initializationState === InitializationStates.INITIALIZED) { | ||
const oldProps = { ...this.props }; | ||
Object.keys(initialArgs).forEach(key => { | ||
if (this.props[key] !== initialArgs[key]) { | ||
|
@@ -148,9 +156,17 @@ class ApplicationController< | |
}); | ||
|
||
this.changeProps(initialArgs, oldProps); | ||
} else { | ||
throw new Error( | ||
'Attempted to reinitialize controller before initialization finished.' | ||
); | ||
Comment on lines
+160
to
+162
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
} | ||
} | ||
|
||
get initialized() { | ||
return this.initializationState === InitializationStates.INITIALIZED; | ||
} | ||
|
||
/** | ||
* Controllers can override this method to cleanup when removed | ||
*/ | ||
|
Uh oh!
There was an error while loading. Please reload this page.