Skip to content

Commit

Permalink
upgrade preferences controller to base controller v2
Browse files Browse the repository at this point in the history
  • Loading branch information
kanthesha committed Sep 20, 2024
1 parent 27d8a54 commit 3da007e
Show file tree
Hide file tree
Showing 14 changed files with 684 additions and 528 deletions.
5 changes: 2 additions & 3 deletions app/scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ function maybeDetectPhishing(theController) {
return {};
}

const prefState = theController.preferencesController.store.getState();
const prefState = theController.preferencesController.state;
if (!prefState.usePhishDetect) {
return {};
}
Expand Down Expand Up @@ -756,8 +756,7 @@ export function setupController(
controller.preferencesController,
),
getUseAddressBarEnsResolution: () =>
controller.preferencesController.store.getState()
.useAddressBarEnsResolution,
controller.preferencesController.state.useAddressBarEnsResolution,
provider: controller.provider,
});

Expand Down
15 changes: 8 additions & 7 deletions app/scripts/controllers/app-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default class AppStateController extends EventEmitter {
isUnlocked,
initState,
onInactiveTimeout,
preferencesStore,
preferences,
messenger,
extension,
} = opts;
Expand Down Expand Up @@ -83,11 +83,13 @@ export default class AppStateController extends EventEmitter {
this.waitingForUnlock = [];
addUnlockListener(this.handleUnlock.bind(this));

preferencesStore.subscribe(({ preferences }) => {
const currentState = this.store.getState();
if (currentState.timeoutMinutes !== preferences.autoLockTimeLimit) {
this._setInactiveTimeout(preferences.autoLockTimeLimit);
}
messenger.subscribe(
'PreferencesController:stateChange',
({ preferences }) => {
const currentState = this.store.getState();
if (currentState.timeoutMinutes !== preferences.autoLockTimeLimit) {
this._setInactiveTimeout(preferences.autoLockTimeLimit);
}
});

messenger.subscribe(
Expand All @@ -98,7 +100,6 @@ export default class AppStateController extends EventEmitter {
}),
);

const { preferences } = preferencesStore.getState();
this._setInactiveTimeout(preferences.autoLockTimeLimit);

this.messagingSystem = messenger;
Expand Down
17 changes: 11 additions & 6 deletions app/scripts/controllers/metametrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,10 @@ export default class MetaMetricsController {
* @param {object} options
* @param {object} options.segment - an instance of analytics for tracking
* events that conform to the new MetaMetrics tracking plan.
* @param {object} options.preferencesStore - The preferences controller store, used
* @param {object} options.preferencesControllerState - The preferences controller state
* to access and subscribe to preferences that will be attached to events
* @param {Function} options.onPreferencesControllerStateChange - Used to attach a listener to the
* networkDidChange event emitted by the networkController
* @param {Function} options.onNetworkDidChange - Used to attach a listener to the
* networkDidChange event emitted by the networkController
* @param {Function} options.getCurrentChainId - Gets the current chain id from the
Expand All @@ -121,7 +123,7 @@ export default class MetaMetricsController {
*/
constructor({
segment,
preferencesStore,
preferencesController,
onNetworkDidChange,
getCurrentChainId,
version,
Expand All @@ -137,16 +139,15 @@ export default class MetaMetricsController {
captureException(err);
}
};
const prefState = preferencesStore.getState();
this.chainId = getCurrentChainId();
this.locale = prefState.currentLocale.replace('_', '-');
this.locale = preferencesController.state.currentLocale.replace('_', '-');
this.version =
environment === 'production' ? version : `${version}-${environment}`;
this.extension = extension;
this.environment = environment;

///: BEGIN:ONLY_INCLUDE_IF(build-mmi)
this.selectedAddress = prefState.selectedAddress;
this.selectedAddress = preferencesController.state.selectedAddress;
///: END:ONLY_INCLUDE_IF

const abandonedFragments = omitBy(initState?.fragments, 'persist');
Expand All @@ -170,10 +171,14 @@ export default class MetaMetricsController {
},
});

preferencesStore.subscribe(({ currentLocale }) => {
preferencesController.onStateChange(({ currentLocale }) => {
this.locale = currentLocale.replace('_', '-');
});

// preferencesStore.subscribe(({ currentLocale }) => {
// this.locale = currentLocale.replace('_', '-');
// });

onNetworkDidChange(() => {
this.chainId = getCurrentChainId();
});
Expand Down
Loading

0 comments on commit 3da007e

Please sign in to comment.