1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-01 21:57:06 +01:00
metamask-extension/app/scripts/lib/ComposableObservableStore.js
Erik Marks 76a2a9bb8b
@metamask/eslint config@5.0.0 (#10358)
* @metamask/eslint-config@5.0.0
* Update eslintrc and prettierrc
* yarn lint:fix
2021-02-04 10:15:23 -08:00

56 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);
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 in config) {
if (Object.prototype.hasOwnProperty.call(config, key)) {
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() {
let flatState = {};
for (const key in this.config) {
if (Object.prototype.hasOwnProperty.call(this.config, key)) {
const controller = this.config[key];
const state = controller.getState
? controller.getState()
: controller.state;
flatState = { ...flatState, ...state };
}
}
return flatState;
}
}