commit 0305fc4072d6a95d3f8feab34819f5eca0b6ce94
parent 448c1c6b9f4329633146dd7add235770b7173e82
Author: mokou <mokou@posteo.de>
Date: Thu, 30 Jul 2020 05:42:52 +0200
fix(couch): Remove async sync handler
Diffstat:
3 files changed, 29 insertions(+), 23 deletions(-)
diff --git a/src/couch.js b/src/couch.js
@@ -30,13 +30,12 @@ export async function replicateToCouch() {
}
}
-export async function syncDoc(id) {
+export function syncDoc(id) {
if (get(isPouchAccessible)) {
- const db = await pouch()
- return db.sync(process.env.KORO_COUCHDB_URL, {
+ return PouchDB.sync('koro', process.env.KORO_COUCHDB_URL, {
live: true,
retry: true,
- selector: { _id: { $eq: id } }
+ doc_ids: [id]
})
}
}
diff --git a/src/pages/Event.svelte b/src/pages/Event.svelte
@@ -117,7 +117,7 @@
// 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 = await syncDoc(params.id)
+ live = syncDoc(params.id)
live.on('change', async (change) => {
// If a change to the document is detected, pull from the PouchDB, triggering
// a rerender.
@@ -134,7 +134,9 @@
})
onDestroy(() => {
- live.cancel()
+ if (live) {
+ live.cancel()
+ }
})
</script>
diff --git a/src/pages/Poll.svelte b/src/pages/Poll.svelte
@@ -27,16 +27,7 @@
format: 'hex',
alpha: 1
})
- if (poll.responses) {
- votes = poll.answers.map((_a, i) => {
- return Object.values(poll.responses).reduce((acc, j) => {
- if (poll.isMulti) {
- return j.includes(Number(i)) ? acc + 1 : acc
- }
- return i === j ? acc + 1 : acc
- }, 0)
- })
- }
+ calcVotes()
loading = false
document.title = `${poll.question} - Koro`
} catch (e) {
@@ -49,6 +40,19 @@
return `background-color: ${colors[i]}; color: ${contrast(colors[i])}`
}
+ function calcVotes() {
+ if (poll.responses) {
+ votes = poll.answers.map((_a, i) => {
+ return Object.values(poll.responses).reduce((acc, j) => {
+ if (poll.isMulti) {
+ return j.includes(Number(i)) ? acc + 1 : acc
+ }
+ return i === j ? acc + 1 : acc
+ }, 0)
+ })
+ }
+ }
+
function answerClick(i) {
return () => {
if (poll.isMulti && multiSelection.includes(i)) {
@@ -89,7 +93,7 @@
return votes[i]
}
- function totalVotes(i) {
+ function totalVotes() {
return votes.reduce((acc, j) => acc + j)
}
@@ -131,14 +135,13 @@
}
// Set up replication
- live = await syncDoc(params.id)
+ live = syncDoc(params.id)
live
.on('change', async (change) => {
const db = await pouch()
if (change.direction === 'pull') {
- getPoll().catch((err) => {
- errorFlash = `Couldn't find the poll. You might be offline!`
- })
+ poll = change.change.docs[0]
+ calcVotes()
}
})
.on('error', (err) => {
@@ -147,7 +150,9 @@
})
onDestroy(() => {
- live.cancel()
+ if (live) {
+ live.cancel()
+ }
})
</script>
@@ -181,7 +186,7 @@
class="rounded-b mb-4 py-3 px-3 text-center"
class:bg-green-500={hasMostVotes(i)}
class:bg-red-500={!hasMostVotes(i)}>
- {countVotes(i)}/{totalVotes(i)} votes
+ {countVotes(i)} votes
</div>
{/each}
</div>