Code Cleanup.

This commit is contained in:
Michael Mainguy 2023-11-07 10:27:21 -06:00
parent cee114f784
commit fd93444ac4
2 changed files with 24 additions and 21 deletions

View File

@ -9,7 +9,7 @@ function buildOptions(req: Request) {
if (req.method == 'OPTIONS') { if (req.method == 'OPTIONS') {
const origin = req.headers['origin']; const origin = req.headers['origin'];
return new Response( return new Response(
new Blob(), "",
{ {
headers: { headers: {
'Allow': 'POST', 'Allow': 'POST',

View File

@ -6,6 +6,7 @@ import {AppConfigType} from "../util/appConfigType";
import {v4 as uuidv4} from 'uuid'; import {v4 as uuidv4} from 'uuid';
import axios from "axios"; import axios from "axios";
import {DiagramManager} from "../diagram/diagramManager"; import {DiagramManager} from "../diagram/diagramManager";
import log, {Logger} from "loglevel";
export class PouchdbPersistenceManager implements IPersistenceManager { export class PouchdbPersistenceManager implements IPersistenceManager {
configObserver: Observable<AppConfigType> = new Observable<AppConfigType>(); configObserver: Observable<AppConfigType> = new Observable<AppConfigType>();
@ -17,7 +18,7 @@ export class PouchdbPersistenceManager implements IPersistenceManager {
private remote: PouchDB; private remote: PouchDB;
private config: PouchDB; private config: PouchDB;
private diagramListings: PouchDB; private diagramListings: PouchDB;
private readonly logger: Logger = log.getLogger('PouchdbPersistenceManager');
constructor() { constructor() {
this.config = new PouchDB("config"); this.config = new PouchDB("config");
this.diagramListings = new PouchDB("diagramListings"); this.diagramListings = new PouchDB("diagramListings");
@ -80,7 +81,7 @@ export class PouchdbPersistenceManager implements IPersistenceManager {
try { try {
this.db.put(newEntity); this.db.put(newEntity);
} catch (err) { } catch (err) {
console.log(err); this.logger.error(err);
} }
} }
@ -92,7 +93,7 @@ export class PouchdbPersistenceManager implements IPersistenceManager {
const doc = await this.db.get(id); const doc = await this.db.get(id);
this.db.remove(doc); this.db.remove(doc);
} catch (err) { } catch (err) {
console.log(err); this.logger.error(err);
} }
} }
@ -106,7 +107,7 @@ export class PouchdbPersistenceManager implements IPersistenceManager {
this.db.put(newDoc); this.db.put(newDoc);
} catch (err) { } catch (err) {
console.log(err); this.logger.error(err);
} }
} }
@ -123,7 +124,7 @@ export class PouchdbPersistenceManager implements IPersistenceManager {
try { try {
this.diagramListings.delete(diagram.id); this.diagramListings.delete(diagram.id);
} catch (err) { } catch (err) {
console.log(err); this.logger.error(err);
} }
} }
@ -132,7 +133,7 @@ export class PouchdbPersistenceManager implements IPersistenceManager {
const doc = await this.db.get(diagram.id); const doc = await this.db.get(diagram.id);
this.db.put({...doc, ...diagram}); this.db.put({...doc, ...diagram});
} catch (err) { } catch (err) {
console.log(err); this.logger.error(err);
} }
} }
@ -187,7 +188,7 @@ export class PouchdbPersistenceManager implements IPersistenceManager {
try { try {
await this.setConfig(defaultConfig, true); await this.setConfig(defaultConfig, true);
} catch (err) { } catch (err) {
console.log(err); this.logger.error(err);
} }
this.diagramListings.put({_id: defaultConfig.currentDiagramId, name: "New Diagram"}); this.diagramListings.put({_id: defaultConfig.currentDiagramId, name: "New Diagram"});
@ -201,12 +202,12 @@ export class PouchdbPersistenceManager implements IPersistenceManager {
this.updateObserver.notifyObservers(entity.doc, 1); this.updateObserver.notifyObservers(entity.doc, 1);
} }
} catch (err) { } catch (err) {
this.logger.error(err);
} }
} }
syncDoc = function (info) { syncDoc = function (info) {
console.log(info); this.logger.info(info);
if (info.direction == 'pull') { if (info.direction == 'pull') {
const docs = info.change.docs; const docs = info.change.docs;
for (const doc of docs) { for (const doc of docs) {
@ -225,7 +226,7 @@ export class PouchdbPersistenceManager implements IPersistenceManager {
async changeColor(oldColor: Color3, newColor: Color3) { async changeColor(oldColor: Color3, newColor: Color3) {
const all = await this.db.allDocs({include_docs: true}); const all = await this.db.allDocs({include_docs: true});
for (const entity of all.rows) { for (const entity of all.rows) {
console.log(`comparing ${entity.doc.color} to ${oldColor.toHexString()}`); this.logger.debug(`comparing ${entity.doc.color} to ${oldColor.toHexString()}`);
if (entity.doc.color == oldColor.toHexString()) { if (entity.doc.color == oldColor.toHexString()) {
entity.doc.color = newColor.toHexString(); entity.doc.color = newColor.toHexString();
this.db.put({...entity.doc, _rev: entity.doc._rev}); this.db.put({...entity.doc, _rev: entity.doc._rev});
@ -244,27 +245,29 @@ export class PouchdbPersistenceManager implements IPersistenceManager {
private async beginSync() { private async beginSync() {
try { try {
const syncTarget = "user123"; const remoteDbName = "db1";
const dbs = await axios.get(import.meta.env.VITE_SYNCDB_ENDPOINT); const remoteUserName = "user1";
if (dbs.data.indexOf(syncTarget) == -1) { const password = "password";
console.log('sync target missing'); const dbs = await axios.get(import.meta.env.VITE_SYNCDB_ENDPOINT + '_all_dbs');
if (dbs.data.indexOf(remoteDbName) == -1) {
this.logger.warn('sync target missing');
const buildTarget = await axios.post(import.meta.env.VITE_USER_ENDPOINT, const buildTarget = await axios.post(import.meta.env.VITE_USER_ENDPOINT,
{username: syncTarget, password: 'password', db: syncTarget}); {username: remoteUserName, password: password, db: remoteDbName});
if (buildTarget.status != 200) { if (buildTarget.status != 200) {
console.log(buildTarget.statusText); this.logger.info(buildTarget.statusText);
return; return;
} }
} }
console.log(dbs); this.logger.debug(dbs);
this.remote = new PouchDB('https://syncdb-service-d3f974de56ef.herokuapp.com/' + syncTarget, this.remote = new PouchDB(import.meta.env.VITE_SYNCDB_ENDPOINT + remoteDbName,
{auth: {username: syncTarget, password: 'password'}}); {auth: {username: remoteUserName, password: password}});
this.syncDoc = this.syncDoc.bind(this); this.syncDoc = this.syncDoc.bind(this);
this.db.sync(this.remote, {live: true, retry: true}) this.db.sync(this.remote, {live: true, retry: true})
.on('change', this.syncDoc); .on('change', this.syncDoc);
} catch (err) { } catch (err) {
console.log(err); this.logger.error(err);
} }
} }
} }