问题
First of all, I am new to pouchdb. What I want to do is I want to display the sync progress status to user. I have localdb and remotedb. Currently I can only display the item that is already sync by console only. But the problem is, I need to display to user. For example, if the data in remotedb is 1000, I want to show the sync status progress like 4/1000 and will increase until 1000/1000. Below is my code.
//declaration counter
let counter:number = 0;
this.db = new PouchDB('users'); //localdb
this.remote = http://localhost:5984/users; //remotedb
let options = {
live: true,
retry: true,
continuous: true
};
this.db.sync(this.remote, options)
.on('change', function(change){
counter++; //to count how many data is sync
console.log('Data sync', counter);
console.log('Users provider change!', change);
})
.on('paused', function(info){
console.log('Users provider paused!', info);
})
.on('active', function(info){
console.log('Users provider active!', info);
})
.on('error', function(err){
console.log('users provider error!', err)
});
Sorry for my bad English.
回答1:
With CouchDB, you could have simply use the active tasks endpoint.
For CouchDB, you will need to work with the db.info() and onChange event of the replication.
First, as I commented, remove the continuous option in your replication. Otherway, your replication would be infinite. In your case, you want a single shot replication.
To calculate a progress, you will need to count the number of sequence from your onChange response. Divide that number of the sequence by the number of sequences of the remote database and you should have a progress pretty much accurate.
Take a look at this example, it might help you to find a way to calculate the progress.
回答2:
I have an application where I set up first a one-shot replication to copy the whole remote CouchDB database to a local PouchDB, and then I set up a continuous replication.
First I count the number of distant documents :
let doc_count = 0;
this.remoteDB.info()
.then((infos:any) => {
doc_count = infos.doc_count;
}).catch((error) => {
console.error(error);
});
And then I count the local number of documents with a setInterval :
let interval = setInterval(() => {
this.pouchBase.info()
.then((infos: any) => {
let count = doc_count ? Math.round((infos.doc_count / doc_count) * 100) : 0;
if (infos && infos.doc_count === doc_count) {
clearInterval(interval);
}
// do something with doc_count and count
});
});
}, 500);
来源:https://stackoverflow.com/questions/40857758/pouchdb-how-to-display-sync-progress-status