aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/ComposableObservableStore.js
diff options
context:
space:
mode:
authorDan <danjm.com@gmail.com>2018-04-20 23:53:17 +0800
committerDan <danjm.com@gmail.com>2018-04-20 23:53:17 +0800
commit71b0de76ffdbdfc0ae696a009d5ee34971541e0b (patch)
treed0f2f4fd891a20eb7cbe5e7bfb82a375286fbc43 /app/scripts/lib/ComposableObservableStore.js
parent9f12c26d44a0d78f28af25056857b993f80bbd95 (diff)
parent00efcf9e8ba34d448b628c98d32ad12d5be2ffc9 (diff)
downloadtangerine-wallet-browser-71b0de76ffdbdfc0ae696a009d5ee34971541e0b.tar
tangerine-wallet-browser-71b0de76ffdbdfc0ae696a009d5ee34971541e0b.tar.gz
tangerine-wallet-browser-71b0de76ffdbdfc0ae696a009d5ee34971541e0b.tar.bz2
tangerine-wallet-browser-71b0de76ffdbdfc0ae696a009d5ee34971541e0b.tar.lz
tangerine-wallet-browser-71b0de76ffdbdfc0ae696a009d5ee34971541e0b.tar.xz
tangerine-wallet-browser-71b0de76ffdbdfc0ae696a009d5ee34971541e0b.tar.zst
tangerine-wallet-browser-71b0de76ffdbdfc0ae696a009d5ee34971541e0b.zip
Merge branch 'master' into dm-docs-2
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