Last active
June 5, 2017 08:47
-
-
Save erezrokah/7d300dd5ba8b7b8379b1a9e1a15fdd56 to your computer and use it in GitHub Desktop.
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
//@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