-
-
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
feat: Re-simulate focused transactions every 3000 ms #5189
base: main
Are you sure you want to change the base?
Conversation
@metamaskbot publish-preview |
Preview builds have been published. See these instructions for more information about preview builds. Expand for full list of packages and versions.
|
@metamaskbot publish-preview |
Preview builds have been published. See these instructions for more information about preview builds. Expand for full list of packages and versions.
|
@metamaskbot publish-preview |
Preview builds have been published. See these instructions for more information about preview builds. Expand for full list of packages and versions.
|
f159aea
to
ea69888
Compare
@metamaskbot publish-preview |
Preview builds have been published. See these instructions for more information about preview builds. Expand for full list of packages and versions.
|
packages/transaction-controller/src/helpers/ResimulateHelper.ts
Outdated
Show resolved
Hide resolved
packages/transaction-controller/src/TransactionController.test.ts
Outdated
Show resolved
Hide resolved
Co-authored-by: Elliot Winkler <[email protected]>
Co-authored-by: Elliot Winkler <[email protected]>
Co-authored-by: Elliot Winkler <[email protected]>
/** | ||
* Whether the transaction confirmation window is focused. | ||
*/ | ||
isFocused?: boolean; |
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.
Would it be more helpful to call this isActive
?
As the UI can own what this means, currently focused and visible but those requirements may change in future?
onStateChange: (listener) => { | ||
this.messagingSystem.subscribe( | ||
'TransactionController:stateChange', | ||
listener, |
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.
Can we use a selector to limit the events to when the transactions
change?
export type ResimulateHelperOptions = { | ||
getTransactions: () => TransactionMeta[]; | ||
onStateChange: (listener: () => void) => void; | ||
updateSimulationData: (transactionMeta: TransactionMeta) => Promise<void>; |
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.
Would it be more explicit to call this simulateTransaction
as it actually performs the simulation, not just updates state?
this.#getTransactions = getTransactions; | ||
this.#updateSimulationData = updateSimulationData; | ||
|
||
onStateChange(() => { |
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.
Minor, would it be cleaner for this callback to go in a private function rather than within the constructor?
); | ||
|
||
// Start or stop resimulation based on the current isFocused state | ||
unapprovedTransactions.forEach((transactionMeta) => { |
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.
Minor, would it be cleaner to have a #getActiveUnapprovedTransactions(active: boolean)
?
|
||
export class ResimulateHelper { | ||
// Map of transactionId <=> isActive | ||
readonly #activeResimulations: Map<string, boolean> = new Map(); |
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.
Do we need this property at all?
Can we not infer the active transaction IDs from the keys of #intervalIds
?
} | ||
|
||
this.#removeListener(transactionId); | ||
log(`Stopped resimulating transaction ${transactionId} every 3 seconds`); |
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.
Minor, should we avoid hardcoding the duration here so it's dynamic based on the constant?
@@ -926,6 +930,17 @@ export class TransactionController extends BaseController< | |||
this.#checkForPendingTransactionAndStartPolling, | |||
); | |||
|
|||
new ResimulateHelper({ | |||
updateSimulationData: this.#updateSimulationData.bind(this), | |||
onStateChange: (listener) => { |
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.
Should we call this onTransactionsUpdate
for accuracy if we use a selector?
* @param transactionId - The ID of the transaction to update. | ||
* @param isFocused - The new focus state. | ||
*/ | ||
updateTransactionFocus(transactionId: string, isFocused: boolean) { |
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.
setTransactionActive
as per my other comment about decoupling?
isFocused, | ||
}; | ||
|
||
this.updateTransaction( |
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.
We want to exclusively use #updateTransactionInternal
going forward as it uses callbacks to update meaning there is no risk of race conditions from multiple updates.
It would also mean we don't need to query the existing state in this method.
Explanation
This PR adds
ResimulateHelper
, which focuses ontransactionMeta.isFocused
property and re-simulates transaction depending on that value.In order to capsulate re-simulation logic, this PR also relocates other utility functions under the new created
ResimulationHelper.ts
file.References
Fixes: https://github.com/MetaMask/MetaMask-planning/issues/3922
Extension PR: MetaMask/metamask-extension#29878
Changelog
@metamask/transaction-controller
isFocused
property ontransactionMeta
isFocused
property is expected to set by client.isFocused
istrue
.updateTransactionFocus
function to update theisFocused
property ontransactionMeta
.Checklist