From 4780f825b1bfb33f03c60133f293b122b8b43be4 Mon Sep 17 00:00:00 2001 From: bitpshr Date: Thu, 12 Apr 2018 23:26:50 -0400 Subject: Add ComposableObservableStore for subscription management --- app/scripts/lib/ComposableObservableStore.js | 49 ++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 app/scripts/lib/ComposableObservableStore.js (limited to 'app/scripts/lib/ComposableObservableStore.js') diff --git a/app/scripts/lib/ComposableObservableStore.js b/app/scripts/lib/ComposableObservableStore.js new file mode 100644 index 000000000..688594b6d --- /dev/null +++ b/app/scripts/lib/ComposableObservableStore.js @@ -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() + 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 -- cgit From 8974f933fc97a37f5cd8dcd510ff0e6dc21d6751 Mon Sep 17 00:00:00 2001 From: bitpshr Date: Fri, 13 Apr 2018 13:13:36 -0400 Subject: Add tests for ComposableObservableStore --- app/scripts/lib/ComposableObservableStore.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'app/scripts/lib/ComposableObservableStore.js') diff --git a/app/scripts/lib/ComposableObservableStore.js b/app/scripts/lib/ComposableObservableStore.js index 688594b6d..d5ee708a1 100644 --- a/app/scripts/lib/ComposableObservableStore.js +++ b/app/scripts/lib/ComposableObservableStore.js @@ -11,24 +11,24 @@ class ComposableObservableStore extends ObservableStore { * @param {Object} [initState] - The initial store state * @param {Object} [config] - Map of internal state keys to child stores */ - constructor (initState, config) { - super() - this.updateStructure(config) - } + 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) { + updateStructure (config) { this.config = config - this.removeAllListeners() - for (const key in config) { - config[key].subscribe((state) => { - this.updateState({ [key]: state }) - }) - } + this.removeAllListeners() + for (const key in config) { + config[key].subscribe((state) => { + this.updateState({ [key]: state }) + }) + } } /** -- cgit