Skip to content

Commit

Permalink
Added getPersistentState support for V2 controllers persist metad…
Browse files Browse the repository at this point in the history
…ata (#12007)
  • Loading branch information
shanejonas authored Sep 6, 2021
1 parent 49e1abc commit 362b54b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
11 changes: 9 additions & 2 deletions app/scripts/lib/ComposableObservableStore.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ObservableStore } from '@metamask/obs-store';
import { getPersistentState } from '@metamask/controllers';

/**
* @typedef {import('@metamask/controllers').ControllerMessenger} ControllerMessenger
Expand Down Expand Up @@ -27,9 +28,11 @@ export default class ComposableObservableStore extends ObservableStore {
* messenger, used for subscribing to events from BaseControllerV2-based
* controllers.
* @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);
this.persist = persist;
this.controllerMessenger = controllerMessenger;
if (config) {
this.updateStructure(config);
Expand Down Expand Up @@ -60,7 +63,11 @@ export default class ComposableObservableStore extends ObservableStore {
this.controllerMessenger.subscribe(
`${store.name}:stateChange`,
(state) => {
this.updateState({ [key]: state });
let updatedState = state;
if (this.persist) {
updatedState = getPersistentState(state, config[key].metadata);
}
this.updateState({ [key]: updatedState });
},
);
}
Expand Down
1 change: 1 addition & 0 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export default class MetamaskController extends EventEmitter {
this.store = new ComposableObservableStore({
state: initState,
controllerMessenger: this.controllerMessenger,
persist: true,
});

// external connections by origin
Expand Down

0 comments on commit 362b54b

Please sign in to comment.