From e445a7e1af2549ac63553ea1b1b3fffd0e9a86e1 Mon Sep 17 00:00:00 2001 From: evlist Date: Mon, 13 Feb 2023 10:56:48 +0100 Subject: [PATCH] db put should take parent's shared by default. --- src/db-admin/health-legacy.ts | 4 ++-- src/db/change-handler.ts | 13 ++----------- src/db/lib.ts | 10 +++++++++- src/lib/docuri/index.js | 9 +++++++++ 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/db-admin/health-legacy.ts b/src/db-admin/health-legacy.ts index 753c165..e8ef099 100644 --- a/src/db-admin/health-legacy.ts +++ b/src/db-admin/health-legacy.ts @@ -214,7 +214,7 @@ export const watchDbLegacy = async () => { // // store, // // allDocs, // docs, - // state: state(), + // state: state(),gi // }); return docs; }; @@ -223,7 +223,7 @@ export const watchDbLegacy = async () => { await until( () => state()?.sync?.paused && - state()?.sync?.lastSeq >= state()?.localUpdateSeq, + state()?.sync?.lastSeq >= state()?.localUpdateSeq - 1, // I wonder why this can happen ! 1000 ); diff --git a/src/db/change-handler.ts b/src/db/change-handler.ts index b804783..31f79a4 100644 --- a/src/db/change-handler.ts +++ b/src/db/change-handler.ts @@ -1,5 +1,6 @@ import { debounce, slice } from 'lodash'; import { getWpt } from '../db/wpt'; +import { getParentId } from '../lib/docuri'; import { returnAgain } from '../workers/dispatcher-worker'; import { getAllGpxes, getGpx } from './gpx'; import { put } from './lib'; @@ -50,17 +51,7 @@ const changeHandler = async (change: any) => { } sendUpdate(globalThis.watches.get(id)); - const tokens = id.split('/').slice(0, -1); - if (['1wpt', '2rte', '3trk'].includes(tokens.at(-1))) { - tokens.splice(-1); - } - const parentId = tokens.join('/'); - // console.log({ - // caller: 'ChangeHandler', - // change, - // watches: globalThis.watches, - // parentId, - // }); + const parentId = getParentId(id); if (parentId === 'gpx') { const gpxes = await getAllGpxes(); diff --git a/src/db/lib.ts b/src/db/lib.ts index 11adfe7..f61d7ab 100644 --- a/src/db/lib.ts +++ b/src/db/lib.ts @@ -1,5 +1,6 @@ import { cloneDeep } from 'lodash'; import { state } from '../db-admin/health-legacy'; +import { getParentId } from '../lib/docuri'; declare global { var localDb: any; @@ -19,7 +20,14 @@ export const put = async ( try { current = await targetDb.get(_id); } catch { - current = { _rev: undefined, doc: cloneDeep(defaultDoc) }; + const parentId = getParentId(_id); + let shared; + try { + const parent = await db.get(parentId); +// console.log({ caller: 'put', parent }); + shared = parent.shared; + } catch {} + current = { _rev: undefined, shared, doc: cloneDeep(defaultDoc) }; } try { putDoc = { diff --git a/src/lib/docuri/index.js b/src/lib/docuri/index.js index c236b41..2159cca 100644 --- a/src/lib/docuri/index.js +++ b/src/lib/docuri/index.js @@ -130,4 +130,13 @@ export const route = (route, coding = {}) => { }; }; +export const getParentId = (id) => { + const tokens = id.split('/').slice(0, -1); + if (['1wpt', '2rte', '3trk'].includes(tokens.at(-1))) { + tokens.splice(-1); + } + const parentId = tokens.join('/'); + return parentId; +}; + export default route;