commit b461804801daabfacd9a646e165a660c8b0ef3fa
parent 2a3706d6818be8587dfa921b334449316f09c632
Author: mokou <mokou@posteo.de>
Date: Thu, 21 May 2020 17:07:05 +0200
chore: Document and lint
Diffstat:
2 files changed, 14 insertions(+), 6 deletions(-)
diff --git a/src/couch.js b/src/couch.js
@@ -1,15 +1,14 @@
import PouchDB from 'pouchdb-browser'
-export async function replicateToCouch () {
+export async function replicateToCouch() {
PouchDB.replicate('koro', process.env.KORO_COUCHDB_URL, { live: false })
- .on('complete', info => info)
- .on('error', err => new Error(err))
+ .on('complete', (info) => info)
+ .on('error', (err) => new Error(err))
}
-export function syncDoc (id, onChange) {
+export function syncDoc(id, onChange) {
return PouchDB.sync('koro', process.env.KORO_COUCHDB_URL, {
live: true,
selector: { _id: { $eq: id } }
- })
- .on('change', onChange)
+ }).on('change', onChange)
}
diff --git a/src/pages/Event.svelte b/src/pages/Event.svelte
@@ -94,10 +94,19 @@
}
onMount(async () => {
+ // We make sure our changes are in the Couch, just in case we're coming
+ // back from being offline.
await replicateToCouch()
+ // Then, we set up a two-way replication (a sync) between specifically the
+ // current document and the local PouchDB. We only sync the current document
+ // because syncing the whole DB would be completely overkill, and we only
+ // really need to know about the documents that we can see.
live = syncDoc(params.id, () => {
+ // If a change to the document is detected, pull from the PouchDB, triggering
+ // a rerender.
getEvent()
})
+ // Fetch document details from the local PouchDB once.
await getEvent()
})
</script>