aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/ComposableObservableStore.js
diff options
context:
space:
mode:
authorDan <danjm.com@gmail.com>2018-04-19 02:45:48 +0800
committerDan <danjm.com@gmail.com>2018-04-19 02:45:48 +0800
commit649d2f8b229a6a749c0129dd5ef6dcf5b870e6c9 (patch)
treebc1e935331aa3678d18c2ffbcc04e791a711c0aa /app/scripts/lib/ComposableObservableStore.js
parente80bd230b9bb6ac9ff05d7095f74dd2fd7ebb3af (diff)
parent7e21fc2aa780ccb4ffb2f642156385db22c47a52 (diff)
downloadtangerine-wallet-browser-649d2f8b229a6a749c0129dd5ef6dcf5b870e6c9.tar
tangerine-wallet-browser-649d2f8b229a6a749c0129dd5ef6dcf5b870e6c9.tar.gz
tangerine-wallet-browser-649d2f8b229a6a749c0129dd5ef6dcf5b870e6c9.tar.bz2
tangerine-wallet-browser-649d2f8b229a6a749c0129dd5ef6dcf5b870e6c9.tar.lz
tangerine-wallet-browser-649d2f8b229a6a749c0129dd5ef6dcf5b870e6c9.tar.xz
tangerine-wallet-browser-649d2f8b229a6a749c0129dd5ef6dcf5b870e6c9.tar.zst
tangerine-wallet-browser-649d2f8b229a6a749c0129dd5ef6dcf5b870e6c9.zip
Merge branch 'master' into dm-docs-1
Diffstat (limited to 'app/scripts/lib/ComposableObservableStore.js')
-rw-r--r--app/scripts/lib/ComposableObservableStore.js49
1 files changed, 49 insertions, 0 deletions
diff --git a/app/scripts/lib/ComposableObservableStore.js b/app/scripts/lib/ComposableObservableStore.js
new file mode 100644
index 000000000..d5ee708a1
--- /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(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