自从Electron 9.x以来,没有PouchDB-CouchDB复制

问题描述

我有一个Electron应用程序,可以从本地CouchDB进行实时复制。从Electron 9.x开始,一旦我在应用程序中加载新的html页面,该复制就会停止。

代码基于this brilliant script,并且只要不重新加载页面即可使用。

const PouchDB = require('pouchdb-browser');
const db = new PouchDB('TestDB',{adapter: 'idb'});
const remoteDB = new PouchDB('http://user:password@192.168.1.130:5984/dbname',{skip_setup: true});

db.sync(remoteDB,{
    live: true,retry: true
}).on('change',function (change) {
    console.log(change);
}).on('paused',function (info) {
    console.log(info)
}).on('active',function (info) {
    console.log(info);
}).on('complete',function (info) {
    console.log(info);
}).on('error',function (err) {
});

let docs;

fetchInitialDocs().then(renderDocsSomehow).then(reacttochanges).catch(console.log.bind(console));

function fetchInitialDocs() {
    return db.allDocs({
        include_docs: true,}).then(function (result) {
        docs = result.rows.map(function (row) {
            return row.doc;
        });
    }).catch(function (err) {
    })

}

function renderDocsSomehow() {
  //my output
}

function reacttochanges() {
    db.changes({live: true,since: 'Now',include_docs: true}).on('change',function (change) {

        if (change.deleted) {
            onDeleted(change.id);
        } else { 
            onUpdatedOrInserted(change.doc);
        }
    }).on('error',console.log.bind(console));
}

function binarySearch(arr,docId) {
    let low = 0,high = arr.length,mid;
    while (low < high) {
        mid = (low + high) >>> 1; 
        if (arr[mid]._id < docId) {
            low = mid + 1
        } else {
            high = mid
        }
    }
    return low;
}

function onDeleted(id) {
    let index = binarySearch(docs,id);
    let doc = docs[index];
    if (doc && doc._id === id) {
        docs.splice(index,1);
    }
}

function onUpdatedOrInserted(newDoc) {
    let index = binarySearch(docs,newDoc._id);
    let doc = docs[index];
    if (doc && doc._id === newDoc._id) {
        docs[index] = newDoc;
    } else {
        docs.splice(index,newDoc);
    }
}

我的CouchDB启用了CORS(因为我感觉它与它有关),并且我也尝试对main.js使用app.commandLine.appendSwitch('disable-features','OutOfBlinkCors');webSecurity : false,但这并没有改变。

如前所述-复制在应用程序启动时效果很好,但在刷新页面或更改为新页面(使用相同的复制脚本)时停止。尽管fetchInitialDocs()函数始终有效。

有人知道这里出了什么问题吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)