From e1ea15b79fe6a24692c3577766a120c81715d43c Mon Sep 17 00:00:00 2001 From: Ezerous Date: Thu, 5 Nov 2020 22:09:24 +0200 Subject: [PATCH] Avoid recreating databases --- src/orbit/orbitActions.js | 1 + src/orbit/orbitConstants.js | 7 ------- src/orbit/orbitSaga.js | 30 ++++++++++++++++++------------ src/orbit/orbitUtils.js | 25 ------------------------- 4 files changed, 19 insertions(+), 44 deletions(-) delete mode 100644 src/orbit/orbitUtils.js diff --git a/src/orbit/orbitActions.js b/src/orbit/orbitActions.js index 9a2f8a4..c317166 100644 --- a/src/orbit/orbitActions.js +++ b/src/orbit/orbitActions.js @@ -11,6 +11,7 @@ export const ORBIT_IDENTITY_PROVIDER_FAILED = 'ORBIT_IDENTITY_PROVIDER_FAILED'; // Database Status export const ORBIT_DATABASE_CREATING = 'ORBIT_DATABASE_CREATING'; export const ORBIT_DATABASE_CREATED = 'ORBIT_DATABASE_CREATED'; +export const ORBIT_DATABASE_ALREADY_EXISTS = 'ORBIT_DATABASE_ALREADY_EXISTS'; export const ORBIT_DATABASE_FAILED = 'ORBIT_DATABASE_FAILED'; export const ORBIT_DATABASE_LISTEN = 'ORBIT_DATABASE_LISTEN'; diff --git a/src/orbit/orbitConstants.js b/src/orbit/orbitConstants.js index 0574ce2..987efd7 100644 --- a/src/orbit/orbitConstants.js +++ b/src/orbit/orbitConstants.js @@ -1,10 +1,3 @@ -// All valid OrbitDB types -export const ORBIT_TYPE_LOG = 'log'; -export const ORBIT_TYPE_FEED = 'feed'; -export const ORBIT_TYPE_KEYVALUE = 'keyvalue'; -export const ORBIT_TYPE_DOCS = 'docs'; -export const ORBIT_TYPE_COUNTER = 'counter'; - // OrbitDB Databases status export const DB_STATUS_INIT = 'init'; export const DB_STATUS_READY = 'ready'; diff --git a/src/orbit/orbitSaga.js b/src/orbit/orbitSaga.js index d2634b8..ea3844c 100644 --- a/src/orbit/orbitSaga.js +++ b/src/orbit/orbitSaga.js @@ -4,6 +4,7 @@ import OrbitDB from 'orbit-db'; import Identities from 'orbit-db-identity-provider' import { + ORBIT_DATABASE_ALREADY_EXISTS, ORBIT_DATABASE_CREATED, ORBIT_DATABASE_CREATING, ORBIT_DATABASE_FAILED, @@ -20,11 +21,8 @@ import { ORBIT_INITIALIZING } from './orbitActions'; -import { resolveOrbitDBTypeFun} from './orbitUtils'; - const LOGGING_PREFIX = 'orbitSaga: '; - /* * Add Orbit Identity Provider */ @@ -70,27 +68,35 @@ function * initOrbit(action) { } } +// Keeps track of added databases, so no duplicates are attempted to be created +let databases = new Set(); + /* * Creates and loads an orbit database given a name and a type as its parameters * Note: db.name can also be an OrbitDB address */ function * createDatabase({ orbit, db }) { try { - const dbTypeFun = resolveOrbitDBTypeFun(orbit, db.type); + const { size } = databases; + databases.add(orbit.id + db.name); - const createdDB = yield call([orbit, dbTypeFun], db.name); + if (databases.size > size) { + const createdDB = yield call([orbit, orbit.open], ...[db.name, { type: db.type, create: true }]); - yield put({ type: ORBIT_DATABASE_CREATED, database: createdDB, timestamp: +new Date }); + yield put({ type: ORBIT_DATABASE_CREATED, database: createdDB, timestamp: +new Date }); - // Event channel setup - yield spawn(callListenForOrbitDatabaseEvent, { database: createdDB }); + // Event channel setup + yield spawn(callListenForOrbitDatabaseEvent, { database: createdDB }); - // Wait for event channel setup before loading - yield take(action => action.type === ORBIT_DATABASE_LISTEN && action.id === createdDB.id); + // Wait for event channel setup before loading + yield take(action => action.type === ORBIT_DATABASE_LISTEN && action.id === createdDB.id); - yield call([createdDB, createdDB.load]); + yield call([createdDB, createdDB.load]); - return createdDB; + return createdDB; + } + else + yield put({ type: ORBIT_DATABASE_ALREADY_EXISTS, database: db.name }); } catch (error) { yield put({ type: ORBIT_DATABASE_FAILED, error }); console.error(LOGGING_PREFIX + 'OrbitDB database creation error:'); diff --git a/src/orbit/orbitUtils.js b/src/orbit/orbitUtils.js deleted file mode 100644 index 13cc9ef..0000000 --- a/src/orbit/orbitUtils.js +++ /dev/null @@ -1,25 +0,0 @@ -import * as orbitTypes from "./orbitConstants"; - -export function resolveOrbitDBTypeFun(orbitdb, type){ - let dbTypeFun; - switch(type) { - case orbitTypes.ORBIT_TYPE_LOG: - dbTypeFun = orbitdb.log; - break; - case orbitTypes.ORBIT_TYPE_FEED: - dbTypeFun = orbitdb.feed; - break; - case orbitTypes.ORBIT_TYPE_KEYVALUE: - dbTypeFun = orbitdb.keyvalue; - break; - case orbitTypes.ORBIT_TYPE_DOCS: - dbTypeFun = orbitdb.docs; - break; - case orbitTypes.ORBIT_TYPE_COUNTER: - dbTypeFun = orbitdb.counter; - break; - default: - throw "Invalid OrbitDB type!"; - } - return dbTypeFun; -}