aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/ComposableObservableStore.js
diff options
context:
space:
mode:
authorfrankiebee <frankie.diamond@gmail.com>2018-04-20 02:49:11 +0800
committerfrankiebee <frankie.diamond@gmail.com>2018-04-20 02:49:11 +0800
commitc20c97ce625207c7afcd48a12f6e7891d582ae99 (patch)
treeb5e063ee7cfd0d0e99fcdb0409fe3106c010bf79 /app/scripts/lib/ComposableObservableStore.js
parenteeb9390de81ce6fc92247d5c499e991dce8330bd (diff)
parent00efcf9e8ba34d448b628c98d32ad12d5be2ffc9 (diff)
downloadtangerine-wallet-browser-c20c97ce625207c7afcd48a12f6e7891d582ae99.tar.gz
tangerine-wallet-browser-c20c97ce625207c7afcd48a12f6e7891d582ae99.tar.zst
tangerine-wallet-browser-c20c97ce625207c7afcd48a12f6e7891d582ae99.zip
Merge branch 'master' into tx-controller-rewrite-v3
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