Last active
November 10, 2015 08:17
-
-
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)
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
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