aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/observable/util/sync.js
blob: c61feb02e93fde2519ac32c9bdb6d83aa133a689 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

//
// synchronizeStore(inStore, outStore, stateTransform)
//
// keeps outStore synchronized with inStore, via an optional stateTransform
//

module.exports = synchronizeStore


function synchronizeStore(inStore, outStore, stateTransform) {
  stateTransform = stateTransform || transformNoop
  const initState = stateTransform(inStore.get())
  outStore.put(initState)
  inStore.subscribe((inState) => {
    const outState = stateTransform(inState)
    outStore.put(outState)
  })
  return outStore
}

function transformNoop(state) {
  return state
}