Moving sync back in the first worker (sync doesn't see updates done in other workers!)
This commit is contained in:
1 parent
51250c1880
commit
0d86b789c1
3 files changed
+108
-49
No files matched your search
@@ -17,6 +17,8 @@ declare global {
|
||||
var localDb: any;
|
||||
var db: any;
|
||||
var dbReady: boolean;
|
||||
var currentAccount: any;
|
||||
var sync: any;
|
||||
}
|
||||
|
||||
export const initDb = async () => {
|
||||
@@ -27,6 +29,56 @@ export const initDb = async () => {
|
||||
|
||||
await openDatabases();
|
||||
|
||||
globalThis.currentAccount = await openDatabases();
|
||||
const currentAccount = globalThis.currentAccount;
|
||||
|
||||
if (currentAccount.remoteDbServer) {
|
||||
const url = `${currentAccount.remoteDbServer}/userdb-${toHex(
|
||||
currentAccount.remoteDbUser
|
||||
)}`;
|
||||
|
||||
const remoteDb = new PouchDB(url, {
|
||||
auth: {
|
||||
username: currentAccount.remoteDbUser,
|
||||
password: currentAccount.remoteDbPassword,
|
||||
},
|
||||
skip_setup: true,
|
||||
});
|
||||
globalThis.remoteDb = remoteDb;
|
||||
globalThis.sync = PouchDB.sync(db, remoteDb, {
|
||||
live: true,
|
||||
retry: true,
|
||||
});
|
||||
}
|
||||
|
||||
if (globalThis.sync) {
|
||||
globalThis.sync
|
||||
.on('change', function (info) {
|
||||
// handle change
|
||||
console.log({ caller: 'Sync / change', info });
|
||||
})
|
||||
.on('paused', function (err) {
|
||||
// replication paused (e.g. replication up to date, user went offline)
|
||||
console.log({ caller: 'Sync / paused', err });
|
||||
})
|
||||
.on('active', function () {
|
||||
// replicate resumed (e.g. new changes replicating, user went back online)
|
||||
console.log({ caller: 'Sync / active' });
|
||||
})
|
||||
.on('denied', function (err) {
|
||||
// a document failed to replicate (e.g. due to permissions)
|
||||
console.error({ caller: 'Sync / denied', err });
|
||||
})
|
||||
.on('complete', function (info) {
|
||||
// handle complete
|
||||
console.log({ caller: 'Sync / complete', info });
|
||||
})
|
||||
.on('error', function (err) {
|
||||
// handle error
|
||||
console.error({ caller: 'Sync / error', err });
|
||||
});
|
||||
}
|
||||
|
||||
// console.log({ caller: 'initDb / before db.changes' });
|
||||
|
||||
const changes = db
|
||||
|
||||
Reference in new issue
Block a user