Merge pull request #3975 from MetaMask/i3947-composableStore
Add ComposableObservableStore for subscription managementfeature/default_network_editable
commit
3afe76bcba
@ -0,0 +1,49 @@ |
||||
const ObservableStore = require('obs-store') |
||||
|
||||
/** |
||||
* An ObservableStore that can composes a flat |
||||
* structure of child stores based on configuration |
||||
*/ |
||||
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) { |
||||
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) { |
||||
flatState = { ...flatState, ...this.config[key].getState() } |
||||
} |
||||
return flatState |
||||
} |
||||
} |
||||
|
||||
module.exports = ComposableObservableStore |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,5 @@ |
||||
require('babel-register')({ |
||||
ignore: name => name.includes('node_modules') && !name.includes('obs-store'), |
||||
}) |
||||
|
||||
require('./helper') |
@ -0,0 +1,35 @@ |
||||
const assert = require('assert') |
||||
const ComposableObservableStore = require('../../app/scripts/lib/ComposableObservableStore') |
||||
const ObservableStore = require('obs-store') |
||||
|
||||
describe('ComposableObservableStore', () => { |
||||
it('should register initial state', () => { |
||||
const store = new ComposableObservableStore('state') |
||||
assert.strictEqual(store.getState(), 'state') |
||||
}) |
||||
|
||||
it('should register initial structure', () => { |
||||
const testStore = new ObservableStore() |
||||
const store = new ComposableObservableStore(null, { TestStore: testStore }) |
||||
testStore.putState('state') |
||||
assert.deepEqual(store.getState(), { TestStore: 'state' }) |
||||
}) |
||||
|
||||
it('should update structure', () => { |
||||
const testStore = new ObservableStore() |
||||
const store = new ComposableObservableStore() |
||||
store.updateStructure({ TestStore: testStore }) |
||||
testStore.putState('state') |
||||
assert.deepEqual(store.getState(), { TestStore: 'state' }) |
||||
}) |
||||
|
||||
it('should return flattened state', () => { |
||||
const fooStore = new ObservableStore({ foo: 'foo' }) |
||||
const barStore = new ObservableStore({ bar: 'bar' }) |
||||
const store = new ComposableObservableStore(null, { |
||||
FooStore: fooStore, |
||||
BarStore: barStore, |
||||
}) |
||||
assert.deepEqual(store.getFlatState(), { foo: 'foo', bar: 'bar' }) |
||||
}) |
||||
}) |
Loading…
Reference in new issue