1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-23 03:36:18 +02:00
metamask-extension/app/scripts/lib/ComposableObservableStore.js
Mark Stacey 14d85b1332
Make JSDoc formatting more consistent (#9796)
A few inconsistencies in JSDoc formatting have been fixed throughout
the project. Many issues remain; these were just the few things that
were easy to fix with a regular expression.

The changes include:

* Using lower-case for primitive types, but capitalizing non-primitive
 types
* Separating the parameter identifier and the description with a dash
* Omitting a dash between the return type and the return description
* Ensuring the parameter type is first and the identifier is second (in
 a few places it was backwards)
* Using square brackets to denote when a parameter is optional, rather
 than putting "(optional)" in the parameter description
* Including a type and identifier with every parameter
* Fixing inconsistent spacing, except where it's used for alignment
* Remove incorrectly formatted `@deprecated` tags that reference non-
 existent properties
* Remove lone comment block without accompanying function

Additionally, one parameter was renamed for clarity.
2020-11-10 14:00:41 -03:30

56 lines
1.5 KiB
JavaScript

import ObservableStore from '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
}
}