Signals are very handy in web workers !

This commit is contained in:
Eric van der Vlist 2023-02-12 11:12:53 +01:00
parent 89689c5ed6
commit 534ec32312
1 changed files with 19 additions and 10 deletions

View File

@ -2,7 +2,7 @@ import { createEffect, createRoot, createSignal, Signal } from 'solid-js';
import PouchDB from 'pouchdb'; import PouchDB from 'pouchdb';
import indexeddb from 'pouchdb-adapter-indexeddb'; import indexeddb from 'pouchdb-adapter-indexeddb';
import { sleep } from '../lib/async-wait'; import { sleep, until } from '../lib/async-wait';
import { openDatabases } from './open'; import { openDatabases } from './open';
import { toHex } from '../lib/to-hex'; import { toHex } from '../lib/to-hex';
import { isEqual } from 'lodash'; import { isEqual } from 'lodash';
@ -17,11 +17,11 @@ declare global {
var sync: any; var sync: any;
} }
const [state, setState] = createSignal({}); const [state, setState] = createSignal<any>({});
export { state }; export { state };
export const watchDbLegacy = async () => { export const watchDbLegacy = async () => {
createRoot((dispose) => { createRoot(async (dispose) => {
console.log({ caller: 'watchDbLegacy' }); console.log({ caller: 'watchDbLegacy' });
db = globalThis.db; db = globalThis.db;
@ -61,14 +61,15 @@ export const watchDbLegacy = async () => {
}); });
createEffect(() => { createEffect(() => {
const newState = { ...state(), sync: syncState() }; const newState = {
...state(),
dbName: status()?.dbInfo?.db_name,
localUpdateSeq: status()?.dbInfo?.update_seq,
remoteUrl: status()?.remoteDbInfo?.host,
sync: syncState(),
};
if (!isEqual(newState, state())) { if (!isEqual(newState, state())) {
setState({ setState(newState);
...state(),
localUpdateSeq: status()?.dbInfo?.update_seq,
remoteUrl: status()?.remoteDbInfo?.host,
sync: syncState(),
});
} }
}); });
@ -157,6 +158,14 @@ export const watchDbLegacy = async () => {
}; };
}); });
await until(
() =>
state()?.sync?.paused &&
state()?.sync?.lastSeq >= state()?.localUpdateSeq,
1000
);
console.log({ caller: 'watchDbLegacy / ready to purge after sync ' });
// const iDb = (await openIDb('_pouch__dyomedea_')) as IDBDatabase; // const iDb = (await openIDb('_pouch__dyomedea_')) as IDBDatabase;
// const bySequence = iDb.transaction('docs', 'readonly'); // const bySequence = iDb.transaction('docs', 'readonly');