Skip to content

Commit

Permalink
Update EngineService to align with new ComposableController inter…
Browse files Browse the repository at this point in the history
…face
  • Loading branch information
MajorLift committed Nov 26, 2024
1 parent a7440f8 commit 7e2a0ab
Showing 1 changed file with 37 additions and 130 deletions.
167 changes: 37 additions & 130 deletions app/core/EngineService/EngineService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import UntypedEngine from '../Engine';
import AppConstants from '../AppConstants';
import { getVaultFromBackup } from '../BackupVault';
import { store as importedStore } from '../../store';
import Logger from '../../util/Logger';
Expand All @@ -10,6 +9,11 @@ import {
import { getTraceTags } from '../../util/sentry/tags';
import { trace, endTrace, TraceName, TraceOperation } from '../../util/trace';
import getUIStartupSpan from '../Performance/UIStartup';
import {
isBaseController,
isBaseControllerV1,
} from '@metamask/base-controller';
import { Controllers } from '../Engine/types';

interface InitializeEngineResult {
success: boolean;
Expand Down Expand Up @@ -58,141 +62,44 @@ class EngineService {
return;
}

const controllers = [
{
name: 'AddressBookController',
key: `${engine.context.AddressBookController.name}:stateChange`,
},
{ name: 'NftController', key: 'NftController:stateChange' },
{
name: 'TokensController',
key: `${engine.context.TokensController.name}:stateChange`,
},
{
name: 'KeyringController',
key: `${engine.context.KeyringController.name}:stateChange`,
},
{
name: 'AccountTrackerController',
key: 'AccountTrackerController:stateChange',
},
{
name: 'NetworkController',
key: AppConstants.NETWORK_STATE_CHANGE_EVENT,
},
{
name: 'PhishingController',
key: `${engine.context.PhishingController.name}:stateChange`,
},
{
name: 'PreferencesController',
key: `${engine.context.PreferencesController.name}:stateChange`,
},
{
name: 'SelectedNetworkController',
key: `${engine.context.SelectedNetworkController.name}:stateChange`,
},
{
name: 'TokenBalancesController',
key: `${engine.context.TokenBalancesController.name}:stateChange`,
},
{ name: 'TokenRatesController', key: 'TokenRatesController:stateChange' },
{
name: 'TransactionController',
key: `${engine.context.TransactionController.name}:stateChange`,
},
{
name: 'SmartTransactionsController',
key: `${engine.context.SmartTransactionsController.name}:stateChange`,
},
{
name: 'SwapsController',
key: `${engine.context.SwapsController.name}:stateChange`,
},
{
name: 'TokenListController',
key: `${engine.context.TokenListController.name}:stateChange`,
},
{
name: 'CurrencyRateController',
key: `${engine.context.CurrencyRateController.name}:stateChange`,
},
{
name: 'GasFeeController',
key: `${engine.context.GasFeeController.name}:stateChange`,
},
{
name: 'ApprovalController',
key: `${engine.context.ApprovalController.name}:stateChange`,
},
///: BEGIN:ONLY_INCLUDE_IF(preinstalled-snaps,external-snaps)
{
name: 'SnapController',
key: `${engine.context.SnapController.name}:stateChange`,
},
{
name: 'SubjectMetadataController',
key: `${engine.context.SubjectMetadataController.name}:stateChange`,
},
{
name: 'AuthenticationController',
key: 'AuthenticationController:stateChange',
},
{
name: 'UserStorageController',
key: 'UserStorageController:stateChange',
},
{
name: 'NotificationServicesController',
key: 'NotificationServicesController:stateChange',
},
{
name: 'NotificationServicesPushController',
key: 'NotificationServicesPushController:stateChange',
},
///: END:ONLY_INCLUDE_IF
{
name: 'PermissionController',
key: `${engine.context.PermissionController.name}:stateChange`,
},
{
name: 'LoggingController',
key: `${engine.context.LoggingController.name}:stateChange`,
},
{
name: 'AccountsController',
key: `${engine.context.AccountsController.name}:stateChange`,
},
{
name: 'PPOMController',
key: `${engine.context.PPOMController.name}:stateChange`,
// TODO: Optimize to only trigger on datamodel initialization instead of on every `ComposableController:stateChange` event, which will trigger every time any child controller's state is updated.
engine.controllerMessenger.subscribe(
'ComposableController:stateChange',
() => {
if (!engine.context.KeyringController.metadata.vault) {
Logger.log('keyringController vault missing for INIT_BG_STATE_KEY');
}
if (!this.engineInitialized) {
store.dispatch({ type: INIT_BG_STATE_KEY });
this.engineInitialized = true;
}
},
];
);

engine.controllerMessenger.subscribe('ComposableController:stateChange', () => {
const update_bg_state_cb = (name: string) => {
if (!engine.context.KeyringController.metadata.vault) {
Logger.log('keyringController vault missing for INIT_BG_STATE_KEY');
}
if (!this.engineInitialized) {
store.dispatch({ type: INIT_BG_STATE_KEY });
this.engineInitialized = true;
Logger.log('keyringController vault missing for UPDATE_BG_STATE_KEY');
}
});
store.dispatch({ type: UPDATE_BG_STATE_KEY, payload: { key: name } });
};

controllers.forEach((controller) => {
const { name, key = undefined } = controller;
const update_bg_state_cb = () => {
if (!engine.context.KeyringController.metadata.vault) {
Logger.log('keyringController vault missing for UPDATE_BG_STATE_KEY');
(Object.values(engine.context) as Controllers[keyof Controllers][]).forEach(
(controller) => {
// @ts-expect-error - TODO: remove this directive once `@metamask/base-controller` updates `isBaseController` input type to `unknown`
if (isBaseController(controller)) {
engine.controllerMessenger.subscribe(
`${controller.name}:stateChange`,
() => update_bg_state_cb(controller.name),
);
// TODO: Remove once it is confirmed that Engine context never interacts with any V1 controller throughout its lifecycle.
// @ts-expect-error - TODO: remove this directive once `@metamask/base-controller` updates `isBaseController` input type to `unknown`
} else if (isBaseControllerV1(controller)) {
engine.context[controller.name].subscribe(() =>
update_bg_state_cb(controller.name),
);
}
store.dispatch({ type: UPDATE_BG_STATE_KEY, payload: { key: name } });
};
if (key) {
engine.controllerMessenger.subscribe(key, update_bg_state_cb);
} else {
engine.context[name].subscribe(update_bg_state_cb);
}
});
},
);
};

/**
Expand Down

0 comments on commit 7e2a0ab

Please sign in to comment.