Browse Source

Avoid recreating databases

develop
Ezerous 4 years ago
parent
commit
e1ea15b79f
  1. 1
      src/orbit/orbitActions.js
  2. 7
      src/orbit/orbitConstants.js
  3. 16
      src/orbit/orbitSaga.js
  4. 25
      src/orbit/orbitUtils.js

1
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';

7
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';

16
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,15 +68,20 @@ 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 });
@ -91,6 +94,9 @@ function * createDatabase({ orbit, db }) {
yield call([createdDB, createdDB.load]);
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:');

25
src/orbit/orbitUtils.js

@ -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;
}
Loading…
Cancel
Save