Skip to content

Instantly share code, notes, and snippets.

@phaistonian
Last active November 10, 2015 08:17
Show Gist options
  • Save phaistonian/2542563e6e5d7e5a2f97 to your computer and use it in GitHub Desktop.
Save phaistonian/2542563e6e5d7e5a2f97 to your computer and use it in GitHub Desktop.
A Redux middleware to broadcast state (actions + data) using Firebase (http://d.pr/v/13oMK)
import Rebase from 're-base';
const endPoint = 'actions';
const firebaseUrl = 'https://firesync.firebaseio.com';
const client = String(Date.now() + Math.random() * 100);
const data = [];
let initiated = false;
const base = Rebase.createClass(firebaseUrl);
function firesync (store) {
if (!initiated) {
base.listenTo(endPoint, {
context: {},
asArray: true,
then(newData) {
newData.filter(item => item.client !== client)
.forEach(item => {
store.dispatch({...item.data, client});
});
}
});
initiated = true;
}
return next => action => {
const value = next(action);
if (!value.client) {
base.post(endPoint, {
data: [...data, {
client,
ts: Date.now(),
data: value
}]
});
}
return value;
};
}
export default firesync;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment