Moving sync back in the first worker (sync doesn't see updates done in other workers!)

This commit is contained in:
Eric van der Vlist 2023-02-11 20:23:18 +01:00
parent 51250c1880
commit 0d86b789c1
3 changed files with 108 additions and 49 deletions

View File

@ -22,24 +22,24 @@ const initDb = async () => {
globalThis.currentAccount = await openDatabases();
const currentAccount = globalThis.currentAccount;
if (currentAccount.remoteDbServer) {
const url = `${currentAccount.remoteDbServer}/userdb-${toHex(
currentAccount.remoteDbUser
)}`;
// 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,
});
}
// 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,
// });
// }
}
};
@ -54,33 +54,33 @@ export const watchDb = async (p: { signal: Signal<any>; params: any }) => {
setStatus({ opened: 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 });
});
}
// 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 });
// });
// }
const timerId = setInterval(async () => {
const dbInfo = await db.info();
@ -89,14 +89,23 @@ export const watchDb = async (p: { signal: Signal<any>; params: any }) => {
? await remoteDb.info()
: undefined;
const tasks = PouchDB.activeTasks.list();
const newStatus = { ...status(), tasks, dbInfo, localDbInfo, remoteDbInfo };
const newStatus = {
...status(),
tasks,
dbInfo,
localDbInfo,
remoteDbInfo,
// date: new Date(),
};
if (!isEqual(status(), newStatus)) {
setStatus(newStatus);
}
}, 1000);
}, 10000);
// db.compact();
await sleep(10000);
const openIDb = (name: string) =>
new Promise((resolve, reject) => {
const iDb = indexedDB.open(name);

View File

@ -22,7 +22,6 @@ export const openDatabases = async () => {
});
const accounts = await getAccounts();
console.log({ caller: 'initDb', accounts });
if (accounts.length === 0) {
accounts[0] = initialAccount;
@ -36,8 +35,7 @@ export const openDatabases = async () => {
await putSettings({ settings });
currentAccount = accounts[0];
}
console.log({ caller: 'initDb', settings, currentAccount });
console.log({ caller: 'open', accounts, settings });
if (globalThis.db === undefined) {
globalThis.db = new PouchDB(currentAccount.localDb, {

View File

@ -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