import PouchDB from 'pouchdb-browser' ; import PouchDBFind from 'pouchdb-find' ; const dbBaseName = window . location . pathname . replace ( /\//g , '_' ) ; PouchDB . plugin ( PouchDBFind ) ; let remoteDb = null ; let rep = null ; const localDb = new PouchDB ( `kanban-board-v1@${ dbBaseName }` , { auto_compaction : true } ) ; const localConfigDb = new PouchDB ( `kanban-board-config-v1@${ dbBaseName }` , { auto_compaction : true } ) ; async function startSync ( ) { if ( remoteDb ) { const localDocs = await localDb . allDocs ( { } ) ; const remoteDocs = remoteDb ? await remoteDb . allDocs ( { } ) : null ; const idSet = new Set < string > ( ) ; for ( const doc of localDocs . rows ) { idSet . add ( doc . id ) ; } if ( remoteDocs ) { for ( const doc of remoteDocs . rows ) { idSet . add ( doc . id ) ; } } rep = localDb . sync ( remoteDb , { live : true , retry : true , doc_ids : Array . from ( idSet . values ( ) ) , } ) . on ( 'change' , change => { } ) . on ( 'paused' , info => { } ) . on ( 'active' as any , info => { } ) . on ( 'complete' , info => { } ) . on ( 'error' , err => { } ) ; } } export async function restartSync ( ) { if ( rep ) { try { rep . cancel ( ) ; } catch ( e ) { } rep = null ; } await startSync ( ) ; } export async function setRemoteDb ( url , user , password ) { if ( rep ) { try { rep . cancel ( ) ; } catch ( e ) { } rep = null ; } if ( remoteDb ) { try { await remoteDb . close ( ) ; } catch ( e ) { } remoteDb = null ; } if ( url && url . match ( /^https?:\/\// ) ) { remoteDb = new PouchDB ( url , { auth : { username : user , password : password , } , } ) ; await startSync ( ) ; } } export function getLocalDb ( ) { return localDb ; } export function getLocalConfigDb ( ) { return localConfigDb ; }