Skip to content

Instantly share code, notes, and snippets.

@erezrokah
Last active June 5, 2017 08:47
Show Gist options
  • Save erezrokah/7d300dd5ba8b7b8379b1a9e1a15fdd56 to your computer and use it in GitHub Desktop.
Save erezrokah/7d300dd5ba8b7b8379b1a9e1a15fdd56 to your computer and use it in GitHub Desktop.
//@flow
import firebase from '../firebase'
export function listenRequested(metaType: string) {
return {
type: 'FIREBASE_LISTEN_REQUESTED',
metaType,
}
}
export function listenFulfilled(metaType: string, items: Object) {
return {
type: 'FIREBASE_LISTEN_FULFILLED',
metaType,
items
}
}
type Action = {
type: string,
metaType: string,
items?: Object
}
export function listenToPath(metaType: string, path: string) {
return (dispatch: (Action => void)) => {
dispatch(listenRequested(metaType))
firebase.database().ref(path)
.on('value', (snap) => {
dispatch(listenFulfilled(metaType, snap.val()))
})
}
}
export function listenToMessages() {
return listenToPath('messages', 'messages')
}
export function listenToUserContacts(uid: string) {
return listenToPath('userContacts', `users/${uid}/contacts`)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment