Skip to content

Instantly share code, notes, and snippets.

@aakashns
Last active March 23, 2017 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aakashns/6fe1d370c15e4961664874c0bc23bb5c to your computer and use it in GitHub Desktop.
Save aakashns/6fe1d370c15e4961664874c0bc23bb5c to your computer and use it in GitHub Desktop.
Working instance of linkStateWithPath
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