Added `getPersistentState` support for V2 controllers `persist` metadata (#12007)

feature/default_network_editable
Shane 3 years ago committed by GitHub
parent 49e1abc374
commit 362b54b2dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      app/scripts/lib/ComposableObservableStore.js
  2. 1
      app/scripts/metamask-controller.js

@ -1,4 +1,5 @@
import { ObservableStore } from '@metamask/obs-store'; import { ObservableStore } from '@metamask/obs-store';
import { getPersistentState } from '@metamask/controllers';
/** /**
* @typedef {import('@metamask/controllers').ControllerMessenger} ControllerMessenger * @typedef {import('@metamask/controllers').ControllerMessenger} ControllerMessenger
@ -27,9 +28,11 @@ export default class ComposableObservableStore extends ObservableStore {
* messenger, used for subscribing to events from BaseControllerV2-based * messenger, used for subscribing to events from BaseControllerV2-based
* controllers. * controllers.
* @param {Object} [options.state] - The initial store state * @param {Object} [options.state] - The initial store state
* @param {boolean} [options.persist] - Wether or not to apply the persistence for v2 controllers
*/ */
constructor({ config, controllerMessenger, state }) { constructor({ config, controllerMessenger, state, persist }) {
super(state); super(state);
this.persist = persist;
this.controllerMessenger = controllerMessenger; this.controllerMessenger = controllerMessenger;
if (config) { if (config) {
this.updateStructure(config); this.updateStructure(config);
@ -60,7 +63,11 @@ export default class ComposableObservableStore extends ObservableStore {
this.controllerMessenger.subscribe( this.controllerMessenger.subscribe(
`${store.name}:stateChange`, `${store.name}:stateChange`,
(state) => { (state) => {
this.updateState({ [key]: state }); let updatedState = state;
if (this.persist) {
updatedState = getPersistentState(state, config[key].metadata);
}
this.updateState({ [key]: updatedState });
}, },
); );
} }

@ -114,6 +114,7 @@ export default class MetamaskController extends EventEmitter {
this.store = new ComposableObservableStore({ this.store = new ComposableObservableStore({
state: initState, state: initState,
controllerMessenger: this.controllerMessenger, controllerMessenger: this.controllerMessenger,
persist: true,
}); });
// external connections by origin // external connections by origin

Loading…
Cancel
Save