mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-14 05:37:12 +01:00
352dbd8831
Three uses of `hasOwnProperty.call` have been replaced with simpler alternatives. I did this because I found this convention confusing. The `ComposableObservableStore` changes required adding a check for whether the `config` is defined, because the old syntax would allow iterating over `undefined` as if it was an empty object. Functionally it should be identical. A new test was added to highlight this functionality.
57 lines
1.5 KiB
JavaScript
57 lines
1.5 KiB
JavaScript
import { ObservableStore } from '@metamask/obs-store';
|
|
|
|
/**
|
|
* An ObservableStore that can composes a flat
|
|
* structure of child stores based on configuration
|
|
*/
|
|
export default class ComposableObservableStore extends ObservableStore {
|
|
/**
|
|
* Create a new store
|
|
*
|
|
* @param {Object} [initState] - The initial store state
|
|
* @param {Object} [config] - Map of internal state keys to child stores
|
|
*/
|
|
constructor(initState, config) {
|
|
super(initState);
|
|
if (config) {
|
|
this.updateStructure(config);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Composes a new internal store subscription structure
|
|
*
|
|
* @param {Object} [config] - Map of internal state keys to child stores
|
|
*/
|
|
updateStructure(config) {
|
|
this.config = config;
|
|
this.removeAllListeners();
|
|
for (const key of Object.keys(this.config)) {
|
|
config[key].subscribe((state) => {
|
|
this.updateState({ [key]: state });
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Merges all child store state into a single object rather than
|
|
* returning an object keyed by child store class name
|
|
*
|
|
* @returns {Object} Object containing merged child store state
|
|
*/
|
|
getFlatState() {
|
|
if (!this.config) {
|
|
return {};
|
|
}
|
|
let flatState = {};
|
|
for (const key of Object.keys(this.config)) {
|
|
const controller = this.config[key];
|
|
const state = controller.getState
|
|
? controller.getState()
|
|
: controller.state;
|
|
flatState = { ...flatState, ...state };
|
|
}
|
|
return flatState;
|
|
}
|
|
}
|