@ -13,8 +13,10 @@ export default class ComposableObservableStore extends ObservableStore {
* /
* /
constructor ( initState , config ) {
constructor ( initState , config ) {
super ( initState ) ;
super ( initState ) ;
if ( config ) {
this . updateStructure ( config ) ;
this . updateStructure ( config ) ;
}
}
}
/ * *
/ * *
* Composes a new internal store subscription structure
* Composes a new internal store subscription structure
@ -24,14 +26,12 @@ export default class ComposableObservableStore extends ObservableStore {
updateStructure ( config ) {
updateStructure ( config ) {
this . config = config ;
this . config = config ;
this . removeAllListeners ( ) ;
this . removeAllListeners ( ) ;
for ( const key in config ) {
for ( const key of Object . keys ( this . config ) ) {
if ( Object . prototype . hasOwnProperty . call ( config , key ) ) {
config [ key ] . subscribe ( ( state ) => {
config [ key ] . subscribe ( ( state ) => {
this . updateState ( { [ key ] : state } ) ;
this . updateState ( { [ key ] : state } ) ;
} ) ;
} ) ;
}
}
}
}
}
/ * *
/ * *
* Merges all child store state into a single object rather than
* Merges all child store state into a single object rather than
@ -40,16 +40,17 @@ export default class ComposableObservableStore extends ObservableStore {
* @ returns { Object } Object containing merged child store state
* @ returns { Object } Object containing merged child store state
* /
* /
getFlatState ( ) {
getFlatState ( ) {
if ( ! this . config ) {
return { } ;
}
let flatState = { } ;
let flatState = { } ;
for ( const key in this . config ) {
for ( const key of Object . keys ( this . config ) ) {
if ( Object . prototype . hasOwnProperty . call ( this . config , key ) ) {
const controller = this . config [ key ] ;
const controller = this . config [ key ] ;
const state = controller . getState
const state = controller . getState
? controller . getState ( )
? controller . getState ( )
: controller . state ;
: controller . state ;
flatState = { ... flatState , ... state } ;
flatState = { ... flatState , ... state } ;
}
}
}
return flatState ;
return flatState ;
}
}
}
}