From 534ec323121b95c79bd70f30c8387a167f0476e9 Mon Sep 17 00:00:00 2001 From: evlist Date: Sun, 12 Feb 2023 11:12:53 +0100 Subject: [PATCH] Signals are very handy in web workers ! --- src/db-admin/health-legacy.ts | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/db-admin/health-legacy.ts b/src/db-admin/health-legacy.ts index 226b8e9..7cae0cd 100644 --- a/src/db-admin/health-legacy.ts +++ b/src/db-admin/health-legacy.ts @@ -2,7 +2,7 @@ import { createEffect, createRoot, createSignal, Signal } from 'solid-js'; import PouchDB from 'pouchdb'; import indexeddb from 'pouchdb-adapter-indexeddb'; -import { sleep } from '../lib/async-wait'; +import { sleep, until } from '../lib/async-wait'; import { openDatabases } from './open'; import { toHex } from '../lib/to-hex'; import { isEqual } from 'lodash'; @@ -17,11 +17,11 @@ declare global { var sync: any; } -const [state, setState] = createSignal({}); +const [state, setState] = createSignal({}); export { state }; export const watchDbLegacy = async () => { - createRoot((dispose) => { + createRoot(async (dispose) => { console.log({ caller: 'watchDbLegacy' }); db = globalThis.db; @@ -61,14 +61,15 @@ export const watchDbLegacy = async () => { }); 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())) { - setState({ - ...state(), - localUpdateSeq: status()?.dbInfo?.update_seq, - remoteUrl: status()?.remoteDbInfo?.host, - sync: syncState(), - }); + setState(newState); } }); @@ -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 bySequence = iDb.transaction('docs', 'readonly');