Browse Source

Version 0.3.0

master v0.3.0
Ezerous 4 years ago
parent
commit
606319f0f2
  1. 4
      package.json
  2. 2
      src/orbit/orbitActions.js
  3. 8
      src/orbit/orbitConstants.js
  4. 20
      src/orbit/orbitReducer.js
  5. 25
      src/orbit/orbitSaga.js
  6. 25
      src/orbit/orbitUtils.js
  7. 2386
      yarn.lock

4
package.json

@ -1,6 +1,6 @@
{
"name": "@ezerous/breeze",
"version": "0.2.0",
"version": "0.3.0",
"description": "A reactive data-store for OrbitDB.",
"license": "MIT",
"author": "Ezerous <ezerous@gmail.com>",
@ -8,7 +8,7 @@
"repository": "github:Ezerous/breeze",
"dependencies": {
"deepmerge": "~4.2.2",
"ipfs": "~0.50.2",
"ipfs": "~0.51.0",
"is-plain-object": "~5.0.0",
"orbit-db": "~0.26.0",
"orbit-db-identity-provider": "~0.3.1",

2
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';
@ -18,6 +19,7 @@ export const ORBIT_DATABASE_LISTEN = 'ORBIT_DATABASE_LISTEN';
export const ORBIT_DATABASE_READY = 'ORBIT_DATABASE_READY';
export const ORBIT_DATABASE_REPLICATING = 'ORBIT_DATABASE_REPLICATING';
export const ORBIT_DATABASE_REPLICATED = 'ORBIT_DATABASE_REPLICATED';
export const ORBIT_DATABASE_WRITE = 'ORBIT_DATABASE_WRITE';
export function orbitInit (breeze, id) {
return {

8
src/orbit/orbitConstants.js

@ -1,12 +1,6 @@
// 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';
export const DB_STATUS_REPLICATING = 'replicating';
export const DB_STATUS_REPLICATED = 'replicated';
export const DB_STATUS_WRITTEN = 'written';

20
src/orbit/orbitReducer.js

@ -5,7 +5,8 @@ import {
ORBIT_DATABASE_REPLICATING,
ORBIT_INITIALIZING,
ORBIT_INITIALIZED,
ORBIT_INIT_FAILED
ORBIT_INIT_FAILED,
ORBIT_DATABASE_WRITE
} from "./orbitActions";
import {
@ -13,6 +14,7 @@ import {
DB_STATUS_READY,
DB_STATUS_REPLICATED,
DB_STATUS_REPLICATING,
DB_STATUS_WRITTEN
} from "./orbitConstants";
import {STATUS_INITIALIZING, STATUS_INITIALIZED, STATUS_FAILED } from "../constants";
@ -47,24 +49,32 @@ const orbitReducer = (state = initialState, action) => {
return newDatabasesStatus(state, action, DB_STATUS_REPLICATING);
case ORBIT_DATABASE_REPLICATED:
return newDatabasesStatus(state, action, DB_STATUS_REPLICATED);
case ORBIT_DATABASE_WRITE:
return newDatabasesStatus(state, action, DB_STATUS_WRITTEN);
default:
return state;
}
};
function newDatabasesStatus (state, action, status) {
const { timestamp, database: {id} } = action;
// Previous values, if exist
const lastReplication = state.databases[id] ? state.databases[id].lastReplication : null;
const lastWrite = state.databases[id] ? state.databases[id].lastWrite : null;
return {
...state,
databases:{
...state.databases,
[action.database.id]: {
...state[action.database.id],
[id]: {
...state.databases[id],
status,
timestamp: action.timestamp
timestamp,
lastReplication: status === DB_STATUS_REPLICATED ? timestamp : lastReplication,
lastWrite: status === DB_STATUS_WRITTEN ? timestamp : lastWrite,
}
}
}
}
export default orbitReducer;

25
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,
@ -11,6 +12,7 @@ import {
ORBIT_DATABASE_READY,
ORBIT_DATABASE_REPLICATED,
ORBIT_DATABASE_REPLICATING,
ORBIT_DATABASE_WRITE,
ORBIT_IDENTITY_PROVIDER_ADD,
ORBIT_IDENTITY_PROVIDER_ADDED,
ORBIT_IDENTITY_PROVIDER_FAILED,
@ -19,11 +21,8 @@ import {
ORBIT_INITIALIZING
} from './orbitActions';
import { resolveOrbitDBTypeFun} from './orbitUtils';
const LOGGING_PREFIX = 'orbitSaga: ';
/*
* Add Orbit Identity Provider
*/
@ -69,14 +68,20 @@ function * initOrbit(action) {
}
}
// Keeps track of added databases, so no duplicates are attempted to be created
let databases = new Set();
/*
* Creates an orbit database given a name and a type as its parameters
* 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 });
@ -89,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:');
@ -112,16 +120,21 @@ function createOrbitDatabaseChannel (database){
const onReplicated = () => {
emit({ type: ORBIT_DATABASE_REPLICATED, database, timestamp: +new Date });
};
const onWrite = (address, entry) => {
emit({ type: ORBIT_DATABASE_WRITE, database, entry, timestamp: +new Date });
};
const eventListener = database.events
.once('ready', onReady)
.on('replicate', onReplicate)
.on('replicated', onReplicated)
.on('write', onWrite)
return () => {
eventListener.removeListener('ready',onReady)
eventListener.removeListener('replicate',onReplicate)
eventListener.removeListener('replicated',onReplicated)
eventListener.removeListener('write',onWrite)
};
})

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

2386
yarn.lock

File diff suppressed because it is too large
Loading…
Cancel
Save