PouchDB 同步取消并重启 [英] PouchDB sync cancel and restart
问题描述
我在应用程序启动时建立同步(使用事件侦听器),我需要停止它并再次运行.有一个 .cancel() 函数可以工作,但我如何重新启动同步?
I'm establishing sync (with event listeners) on application start and I need to stop it and run again. There is a .cancel() function that works, but how I can restart the sync?
我尝试在 cancel() 之后删除从 sync() 函数返回的对象并再次运行 sync() ,但它有时会引发警告pouchdb-6.1.2.min.js:7 (node) 警告:检测到可能的 EventEmitter 内存泄漏.添加了 11 个侦听器.使用 Emitter.setMaxListeners() 增加限制.".据我所知,cancel() 不会删除我的旧听众.
I've tried to delete object returned from sync() function after cancel() and run sync() again , but it sometimes raises a warning "pouchdb-6.1.2.min.js:7 (node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.". As I get it, my old listeners are not removed by cancel().
如何正确停止和开始同步?
How can I stop and start sync correctly?
推荐答案
我认为正确的方法如下:
I believe the correct way is the following:
var db = new PouchDB('local');
var remoteDb = new PouchDB('URL');
var sync = db.sync(remoteDb, {live: true});
每当您想重新启动同步时,您都可以调用以下函数:
Whenever you want to restart sync you call the following function:
function restartSync() {
sync.on('complete', () => {
sync = db.sync(remoteDb, {live: true});
});
sync.cancel();
}
来源:https://pouchdb.com/guides/replication.html#canceling—复制
这篇关于PouchDB 同步取消并重启的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!