Skip to content
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

feat: Upgrade alert controller to base controller v2 #28054

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

kanthesha
Copy link
Contributor

@kanthesha kanthesha commented Oct 23, 2024

Description

Following the Wallet Framework team's OKRs for Q3 2024, we want to bring AlertController up to date with our latest controller patterns.

Open in GitHub Codespaces

Related issues

Fixes: #25915

Manual testing steps

  1. Go to this page...

Screenshots/Recordings

Before

After

Pre-merge author checklist

Pre-merge reviewer checklist

  • I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed).
  • I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.

@kanthesha kanthesha requested a review from a team October 23, 2024 22:13
@kanthesha kanthesha self-assigned this Oct 23, 2024
Copy link
Contributor

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

@metamaskbot
Copy link
Collaborator

Builds ready [3c04312]
Page Load Metrics (2279 ± 109 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint28426201862786377
domContentLoaded178025472241235113
load183125942279228109
domInteractive31104592110
backgroundConnect8104363115
firstReactRender623031395928
getState766352010
initialActions00000
loadScripts12981974169820699
setupStore1690392211
uiStartup204033372613316152
Bundle size diffs [🚀 Bundle size reduced!]
  • background: -587 Bytes (-0.01%)
  • ui: 0 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

@kanthesha kanthesha marked this pull request as ready for review October 24, 2024 08:33
@kanthesha kanthesha requested a review from a team as a code owner October 24, 2024 08:33
mikesposito
mikesposito previously approved these changes Oct 24, 2024
Copy link
Member

@mikesposito mikesposito left a comment

Choose a reason for hiding this comment

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

Looks good! only minor comments.

};

const defaultState: AlertControllerState = {
export const getDefaultAlertControllerState = (): AlertControllerState => ({
Copy link
Member

Choose a reason for hiding this comment

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

nit: can we add some jsdoc for this exported var?


const alertMessenger = controllerMessenger.getRestricted({
const setupController = ({
Copy link
Member

Choose a reason for hiding this comment

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

nit: we could use the withController pattern we have been using for other controllers

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, it make sense to have a common coding patterns.

cryptodev-2s
cryptodev-2s previously approved these changes Oct 24, 2024
@metamaskbot
Copy link
Collaborator

Builds ready [7c3949e]
Page Load Metrics (1957 ± 74 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint17722460195115775
domContentLoaded17262389190114871
load17732464195715574
domInteractive1797552110
backgroundConnect1295512813
firstReactRender541921042713
getState66521199
initialActions01000
loadScripts12781788142511856
setupStore1280272110
uiStartup19392687218117684
Bundle size diffs [🚀 Bundle size reduced!]
  • background: -587 Bytes (-0.01%)
  • ui: 0 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

Copy link
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

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

Overall looks pretty good, just had some suggestions on simplifying certain sections.

web3ShimUsageOrigins[origin] = value;
this.store.updateState({ web3ShimUsageOrigins });
this.update((state) => {
state.web3ShimUsageOrigins = state.web3ShimUsageOrigins || {};
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need this line? It wasn't there in the original code. We also already check for the presence of this property on line 220.

Suggested change
state.web3ShimUsageOrigins = state.web3ShimUsageOrigins || {};

Comment on lines +96 to +108
await withController({ state: initialState }, ({ controller }) => {
expect(controller.state).toStrictEqual({
alertEnabledness: {
unconnectedAccount: true,
web3ShimUsage: true,
},
unconnectedAccountAlertShownOrigins: {
testUnconnectedOrigin: false,
},
web3ShimUsageOrigins: {
testWeb3ShimUsageOrigin: 0,
},
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, this test is funny. "Default state" implies that no initial state is passed and yet this controller was instantiated with initial state in the test setup. And now we are explicitly passing state. So I feel like one or both of these things isn't needed.

Perhaps we can simplify it to the following?

Suggested change
await withController({ state: initialState }, ({ controller }) => {
expect(controller.state).toStrictEqual({
alertEnabledness: {
unconnectedAccount: true,
web3ShimUsage: true,
},
unconnectedAccountAlertShownOrigins: {
testUnconnectedOrigin: false,
},
web3ShimUsageOrigins: {
testWeb3ShimUsageOrigin: 0,
},
});
const defaultState = getDefaultAlertControllerState();
await withController({ state: defaultState }, ({ controller }) => {
expect(controller.state).toStrictEqual(defaultState);

alertController.store.getState().alertEnabledness.unconnectedAccount,
).toStrictEqual(true);
it('should default unconnectedAccount of alertEnabledness to true', async () => {
await withController({ state: initialState }, ({ controller }) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to pass initial state? The controller is defined such the state option is optional, in which case it just uses the default state. So it seems like we could say:

Suggested change
await withController({ state: initialState }, ({ controller }) => {
await withController(({ controller }) => {

Comment on lines +123 to +125
await withController(
{ state: initialState },
({ controller, messenger }) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar point as above: can we simplify this?

Suggested change
await withController(
{ state: initialState },
({ controller, messenger }) => {
await withController(({ controller, messenger }) => {

I won't make suggestions on the remaining tests, but the same applies.

messenger.call('AlertController:getState').web3ShimUsageOrigins,
).toStrictEqual(defaultWeb3ShimUsageOrigins);
},
);
});
});

describe('AlertController:stateChange', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, is this test needed at all? We don't often test that a controller's stateChange event gets published when its state changes, because that functionality is provided by BaseController, and we already test it there. Perhaps we can remove it entirely?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Needs dev review
Development

Successfully merging this pull request may close these issues.

Migrate AlertController to BaseController v2
5 participants