Searching a new balance between the two web workers...

This commit is contained in:
vdv committed 2023-02-11 15:55:49 +01:00
1 parent 71bd820da9
commit 0e714ac361
8 files changed
+247 -201

No files matched your search

+128 -128
View File
@@ -28,147 +28,147 @@ declare global {
var dbReady: boolean;
}
export const initDb = async (params: any) => {
export const initDb = async () => {
console.log({ caller: 'initDb' });
if (globalThis.localDb === undefined) {
globalThis.dbReady = false;
globalThis.localDb = new PouchDB('_local_dyomedea_', {
adapter: 'indexeddb',
auto_compaction: true,
revs_limit: 10,
});
}
const accounts = await getAccounts();
console.log({ caller: 'initDb', accounts });
const accounts = await getAccounts();
console.log({ caller: 'initDb', accounts });
if (accounts.length === 0) {
accounts[0] = initialAccount;
await putAccount({ id: initialAccount.id, account: initialAccount });
}
const settings = await getSettings();
let currentAccount = getAccountById(accounts, settings.currentAccountId);
if (currentAccount === undefined) {
settings.currentAccountId = accounts[0].id;
await putSettings({ settings });
currentAccount = accounts[0];
}
console.log({ caller: 'initDb', settings, currentAccount });
if (globalThis.db === undefined) {
globalThis.db = new PouchDB(currentAccount.localDb, {
adapter: 'indexeddb',
auto_compaction: true,
});
}
const db = globalThis.db;
const dbinfo = await db.info();
// db.compact();
console.log({
caller: 'initDb',
dbinfo,
activeTasks: PouchDB.activeTasks.list(),
storage: await navigator.storage.estimate(),
});
//
var previousDbDefinition = {
_id: dbDefinitionId,
type: dbDefinitionId,
def: { version: '0' },
};
try {
previousDbDefinition = await db.get(dbDefinitionId);
} catch (error: any) {
if (error.status !== 404) {
console.log(
`Unexpected error fetching db definition: ${JSON.stringify(error)}`
);
return;
if (accounts.length === 0) {
accounts[0] = initialAccount;
await putAccount({ id: initialAccount.id, account: initialAccount });
}
}
if (previousDbDefinition.def.version < currentDbDefinition.def.version) {
previousDbDefinition.def = currentDbDefinition.def;
db.put(previousDbDefinition);
// TODO: support migrations
}
//await await db.compact();
const settings = await getSettings();
let currentAccount = getAccountById(accounts, settings.currentAccountId);
if (currentAccount === undefined) {
settings.currentAccountId = accounts[0].id;
await putSettings({ settings });
currentAccount = accounts[0];
}
if (currentAccount.remoteDbServer) {
const url = `${currentAccount.remoteDbServer}/userdb-${toHex(
currentAccount.remoteDbUser
)}`;
console.log({ caller: 'initDb', settings, currentAccount });
const remoteDb = new PouchDB(url, {
auth: {
username: currentAccount.remoteDbUser,
password: currentAccount.remoteDbPassword,
},
skip_setup: true,
});
const sync = PouchDB.sync(db, remoteDb, {
live: true,
retry: true,
})
.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.db === undefined) {
globalThis.db = new PouchDB(currentAccount.localDb, {
adapter: 'indexeddb',
auto_compaction: true,
});
}
const db = globalThis.db;
const dbinfo = await db.info();
// db.compact();
console.log({
caller: 'initDb',
dbinfo,
activeTasks: PouchDB.activeTasks.list(),
storage: await navigator.storage.estimate(),
});
//
var previousDbDefinition = {
_id: dbDefinitionId,
type: dbDefinitionId,
def: { version: '0' },
};
try {
previousDbDefinition = await db.get(dbDefinitionId);
} catch (error: any) {
if (error.status !== 404) {
console.log(
`Unexpected error fetching db definition: ${JSON.stringify(error)}`
);
return;
}
}
if (previousDbDefinition.def.version < currentDbDefinition.def.version) {
previousDbDefinition.def = currentDbDefinition.def;
db.put(previousDbDefinition);
// TODO: support migrations
}
//await await db.compact();
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,
});
const sync = PouchDB.sync(db, remoteDb, {
live: true,
retry: true,
})
.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
.changes({ since: 'now', live: true, include_docs: false })
.on('change', changeHandler)
.on('complete', (info: any) => {
console.log({ caller: 'changes / complete', info });
})
.on('error', (error: any) => {
console.log({ caller: 'changes / complete', error });
});
const localDbChanges = localDb
.changes({ since: 'now', live: true, include_docs: false })
.on('change', changeHandler)
.on('complete', (info: any) => {
console.log({ caller: 'localDb changes / complete', info });
})
.on('error', (error: any) => {
console.log({ caller: 'localDb changes / complete', error });
});
// console.log({ caller: 'initDb / back from db.changes', changes });
// changes.cancel();
console.log({
caller: 'initDb (end)',
dbinfo,
activeTasks: PouchDB.activeTasks.list(),
storage: await navigator.storage.estimate(),
});
globalThis.dbReady = true;
}
// console.log({ caller: 'initDb / before db.changes' });
const changes = db
.changes({ since: 'now', live: true, include_docs: false })
.on('change', changeHandler)
.on('complete', (info: any) => {
console.log({ caller: 'changes / complete', info });
})
.on('error', (error: any) => {
console.log({ caller: 'changes / complete', error });
});
const localDbChanges = localDb
.changes({ since: 'now', live: true, include_docs: false })
.on('change', changeHandler)
.on('complete', (info: any) => {
console.log({ caller: 'localDb changes / complete', info });
})
.on('error', (error: any) => {
console.log({ caller: 'localDb changes / complete', error });
});
// console.log({ caller: 'initDb / back from db.changes', changes });
// changes.cancel();
console.log({
caller: 'initDb (end)',
dbinfo,
activeTasks: PouchDB.activeTasks.list(),
storage: await navigator.storage.estimate(),
});
globalThis.dbReady = true;
};