From c4b2fa175d5dc8056d77bcaa2dfa2630e4592b61 Mon Sep 17 00:00:00 2001 From: evlist Date: Tue, 14 Feb 2023 20:55:20 +0100 Subject: [PATCH] Bug fix (trkpts were deleted during compaction) --- package-lock.json | 76 +++++++++++++++++++++---------------------- package.json | 6 ++-- src/db/rte.test.ts | 30 +++++++++++++++++ src/db/rte.ts | 34 +++++++++++-------- src/db/trkseg.test.ts | 66 ++++++++++++++++++++++++++++++++++++- src/db/trkseg.ts | 14 ++++---- 6 files changed, 163 insertions(+), 63 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5319bc5..75792fb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -38,7 +38,7 @@ "pouchdb-adapter-indexeddb": "^8.0.1", "pouchdb-browser": "^8.0.1", "proj4": "^2.8.1", - "solid-js": "^1.6.10", + "solid-js": "^1.6.11", "vite-plugin-solid-svg": "^0.6.0" }, "devDependencies": { @@ -50,7 +50,7 @@ "@types/pouchdb": "^6.4.0", "@types/proj4": "^2.5.2", "@types/shelljs": "^0.8.11", - "@vitest/ui": "^0.28.4", + "@vitest/ui": "^0.28.5", "jsdom": "^21.1.0", "license-checker": "^25.0.1", "license-compatibility-checker": "^0.3.5", @@ -59,7 +59,7 @@ "typescript": "^4.9.5", "vite": ">=4.1.1", "vite-plugin-solid": "^2.5.0", - "vitest": "^0.28.4" + "vitest": "^0.28.5" } }, "node_modules/@adobe/css-tools": { @@ -2247,40 +2247,40 @@ "dev": true }, "node_modules/@vitest/expect": { - "version": "0.28.4", - "resolved": "https://registry.npmjs.org/@vitest/expect/-/expect-0.28.4.tgz", - "integrity": "sha512-JqK0NZ4brjvOSL8hXAnIsfi+jxDF7rH/ZWCGCt0FAqRnVFc1hXsfwXksQvEnKqD84avRt3gmeXoK4tNbmkoVsQ==", + "version": "0.28.5", + "resolved": "https://registry.npmjs.org/@vitest/expect/-/expect-0.28.5.tgz", + "integrity": "sha512-gqTZwoUTwepwGIatnw4UKpQfnoyV0Z9Czn9+Lo2/jLIt4/AXLTn+oVZxlQ7Ng8bzcNkR+3DqLJ08kNr8jRmdNQ==", "dev": true, "dependencies": { - "@vitest/spy": "0.28.4", - "@vitest/utils": "0.28.4", + "@vitest/spy": "0.28.5", + "@vitest/utils": "0.28.5", "chai": "^4.3.7" } }, "node_modules/@vitest/runner": { - "version": "0.28.4", - "resolved": "https://registry.npmjs.org/@vitest/runner/-/runner-0.28.4.tgz", - "integrity": "sha512-Q8UV6GjDvBSTfUoq0QXVCNpNOUrWu4P2qvRq7ssJWzn0+S0ojbVOxEjMt+8a32X6SdkhF8ak+2nkppsqV0JyNQ==", + "version": "0.28.5", + "resolved": "https://registry.npmjs.org/@vitest/runner/-/runner-0.28.5.tgz", + "integrity": "sha512-NKkHtLB+FGjpp5KmneQjTcPLWPTDfB7ie+MmF1PnUBf/tGe2OjGxWyB62ySYZ25EYp9krR5Bw0YPLS/VWh1QiA==", "dev": true, "dependencies": { - "@vitest/utils": "0.28.4", + "@vitest/utils": "0.28.5", "p-limit": "^4.0.0", "pathe": "^1.1.0" } }, "node_modules/@vitest/spy": { - "version": "0.28.4", - "resolved": "https://registry.npmjs.org/@vitest/spy/-/spy-0.28.4.tgz", - "integrity": "sha512-8WuhfXLlvCXpNXEGJW6Gc+IKWI32435fQJLh43u70HnZ1otJOa2Cmg2Wy2Aym47ZnNCP4NolF+8cUPwd0MigKQ==", + "version": "0.28.5", + "resolved": "https://registry.npmjs.org/@vitest/spy/-/spy-0.28.5.tgz", + "integrity": "sha512-7if6rsHQr9zbmvxN7h+gGh2L9eIIErgf8nSKYDlg07HHimCxp4H6I/X/DPXktVPPLQfiZ1Cw2cbDIx9fSqDjGw==", "dev": true, "dependencies": { "tinyspy": "^1.0.2" } }, "node_modules/@vitest/ui": { - "version": "0.28.4", - "resolved": "https://registry.npmjs.org/@vitest/ui/-/ui-0.28.4.tgz", - "integrity": "sha512-LQfCCFc17n49mwtraV9/NAWl2DUqJS/9ZEa3fqJjoYO+HowdseQ5jvWflpzliCyfrIAh6cXVo1bNzHnDXe0cbw==", + "version": "0.28.5", + "resolved": "https://registry.npmjs.org/@vitest/ui/-/ui-0.28.5.tgz", + "integrity": "sha512-hzzZzv38mH/LMFh54QEJpWFuGixZZBOD+C0fHU81d1lsvochPwNZhWJbuRJQNyZLSMZYCYW4hF6PpNQJXDHDmg==", "dev": true, "dependencies": { "fast-glob": "^3.2.12", @@ -2291,9 +2291,9 @@ } }, "node_modules/@vitest/utils": { - "version": "0.28.4", - "resolved": "https://registry.npmjs.org/@vitest/utils/-/utils-0.28.4.tgz", - "integrity": "sha512-l2QztOLdc2LkR+w/lP52RGh8hW+Ul4KESmCAgVE8q737I7e7bQoAfkARKpkPJ4JQtGpwW4deqlj1732VZD7TFw==", + "version": "0.28.5", + "resolved": "https://registry.npmjs.org/@vitest/utils/-/utils-0.28.5.tgz", + "integrity": "sha512-UyZdYwdULlOa4LTUSwZ+Paz7nBHGTT72jKwdFSV4IjHF1xsokp+CabMdhjvVhYwkLfO88ylJT46YMilnkSARZA==", "dev": true, "dependencies": { "cli-truncate": "^3.1.0", @@ -7954,9 +7954,9 @@ } }, "node_modules/solid-js": { - "version": "1.6.10", - "resolved": "https://registry.npmjs.org/solid-js/-/solid-js-1.6.10.tgz", - "integrity": "sha512-Sf0e6PQCEFkFtbPq0L+93Ua81YQOefBEbvDJ0YXT92b6Lzw0k7UvzSd2l1BbYM+yzE3UmepU1tyMDc/3nIByjA==", + "version": "1.6.11", + "resolved": "https://registry.npmjs.org/solid-js/-/solid-js-1.6.11.tgz", + "integrity": "sha512-JquQQHPArGq+i2PLURxJ99Pcz2/1docpbycSio/cKSA0SeI3z5zRjy0TNcH4NRYvbOLrcini+iovXwnexKabyw==", "dependencies": { "csstype": "^3.1.0" } @@ -8385,9 +8385,9 @@ } }, "node_modules/tinyspy": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/tinyspy/-/tinyspy-1.0.2.tgz", - "integrity": "sha512-bSGlgwLBYf7PnUsQ6WOc6SJ3pGOcd+d8AA6EUnLDDM0kWEstC1JIlSZA3UNliDXhd9ABoS7hiRBDCu+XP/sf1Q==", + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/tinyspy/-/tinyspy-1.1.1.tgz", + "integrity": "sha512-UVq5AXt/gQlti7oxoIg5oi/9r0WpF7DGEVwXgqWSMmyN16+e3tl5lIvTaOpJ3TAtu5xFzWccFRM4R5NaWHF+4g==", "dev": true, "engines": { "node": ">=14.0.0" @@ -8842,9 +8842,9 @@ } }, "node_modules/vite-node": { - "version": "0.28.4", - "resolved": "https://registry.npmjs.org/vite-node/-/vite-node-0.28.4.tgz", - "integrity": "sha512-KM0Q0uSG/xHHKOJvVHc5xDBabgt0l70y7/lWTR7Q0pR5/MrYxadT+y32cJOE65FfjGmJgxpVEEY+69btJgcXOQ==", + "version": "0.28.5", + "resolved": "https://registry.npmjs.org/vite-node/-/vite-node-0.28.5.tgz", + "integrity": "sha512-LmXb9saMGlrMZbXTvOveJKwMTBTNUH66c8rJnQ0ZPNX+myPEol64+szRzXtV5ORb0Hb/91yq+/D3oERoyAt6LA==", "dev": true, "dependencies": { "cac": "^6.7.14", @@ -9277,18 +9277,18 @@ } }, "node_modules/vitest": { - "version": "0.28.4", - "resolved": "https://registry.npmjs.org/vitest/-/vitest-0.28.4.tgz", - "integrity": "sha512-sfWIy0AdlbyGRhunm+TLQEJrFH9XuRPdApfubsyLcDbCRrUX717BRQKInTgzEfyl2Ipi1HWoHB84Nqtcwxogcg==", + "version": "0.28.5", + "resolved": "https://registry.npmjs.org/vitest/-/vitest-0.28.5.tgz", + "integrity": "sha512-pyCQ+wcAOX7mKMcBNkzDwEHRGqQvHUl0XnoHR+3Pb1hytAHISgSxv9h0gUiSiYtISXUU3rMrKiKzFYDrI6ZIHA==", "dev": true, "dependencies": { "@types/chai": "^4.3.4", "@types/chai-subset": "^1.3.3", "@types/node": "*", - "@vitest/expect": "0.28.4", - "@vitest/runner": "0.28.4", - "@vitest/spy": "0.28.4", - "@vitest/utils": "0.28.4", + "@vitest/expect": "0.28.5", + "@vitest/runner": "0.28.5", + "@vitest/spy": "0.28.5", + "@vitest/utils": "0.28.5", "acorn": "^8.8.1", "acorn-walk": "^8.2.0", "cac": "^6.7.14", @@ -9304,7 +9304,7 @@ "tinypool": "^0.3.1", "tinyspy": "^1.0.2", "vite": "^3.0.0 || ^4.0.0", - "vite-node": "0.28.4", + "vite-node": "0.28.5", "why-is-node-running": "^2.2.2" }, "bin": { diff --git a/package.json b/package.json index f25017e..1333ab9 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "@types/pouchdb": "^6.4.0", "@types/proj4": "^2.5.2", "@types/shelljs": "^0.8.11", - "@vitest/ui": "^0.28.4", + "@vitest/ui": "^0.28.5", "jsdom": "^21.1.0", "license-checker": "^25.0.1", "license-compatibility-checker": "^0.3.5", @@ -30,7 +30,7 @@ "typescript": "^4.9.5", "vite": ">=4.1.1", "vite-plugin-solid": "^2.5.0", - "vitest": "^0.28.4" + "vitest": "^0.28.5" }, "dependencies": { "@awesome-cordova-plugins/android-full-screen": "^6.3.0", @@ -61,7 +61,7 @@ "pouchdb-adapter-indexeddb": "^8.0.1", "pouchdb-browser": "^8.0.1", "proj4": "^2.8.1", - "solid-js": "^1.6.10", + "solid-js": "^1.6.11", "vite-plugin-solid-svg": "^0.6.0" } } diff --git a/src/db/rte.test.ts b/src/db/rte.test.ts index 4a19087..88a6ba5 100644 --- a/src/db/rte.test.ts +++ b/src/db/rte.test.ts @@ -6,6 +6,9 @@ import PouchDB from 'pouchdb'; import { getRte, putNewRte, putRte } from './rte'; import { put } from './lib'; import { emptyWpt } from './wpt'; +import { sleep } from '../lib/async-wait'; +import { debounce, memoize, property, wrap } from 'lodash'; +import memoizee from 'memoizee'; const test = it; const jest = vi; @@ -136,4 +139,31 @@ describe('The rte module with a real db', () => { await put(idRtept, 'rtept', () => rtept3, emptyWpt); expect(await getRte({ id: idRte })).toEqual(rte1); }); + + // it('debounces correctly by param ', async () => { + // let values = [0, 1, 2, 3, 4, 5]; + // const decrement = (params: any) => { + // const { id } = params; + // //await sleep(1); + // values[2] = values[2] - 1; + // }; + + // const debounceByParam = (targetFunc, resolver, ...debounceParams) => + // wrap( + // memoize(() => debounce(targetFunc, ...debounceParams), resolver), + // (getMemoizedFunc, ...params) => getMemoizedFunc(...params)(...params) + // ); + + // const debouncedDecrement = debounce(decrement, 100); + // debouncedDecrement({ id: 2 }); + // debouncedDecrement({ id: 2 }); + // debouncedDecrement({ id: 5 }); + // debouncedDecrement({ id: 2 }); + // debouncedDecrement({ id: 5 }); + // debouncedDecrement({ id: 2 }); + // debouncedDecrement({ id: 2 }); + // debouncedDecrement({ id: 2 }); + // await sleep(2000); + // expect(values).toEqual([0, 1, 1, 3, 4, 4]); + // }); }); diff --git a/src/db/rte.ts b/src/db/rte.ts index 55a8be1..ec5d82b 100644 --- a/src/db/rte.ts +++ b/src/db/rte.ts @@ -34,7 +34,8 @@ export const putNewRte = async (id?: IdRte | IdGpx) => { }; // See https://stackoverflow.com/questions/28787436/debounce-a-function-with-argument -const debounceByParam = ( + +export const debounceByParam = ( targetFunc: (params: any) => any, resolver: (params: any) => any, ...debounceParams: any @@ -44,20 +45,25 @@ const debounceByParam = ( (getMemoizedFunc, ...params) => getMemoizedFunc(...params)(...params) ); -const compactRteOrTrkseg = (params: any) => { +export const compactRteOrTrkseg = async (params: any) => { const { getDocs } = params; - getDocs(params).then(({ docs }) => { - if (docs.rows.length > 1) { - const compactedDocs = docs.rows.map((row: any) => { - if (['rte', 'trkseg'].includes(row.doc.type)) { - return row.doc; - } - return { ...row.doc, doc: undefined, _deleted: true }; - }); - db.bulkDocs(compactedDocs); - console.log({ caller: 'compactRteOrTrkseg', params, compactedDocs }); - } - }); + const { docs, rte, trkseg } = await getDocs(params); + if (docs.rows.length > 1) { + const compactedDocs = docs.rows.map((row: any) => { + if (['rte', 'trkseg'].includes(row.doc.type)) { + return { ...row.doc, doc: rte || trkseg }; + } + return { ...row.doc, doc: undefined, _deleted: true }; + }); + console.log({ + caller: 'compactRteOrTrkseg', + params, + compactedDocs, + firstDoc: compactedDocs[0].doc, + firstDocWpt: compactedDocs[0].doc.trkpt[0], + }); + await db.bulkDocs(compactedDocs); + } }; export const compactRteOrTrksegDebounced = debounceByParam( diff --git a/src/db/trkseg.test.ts b/src/db/trkseg.test.ts index 5910839..1529a87 100644 --- a/src/db/trkseg.test.ts +++ b/src/db/trkseg.test.ts @@ -3,11 +3,13 @@ import shell from 'shelljs'; import PouchDB from 'pouchdb'; -import { appendTrkpt, getTrkseg, putTrkseg } from './trkseg'; +import { appendTrkpt, getTrkseg, getTrksegDocs, putTrkseg } from './trkseg'; import getUri from '../lib/ids'; import { put } from './lib'; import { emptyWpt } from './wpt'; +import { compactRteOrTrkseg } from './rte'; +import { sleep } from '../lib/async-wait'; const test = it; const jest = vi; @@ -125,4 +127,66 @@ describe('The trkseg module with a real db', () => { }); expect(await getTrkseg({ id: idTrkseg })).toEqual(trkseg1); }); + + it('can compact external trkpts after existing internal trkpts', async () => { + const idTrkseg = 'gpx/4320836036089189/3trk/000000/000000'; + const idTrksegpt = 'gpx/4320836036089189/3trk/000000/000000/000035'; + const trkpt3: Wpt = { + $: { lat: 3, lon: 3 }, + }; + const trkseg0: Trkseg = { + trkpt: [ + { + $: { lat: 1, lon: 1 }, + }, + { + $: { lat: 2, lon: 2 }, + }, + ], + }; + const trkseg1: Trkseg = { + trkpt: [ + { + $: { lat: 1, lon: 1 }, + }, + { + $: { lat: 2, lon: 2 }, + }, + trkpt3, + ], + }; + const trksegPut = await putTrkseg(idTrkseg, trkseg0); + await put(idTrksegpt, 'trkpt', () => trkpt3, emptyWpt); + expect(await getTrkseg({ id: idTrkseg })).toEqual(trkseg1); + compactRteOrTrkseg({ id: idTrkseg, getDocs: getTrksegDocs }); + expect(await getTrkseg({ id: idTrkseg })).toEqual(trkseg1); + }); + it('can compact external trkpts without existing internal trkpts', async () => { + const idTrkseg = 'gpx/4320836036089189/3trk/000000/000000'; + const idTrksegpt1 = 'gpx/4320836036089189/3trk/000000/000000/000035'; + const idTrksegpt2 = 'gpx/4320836036089189/3trk/000000/000000/000036'; + const trkpt1 = { + $: { lat: 1, lon: 1 }, + }; + const trkpt2 = { + $: { lat: 2, lon: 2 }, + }; + const trkseg0: Trkseg = {}; + const trkseg1: Trkseg = { + trkpt: [trkpt1, trkpt2], + }; + const trksegPut = await putTrkseg(idTrkseg, trkseg0); + await put(idTrksegpt1, 'trkpt', () => trkpt1, emptyWpt); + await put(idTrksegpt2, 'trkpt', () => trkpt2, emptyWpt); + const { trkseg, docs } = await getTrksegDocs({ id: idTrkseg }); + expect(trkseg).toEqual(trkseg1); + expect(docs.rows.length).toEqual(3); + await compactRteOrTrkseg({ id: idTrkseg, getDocs: getTrksegDocs }); + //await sleep(5000); + const { trkseg: trksegx, docs: docsx } = await getTrksegDocs({ + id: idTrkseg, + }); + expect(trksegx).toEqual(trkseg1); + expect(docsx.rows.length).toEqual(1); + }); }); diff --git a/src/db/trkseg.ts b/src/db/trkseg.ts index a672616..2e274d6 100644 --- a/src/db/trkseg.ts +++ b/src/db/trkseg.ts @@ -30,13 +30,13 @@ export const getTrksegDocs: ({ trkseg.trkpt.push(row.doc.doc); }); } - // console.log({ - // caller: 'getRteDocs', - // id, - // docs, - // nbRteptIn: docs.rows[0].doc.doc.rtept?.length, - // nbRteptTotal: rte?.rtept?.length, - // }); + console.log({ + caller: 'getTrksegDocs', + id, + docs, + nbWptIn: docs.rows[0].doc.doc.trkpt?.length, + nbWptTotal: trkseg?.trkpt?.length, + }); return { docs, trkseg }; };