Last active
March 23, 2017 15:54
-
-
Save aakashns/6fe1d370c15e4961664874c0bc23bb5c to your computer and use it in GitHub Desktop.
Working instance of linkStateWithPath
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const linkStoreWithPath = (path, actionCreator, selector) => { | |
return (db, store) => { | |
let previous = selector(store.getState()); | |
let mustWrite = true; | |
const fromDb = (db, dispatch) => { | |
const listener = db.ref(path).on("value", snap => { | |
if (snap.val()) { | |
mustWrite = false; | |
dispatch(actionCreator(snap.val())); | |
mustWrite = true; | |
} | |
}); | |
return () => db.ref(path).of("value", listener); | |
}; | |
const fromStore = (state, db) => { | |
const portion = selector(state); | |
if (mustWrite && portion !== previous) { | |
db.ref(path).set(portion); | |
} | |
previous = portion; | |
}; | |
return linkStoreWithDb(fromDb, fromStore)(db, store); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment