Refactoring to watch WPT updates.

This commit is contained in:
vdv committed 2022-12-06 14:36:50 +01:00
1 parent 6465a371cb
commit 5829f93c91
5 files changed
+111 -41

No files matched your search

+43 -3
View File
@@ -1,6 +1,46 @@
import { getWpt } from '../db/wpt';
import { returnAgain } from '../workers/dispatcher-worker';
const changeHandler = (change: any) => {
console.log({caller: 'ChangeHandler', change});
declare global {
var watches: Map<string, any>;
}
export default changeHandler;
const methods = {
getWpt,
};
const changeHandler = async (change: any) => {
console.log({ caller: 'ChangeHandler', change });
const { id } = change.id;
if (!globalThis.watches) {
globalThis.watches = new Map();
}
const params = globalThis.watches.get(id);
if (params) {
const { method, _dispatchId, id, ...otherParams } = params;
const returnValue = await methods[<keyof typeof methods>method](params);
if (false) {
returnAgain(_dispatchId, returnValue);
}
}
};
export default changeHandler;
export const getAndWatch = async (params: any) => {
const { method, _dispatchId, id, ...otherParams } = params;
if (!globalThis.watches) {
globalThis.watches = new Map();
}
globalThis.watches.set(id, params);
const returnValue = await methods[<keyof typeof methods>method](params);
return returnValue;
};
export const cancelWatch = (params: any) => {
if (!globalThis.watches) {
globalThis.watches = new Map();
}
const { id } = params;
globalThis.watches.delete(id);
};
+2 -6
View File
@@ -99,12 +99,8 @@ export const initDb = async (params: any) => {
console.log({ caller: 'initDb / before db.changes' });
const changes = db
.changes({ since: 'now', live: true, include_docs: true })
.changes({ since: 'now', live: true, include_docs: false })
.on('change', changeHandler)
// .on('change', (info: any) => {
// console.log({ caller: 'changes / change', info });
// changeHandler(info);
// })
.on('complete', (info: any) => {
console.log({ caller: 'changes / complete', info });
})
@@ -112,7 +108,7 @@ export const initDb = async (params: any) => {
console.log({ caller: 'changes / complete', error });
});
console.log({ caller: 'initDb / back from db.changes', changes });
// console.log({ caller: 'initDb / back from db.changes', changes });
// changes.cancel();