koro

an event time scheduler
git clone https://tilde.team/~marisa/repo/koro.git
Log | Files | Refs | README | LICENSE

commit 31895dddd4b149fd1ff5fd4ef1e4fcff3576c968
parent 48359488c2ad283b4064168803e8bfe7fd534624
Author: mokou <mokou@posteo.de>
Date:   Fri, 29 May 2020 22:10:35 +0200

feat(couch): Sync online-only if needed

This should close #6.

Diffstat:
Msrc/couch.js | 44++++++++++++++++++++++++++++++++------------
Msrc/pages/Event.svelte | 12++++++------
Msrc/pages/Index.svelte | 2+-
Msrc/stores.js | 1+
4 files changed, 40 insertions(+), 19 deletions(-)

diff --git a/src/couch.js b/src/couch.js @@ -1,21 +1,41 @@ import PouchDB from 'pouchdb-browser' +import { isPouchAccessible } from './stores' +import { get } from 'svelte/store' -export function pouch () { - return new PouchDB('koro', { - auto_compaction: true +export async function pouch () { + // We obtain the database, and then check if we can access it. + const local = new PouchDB('koro', { + auto_compation: true }) + + try { + await local.info() + return local + } catch (e) { + // For some reason, we cannot access the local PouchDB. This usually means + // that IndexedDB is acting weird, and in this case, we use a online-only + // CouchDB endpoint. + isPouchAccessible.set(false) + return new PouchDB(process.env.KORO_COUCHDB_URL, { + auto_compaction: true + }) + } } export async function replicateToCouch() { - PouchDB.replicate('koro', process.env.KORO_COUCHDB_URL, { live: false }) - .on('complete', (info) => info) - .on('error', (err) => new Error(err)) + if (get(isPouchAccessible)) { + PouchDB.replicate('koro', process.env.KORO_COUCHDB_URL, { live: false }) + .on('complete', (info) => info) + .on('error', (err) => new Error(err)) + } } -export function syncDoc(id, onChange, onError) { - const db = pouch() - return db.sync(process.env.KORO_COUCHDB_URL, { - live: true, - selector: { _id: { $eq: id } } - }) +export async function syncDoc(id, onChange, onError) { + if (get(isPouchAccessible)) { + const db = await pouch() + return db.sync(process.env.KORO_COUCHDB_URL, { + live: true, + selector: { _id: { $eq: id } } + }) + } } diff --git a/src/pages/Event.svelte b/src/pages/Event.svelte @@ -34,7 +34,7 @@ // TODO: Sync the document so we can make sure we have the // most recent available version, which would reduce conflicts. - const db = pouch() + const db = await pouch() let responsesToPut = event.responses || new Object() responsesToPut[name] = responses try { @@ -81,7 +81,7 @@ } async function getEvent() { - const db = pouch() + const db = await pouch() try { event = await db.get(params.id) loading = false @@ -113,11 +113,11 @@ // 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) - live.on('change', change => { + live = await syncDoc(params.id) + live.on('change', async change => { // If a change to the document is detected, pull from the PouchDB, triggering // a rerender. - const db = pouch() + const db = await pouch() if (change.direction === 'pull') { getEvent().catch(err => { error = `Couldn't find the event. You might be offline!` @@ -135,7 +135,7 @@ </script> {#if loading} - Loading event information... + Loading event information. You might need to refresh the page. {:else if !loading && error} {error} {:else} diff --git a/src/pages/Index.svelte b/src/pages/Index.svelte @@ -58,7 +58,7 @@ return } - const db = pouch() + const db = await pouch() try { const res = await db.put({ times: event.times.map((e) => day(e).utc().format()), diff --git a/src/stores.js b/src/stores.js @@ -4,6 +4,7 @@ const localStorage = window.localStorage export const hasResponded = localStorageStore('hasResponded') export const is24Hrs = localStorageStore('is24Hrs') +export const isPouchAccessible = writable(true) function localStorageStore(key) { const item = localStorage.getItem(key)