Pouchdb - 如何显示同步进度状态? [英] Pouchdb - How to display sync progress status?

查看:125
本文介绍了Pouchdb - 如何显示同步进度状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是pouchdb的新手。我想要做的是我想向用户显示同步进度状态。我有localdb和remotedb。目前我只能显示已经通过控制台同步的项目。但问题是,我需要向用户显示。例如,如果remotedb中的数据是1000,我想显示同步状态进度,如4/1000,并将增加到1000/1000。以下是我的代码。

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)
  });

抱歉我的英语不好。

推荐答案

使用CouchDB,您可以简单地使用活动任务端点

With CouchDB, you could have simply use the active tasks endpoint.

对于CouchDB,您需要使用复制的db.info()和onChange事件。

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.

要计算进度,您需要计算onChange响应中的序列数。将序列号除以远程数据库的序列数,你应该有一个非常准确的进度。

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.

这篇关于Pouchdb - 如何显示同步进度状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆