-
-
Notifications
You must be signed in to change notification settings - Fork 203
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
Use DI for controller communication rather than context #387
Changes from all commits
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 |
---|---|---|
@@ -1,12 +1,5 @@ | ||
import BaseController from './BaseController'; | ||
|
||
/** | ||
* Child controller instances keyed by controller name | ||
*/ | ||
export interface ChildControllerContext { | ||
[key: string]: BaseController<any, any>; | ||
} | ||
|
||
/** | ||
* List of child controller instances | ||
*/ | ||
|
@@ -15,15 +8,8 @@ export type ControllerList = BaseController<any, any>[]; | |
/** | ||
* Controller that can be used to compose multiple controllers together | ||
*/ | ||
export class ComposableController extends BaseController<any, any> { | ||
private cachedState: any; | ||
|
||
private internalControllers: ControllerList = []; | ||
|
||
/** | ||
* Map of stores to compose together | ||
*/ | ||
context: ChildControllerContext = {}; | ||
export class ComposableController extends BaseController<never, any> { | ||
private controllers: ControllerList = []; | ||
|
||
/** | ||
* Name of this controller used during composition | ||
|
@@ -36,45 +22,22 @@ export class ComposableController extends BaseController<any, any> { | |
* @param controllers - Map of names to controller instances | ||
* @param initialState - Initial state keyed by child controller name | ||
*/ | ||
constructor(controllers: ControllerList = [], initialState?: any) { | ||
super(); | ||
constructor(controllers: ControllerList) { | ||
super( | ||
undefined, | ||
controllers.reduce((state, controller) => { | ||
state[controller.name] = controller.state; | ||
return state; | ||
}, {} as any), | ||
); | ||
this.initialize(); | ||
this.cachedState = initialState; | ||
this.controllers = controllers; | ||
this.cachedState = undefined; | ||
} | ||
|
||
/** | ||
* Get current list of child composed store instances | ||
* | ||
* @returns - List of names to controller instances | ||
*/ | ||
get controllers() { | ||
return this.internalControllers; | ||
} | ||
|
||
/** | ||
* Set new list of controller instances | ||
* | ||
* @param controllers - List of names to controller instsances | ||
*/ | ||
set controllers(controllers: ControllerList) { | ||
this.internalControllers = controllers; | ||
const initialState: any = {}; | ||
controllers.forEach((controller) => { | ||
this.controllers.forEach((controller) => { | ||
const { name } = controller; | ||
this.context[name] = controller; | ||
controller.context = this.context; | ||
this.cachedState?.[name] && controller.update(this.cachedState[name]); | ||
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. Apparently this line is how most controllers on mobile had their initial state set. I had wrongly assumed they were being passed initial state via their constructors. This initial state setting has been re-implemented in mobile: MetaMask/metamask-mobile@2b80235 |
||
initialState[name] = controller.state; | ||
controller.subscribe((state) => { | ||
this.update({ [name]: state }); | ||
}); | ||
}); | ||
controllers.forEach((controller) => { | ||
controller.onComposed(); | ||
}); | ||
this.update(initialState, true); | ||
} | ||
|
||
/** | ||
|
@@ -86,8 +49,8 @@ export class ComposableController extends BaseController<any, any> { | |
*/ | ||
get flatState() { | ||
let flatState = {}; | ||
for (const name in this.context) { | ||
flatState = { ...flatState, ...this.context[name].state }; | ||
for (const controller of this.controllers) { | ||
flatState = { ...flatState, ...controller.state }; | ||
} | ||
return flatState; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that the getter and setter for the
controllers
property has been removed. It appears to have been completely unused. I searched and found no references to it in this repo or in mobile.