Skip to content

Instantly share code, notes, and snippets.

@kazuooooo
Created February 5, 2022 15:33
Show Gist options
  • Save kazuooooo/6379aa73b40c19c1cd27a0b7890a73a8 to your computer and use it in GitHub Desktop.
Save kazuooooo/6379aa73b40c19c1cd27a0b7890a73a8 to your computer and use it in GitHub Desktop.
store.sync = (key, ref) => {
// Document
if (ref instanceof DocumentReference) {
return onSnapshot(ref, (ds) => {
if (ds.exists()) {
store.$patch({ [key]: ds.data() })
}
})
}
// Collection or Query
return onSnapshot(ref, (qs) => {
const datum = qs.docs.map(d => d.data())
store.$patch((state) => {
state[key] = datum
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment