Moving sync back in the first worker (sync doesn't see updates done in other workers!)
This commit is contained in:
parent
51250c1880
commit
0d86b789c1
|
@ -22,24 +22,24 @@ const initDb = async () => {
|
||||||
globalThis.currentAccount = await openDatabases();
|
globalThis.currentAccount = await openDatabases();
|
||||||
const currentAccount = globalThis.currentAccount;
|
const currentAccount = globalThis.currentAccount;
|
||||||
|
|
||||||
if (currentAccount.remoteDbServer) {
|
// if (currentAccount.remoteDbServer) {
|
||||||
const url = `${currentAccount.remoteDbServer}/userdb-${toHex(
|
// const url = `${currentAccount.remoteDbServer}/userdb-${toHex(
|
||||||
currentAccount.remoteDbUser
|
// currentAccount.remoteDbUser
|
||||||
)}`;
|
// )}`;
|
||||||
|
|
||||||
const remoteDb = new PouchDB(url, {
|
// const remoteDb = new PouchDB(url, {
|
||||||
auth: {
|
// auth: {
|
||||||
username: currentAccount.remoteDbUser,
|
// username: currentAccount.remoteDbUser,
|
||||||
password: currentAccount.remoteDbPassword,
|
// password: currentAccount.remoteDbPassword,
|
||||||
},
|
// },
|
||||||
skip_setup: true,
|
// skip_setup: true,
|
||||||
});
|
// });
|
||||||
globalThis.remoteDb = remoteDb;
|
// globalThis.remoteDb = remoteDb;
|
||||||
globalThis.sync = PouchDB.sync(db, remoteDb, {
|
// globalThis.sync = PouchDB.sync(db, remoteDb, {
|
||||||
live: true,
|
// live: true,
|
||||||
retry: true,
|
// retry: true,
|
||||||
});
|
// });
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -54,33 +54,33 @@ export const watchDb = async (p: { signal: Signal<any>; params: any }) => {
|
||||||
|
|
||||||
setStatus({ opened: true });
|
setStatus({ opened: true });
|
||||||
|
|
||||||
if (globalThis.sync) {
|
// if (globalThis.sync) {
|
||||||
globalThis.sync
|
// globalThis.sync
|
||||||
.on('change', function (info) {
|
// .on('change', function (info) {
|
||||||
// handle change
|
// // handle change
|
||||||
console.log({ caller: 'Sync / change', info });
|
// console.log({ caller: 'Sync / change', info });
|
||||||
})
|
// })
|
||||||
.on('paused', function (err) {
|
// .on('paused', function (err) {
|
||||||
// replication paused (e.g. replication up to date, user went offline)
|
// // replication paused (e.g. replication up to date, user went offline)
|
||||||
console.log({ caller: 'Sync / paused', err });
|
// console.log({ caller: 'Sync / paused', err });
|
||||||
})
|
// })
|
||||||
.on('active', function () {
|
// .on('active', function () {
|
||||||
// replicate resumed (e.g. new changes replicating, user went back online)
|
// // replicate resumed (e.g. new changes replicating, user went back online)
|
||||||
console.log({ caller: 'Sync / active' });
|
// console.log({ caller: 'Sync / active' });
|
||||||
})
|
// })
|
||||||
.on('denied', function (err) {
|
// .on('denied', function (err) {
|
||||||
// a document failed to replicate (e.g. due to permissions)
|
// // a document failed to replicate (e.g. due to permissions)
|
||||||
console.error({ caller: 'Sync / denied', err });
|
// console.error({ caller: 'Sync / denied', err });
|
||||||
})
|
// })
|
||||||
.on('complete', function (info) {
|
// .on('complete', function (info) {
|
||||||
// handle complete
|
// // handle complete
|
||||||
console.log({ caller: 'Sync / complete', info });
|
// console.log({ caller: 'Sync / complete', info });
|
||||||
})
|
// })
|
||||||
.on('error', function (err) {
|
// .on('error', function (err) {
|
||||||
// handle error
|
// // handle error
|
||||||
console.error({ caller: 'Sync / error', err });
|
// console.error({ caller: 'Sync / error', err });
|
||||||
});
|
// });
|
||||||
}
|
// }
|
||||||
|
|
||||||
const timerId = setInterval(async () => {
|
const timerId = setInterval(async () => {
|
||||||
const dbInfo = await db.info();
|
const dbInfo = await db.info();
|
||||||
|
@ -89,14 +89,23 @@ export const watchDb = async (p: { signal: Signal<any>; params: any }) => {
|
||||||
? await remoteDb.info()
|
? await remoteDb.info()
|
||||||
: undefined;
|
: undefined;
|
||||||
const tasks = PouchDB.activeTasks.list();
|
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)) {
|
if (!isEqual(status(), newStatus)) {
|
||||||
setStatus(newStatus);
|
setStatus(newStatus);
|
||||||
}
|
}
|
||||||
}, 1000);
|
}, 10000);
|
||||||
|
|
||||||
// db.compact();
|
// db.compact();
|
||||||
|
|
||||||
|
await sleep(10000);
|
||||||
|
|
||||||
const openIDb = (name: string) =>
|
const openIDb = (name: string) =>
|
||||||
new Promise((resolve, reject) => {
|
new Promise((resolve, reject) => {
|
||||||
const iDb = indexedDB.open(name);
|
const iDb = indexedDB.open(name);
|
||||||
|
|
|
@ -22,7 +22,6 @@ export const openDatabases = async () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
const accounts = await getAccounts();
|
const accounts = await getAccounts();
|
||||||
console.log({ caller: 'initDb', accounts });
|
|
||||||
|
|
||||||
if (accounts.length === 0) {
|
if (accounts.length === 0) {
|
||||||
accounts[0] = initialAccount;
|
accounts[0] = initialAccount;
|
||||||
|
@ -36,8 +35,7 @@ export const openDatabases = async () => {
|
||||||
await putSettings({ settings });
|
await putSettings({ settings });
|
||||||
currentAccount = accounts[0];
|
currentAccount = accounts[0];
|
||||||
}
|
}
|
||||||
|
console.log({ caller: 'open', accounts, settings });
|
||||||
console.log({ caller: 'initDb', settings, currentAccount });
|
|
||||||
|
|
||||||
if (globalThis.db === undefined) {
|
if (globalThis.db === undefined) {
|
||||||
globalThis.db = new PouchDB(currentAccount.localDb, {
|
globalThis.db = new PouchDB(currentAccount.localDb, {
|
||||||
|
|
|
@ -17,6 +17,8 @@ declare global {
|
||||||
var localDb: any;
|
var localDb: any;
|
||||||
var db: any;
|
var db: any;
|
||||||
var dbReady: boolean;
|
var dbReady: boolean;
|
||||||
|
var currentAccount: any;
|
||||||
|
var sync: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const initDb = async () => {
|
export const initDb = async () => {
|
||||||
|
@ -27,6 +29,56 @@ export const initDb = async () => {
|
||||||
|
|
||||||
await openDatabases();
|
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' });
|
// console.log({ caller: 'initDb / before db.changes' });
|
||||||
|
|
||||||
const changes = db
|
const changes = db
|
||||||
|
|
Loading…
Reference in New Issue