From eff56d12192dbc1f3ce54aa659a93547b95a7e75 Mon Sep 17 00:00:00 2001 From: Mason Houtz Date: Wed, 30 Dec 2020 12:09:44 -0800 Subject: [PATCH] updated configs, wipeDatabase function, changes sharing --- .../src/components/History/caching/CacheApi.js | 2 +- .../src/components/History/caching/db/changes.js | 16 +++++++++++----- .../History/caching/db/changes.test.js | 2 +- .../src/components/History/caching/db/db.test.js | 2 +- .../components/History/caching/db/find.test.js | 2 +- .../src/components/History/caching/db/index.js | 2 +- .../History/caching/db/monitorQuery.test.js | 2 +- .../src/components/History/caching/db/pouch.js | 13 +++---------- .../History/caching/db/wipeDatabase.js | 10 ++++++++++ .../caching/monitorHistoryContent.test.js | 2 +- .../providers/DscProvider/DscProvider.test.js | 2 +- client/src/components/Masthead/Masthead.test.js | 1 + client/src/components/Upload/UploadModal.test.js | 1 + client/src/config/development.js | 2 +- client/src/config/production.js | 2 +- client/src/config/testing.js | 2 +- client/tests/jest/__mocks__/config.js | 2 +- 17 files changed, 38 insertions(+), 27 deletions(-) create mode 100644 client/src/components/History/caching/db/wipeDatabase.js diff --git a/client/src/components/History/caching/CacheApi.js b/client/src/components/History/caching/CacheApi.js index 9f62300ee5b..8f6cc927aca 100644 --- a/client/src/components/History/caching/CacheApi.js +++ b/client/src/components/History/caching/CacheApi.js @@ -17,7 +17,7 @@ export * from "./db/promises"; export { loadDscContent } from "./loadDscContent"; export { loadHistoryContents, clearHistoryDateStore } from "./loadHistoryContents"; export { monitorHistoryContent } from "./monitorHistoryContent"; -export { wipeDatabase } from "./db/pouch"; +export { wipeDatabase } from "./db/wipeDatabase"; // generic content query monitor export const monitorContentQuery = (cfg = {}) => { diff --git a/client/src/components/History/caching/db/changes.js b/client/src/components/History/caching/db/changes.js index 0cdd9474a0c..9eedccc20d5 100644 --- a/client/src/components/History/caching/db/changes.js +++ b/client/src/components/History/caching/db/changes.js @@ -1,8 +1,8 @@ import { pipe, Observable } from "rxjs"; -import { switchMap, filter, share } from "rxjs/operators"; +import { switchMap, filter, share, shareReplay } from "rxjs/operators"; // feed observables, keyed by underlying database instance -const feeds = new Map(); +export const feeds = new Map(); /** * Returns an observable with all the change events from the indicated database @@ -12,6 +12,7 @@ const feeds = new Map(); export const changes = (cfg = {}) => { return pipe( switchMap((db) => { + // console.log("subscribing to changes", db.name); if (!feeds.has(db)) { feeds.set(db, buildFeed(db, cfg)); } @@ -19,7 +20,6 @@ export const changes = (cfg = {}) => { }), // filter out index creation which can appear as a change filter(({ id }) => !id.includes("_design")), - share() ); }; @@ -31,11 +31,17 @@ export const changes = (cfg = {}) => { const buildFeed = (db, cfg = {}) => { const { live = true, returnDocs = true, include_docs = true, since = "now", timeout = false } = cfg; - return new Observable((obs) => { + const feed$ = new Observable((obs) => { + // console.log("creating feed", db.name); const changeOpts = { live, include_docs, returnDocs, since, timeout }; const feed = db.changes(changeOpts); feed.on("change", (update) => obs.next(update)); feed.on("error", (err) => obs.error(err)); - return () => feed.cancel(); + return () => { + // console.log("cancelling feed", db.name); + feed.cancel(); + } }); + + return feed$.pipe(share()); }; diff --git a/client/src/components/History/caching/db/changes.test.js b/client/src/components/History/caching/db/changes.test.js index 6620eccc044..8b09c8a4335 100644 --- a/client/src/components/History/caching/db/changes.test.js +++ b/client/src/components/History/caching/db/changes.test.js @@ -1,6 +1,6 @@ import { takeWhile, share } from "rxjs/operators"; import { content$, dscContent$ } from "./observables"; -import { wipeDatabase } from "./pouch"; +import { wipeDatabase } from "./wipeDatabase"; import { bulkCacheContent, cacheContent, diff --git a/client/src/components/History/caching/db/db.test.js b/client/src/components/History/caching/db/db.test.js index c25599cadba..95a63e7fb57 100644 --- a/client/src/components/History/caching/db/db.test.js +++ b/client/src/components/History/caching/db/db.test.js @@ -1,6 +1,6 @@ import isPromise from "is-promise"; import { isObservable } from "rxjs"; -import { wipeDatabase } from "./pouch"; +import { wipeDatabase } from "./wipeDatabase"; import { content$, dscContent$, buildContentId, buildCollectionId, prepContent, prepDscContent } from "./observables"; import { firstValueFrom } from "utils/observable/firstValueFrom"; diff --git a/client/src/components/History/caching/db/find.test.js b/client/src/components/History/caching/db/find.test.js index 6acd268420e..5cded53c7a9 100644 --- a/client/src/components/History/caching/db/find.test.js +++ b/client/src/components/History/caching/db/find.test.js @@ -2,7 +2,7 @@ import { Subject } from "rxjs"; import { take } from "rxjs/operators"; import { ObserverSpy } from "@hirez_io/observer-spy"; -import { wipeDatabase } from "./pouch"; +import { wipeDatabase } from "./wipeDatabase"; import { bulkCacheContent, bulkCacheDscContent } from "./promises"; import { content$, dscContent$, buildContentId } from "./observables"; import { find } from "./find"; diff --git a/client/src/components/History/caching/db/index.js b/client/src/components/History/caching/db/index.js index 0f8d7b9562e..0e77b861485 100644 --- a/client/src/components/History/caching/db/index.js +++ b/client/src/components/History/caching/db/index.js @@ -13,4 +13,4 @@ export { changes } from "./changes"; export { monitorQuery } from "./monitorQuery"; // utility function -export { wipeDatabase } from "./pouch"; +export { wipeDatabase } from "./wipeDatabase"; diff --git a/client/src/components/History/caching/db/monitorQuery.test.js b/client/src/components/History/caching/db/monitorQuery.test.js index 14b551be462..a319901e04c 100644 --- a/client/src/components/History/caching/db/monitorQuery.test.js +++ b/client/src/components/History/caching/db/monitorQuery.test.js @@ -2,7 +2,7 @@ import { of, timer } from "rxjs"; import { take, takeUntil } from "rxjs/operators"; import { ObserverSpy } from "@hirez_io/observer-spy"; import { wait } from "jest/helpers"; -import { wipeDatabase } from "./pouch"; +import { wipeDatabase } from "./wipeDatabase"; import { monitorQuery, ACTIONS } from "./monitorQuery"; import { content$, dscContent$ } from "./observables"; diff --git a/client/src/components/History/caching/db/pouch.js b/client/src/components/History/caching/db/pouch.js index e6d60fb5daa..fd69f58e2e6 100644 --- a/client/src/components/History/caching/db/pouch.js +++ b/client/src/components/History/caching/db/pouch.js @@ -8,6 +8,7 @@ import deepEqual from "deep-equal"; import { defer, pipe, from } from "rxjs"; import { tap, filter, mergeMap, reduce, shareReplay } from "rxjs/operators"; import { needs } from "../operators/needs"; +import { dasherize } from "underscore.string"; import PouchDB from "pouchdb"; import PouchAdapterMemory from "pouchdb-adapter-memory"; @@ -27,7 +28,7 @@ PouchDB.plugin(PouchErase); // const show = (obj) => console.log(JSON.stringify(obj, null, 4)); // Instance storage map, keyed by database name -const dbs = new Map(); +export const dbs = new Map(); /** * Generate an observable that initializes and shares a pouchdb instance. @@ -71,7 +72,7 @@ async function buildCollection(opts, appConfig) { function collectionName(opts, appConfig) { const { name: dbName } = opts; const { name: envName } = appConfig; - return `${dbName}-${envName}`; + return dasherize(`${dbName} ${envName}`); } async function installCollectionIndexes(db, indexes = []) { @@ -231,11 +232,3 @@ export async function deleteIndexes(db) { return await Promise.all(promises); } -/** - * Erases all stored database instances - */ -export async function wipeDatabase() { - for (const db of dbs.values()) { - await db.erase(); - } -} diff --git a/client/src/components/History/caching/db/wipeDatabase.js b/client/src/components/History/caching/db/wipeDatabase.js new file mode 100644 index 00000000000..0e5c5b6b2dc --- /dev/null +++ b/client/src/components/History/caching/db/wipeDatabase.js @@ -0,0 +1,10 @@ +import { dbs } from "./pouch"; + +/** + * Erases all stored database instances + */ +export async function wipeDatabase() { + for (const db of dbs.values()) { + await db.erase(); + } +} diff --git a/client/src/components/History/caching/monitorHistoryContent.test.js b/client/src/components/History/caching/monitorHistoryContent.test.js index a151e0f6c14..ad386d1bbf2 100644 --- a/client/src/components/History/caching/monitorHistoryContent.test.js +++ b/client/src/components/History/caching/monitorHistoryContent.test.js @@ -1,7 +1,7 @@ import { timer, of } from "rxjs"; import { take, pluck, takeUntil } from "rxjs/operators"; import { firstValueFrom } from "utils/observable/firstValueFrom"; -import { wipeDatabase } from "../caching/db/pouch"; +import { wipeDatabase } from "./db/wipeDatabase"; import { wait } from "jest/helpers"; import { ObserverSpy } from "@hirez_io/observer-spy"; diff --git a/client/src/components/History/providers/DscProvider/DscProvider.test.js b/client/src/components/History/providers/DscProvider/DscProvider.test.js index 894e380bed7..a224a3c5c15 100644 --- a/client/src/components/History/providers/DscProvider/DscProvider.test.js +++ b/client/src/components/History/providers/DscProvider/DscProvider.test.js @@ -1,7 +1,7 @@ /* eslint-disable no-unused-vars */ import { createLocalVue } from "@vue/test-utils"; import { wait, mountRenderless } from "jest/helpers"; -import { wipeDatabase } from "../../caching/db/pouch"; +import { wipeDatabase } from "../../caching"; import { cacheContent, getCachedContent, cacheCollectionContent, getCachedCollectionContent } from "../../caching"; import { DatasetCollection } from "../../model/DatasetCollection"; import DscProvider from "./DscProvider"; diff --git a/client/src/components/Masthead/Masthead.test.js b/client/src/components/Masthead/Masthead.test.js index 1e92be2e7df..cff1d151b5d 100644 --- a/client/src/components/Masthead/Masthead.test.js +++ b/client/src/components/Masthead/Masthead.test.js @@ -8,6 +8,7 @@ import { loadWebhookMenuItems } from "./_webhooks"; jest.mock("app"); jest.mock("layout/menu"); jest.mock("./_webhooks"); +jest.mock("../History/caching"); describe("Masthead.vue", () => { let wrapper; diff --git a/client/src/components/Upload/UploadModal.test.js b/client/src/components/Upload/UploadModal.test.js index 86288de81f4..82cf033a89e 100644 --- a/client/src/components/Upload/UploadModal.test.js +++ b/client/src/components/Upload/UploadModal.test.js @@ -6,6 +6,7 @@ import { shallowMount, createLocalVue } from "@vue/test-utils"; import BootstrapVue from "bootstrap-vue"; jest.mock("app"); +jest.mock("../History/caching"); const propsData = { chunkUploadSize: 1024, diff --git a/client/src/config/development.js b/client/src/config/development.js index 44394b27a53..04b763f0a47 100644 --- a/client/src/config/development.js +++ b/client/src/config/development.js @@ -1,5 +1,5 @@ export default { - name: "development configs", + name: "development", debug: true, rxjsDebug: true, caching: { diff --git a/client/src/config/production.js b/client/src/config/production.js index 671799cee7f..617c605ecad 100644 --- a/client/src/config/production.js +++ b/client/src/config/production.js @@ -1,5 +1,5 @@ export default { - name: "production configs", + name: "production", debug: false, rxjsDebug: false, caching: { diff --git a/client/src/config/testing.js b/client/src/config/testing.js index e8168fb9be2..dd2639c4f24 100644 --- a/client/src/config/testing.js +++ b/client/src/config/testing.js @@ -1,5 +1,5 @@ export default { - name: "unit testing configs", + name: "testing", testBuild: true, debug: false, rxjsDebug: false, diff --git a/client/tests/jest/__mocks__/config.js b/client/tests/jest/__mocks__/config.js index 6f9f2b4119c..0aaa35c1744 100644 --- a/client/tests/jest/__mocks__/config.js +++ b/client/tests/jest/__mocks__/config.js @@ -1,5 +1,5 @@ export default { - name: "unit testing configs", + name: "testing", testBuild: true, debug: false, caching: {