Browse Source

Minor changes, refactoring

develop
Ezerous 4 years ago
parent
commit
06186d246a
  1. 4
      package.json
  2. 1
      src/Breeze.js
  3. 4
      src/breezeStatus/breezeStatusSaga.js
  4. 19
      src/index.js
  5. 18
      src/orbit/orbitMiddleware.js
  6. 66
      src/orbit/orbitSaga.js
  7. 61
      src/orbit/orbitStatusSaga.js
  8. 2523
      yarn.lock

4
package.json

@ -8,9 +8,9 @@
"repository": "github:Ezerous/breeze",
"dependencies": {
"deepmerge": "4.2.2",
"ipfs": "0.50.1",
"ipfs": "0.48.2",
"is-plain-object": "4.1.1",
"orbit-db": "0.26.0-rc1",
"orbit-db": "0.25.3",
"orbit-db-identity-provider": "0.3.1",
"redux-saga": "1.1.3"
}

1
src/Breeze.js

@ -25,7 +25,6 @@ class Breeze {
this.web3 = options.web3;
this.ipfs = {} // To be initialized in ipfsSaga
this.orbit = {} // To be initialized in orbitSaga
this.orbitDatabases = {};
this.ipfsOptions = options.ipfs;
this.orbitOptions = options.orbit;

4
src/breezeStatus/breezeStatusSaga.js

@ -6,7 +6,7 @@ import { addOrbitIdentityProvider } from '../orbit/orbitSaga';
const LOGGING_PREFIX = 'breezeStatusSaga: ';
export function * initializeBreeze (action) {
function * initializeBreeze (action) {
try {
const { breeze } = action;
@ -31,7 +31,7 @@ export function * initializeBreeze (action) {
}
function * breezeStatusSaga () {
yield takeLatest(BreezeActions.BREEZE_INITIALIZING, initializeBreeze)
yield takeLatest(BreezeActions.BREEZE_INITIALIZING, initializeBreeze);
}
export default breezeStatusSaga

19
src/index.js

@ -1,7 +1,7 @@
import Breeze from './Breeze.js'
import breezeStatusReducer from './breezeStatus/breezeStatusReducer';
import ipfsReducer from "./ipfs/ipfsReducer";
import orbitReducer from "./orbit/orbitReducer";
import ipfsReducer from './ipfs/ipfsReducer';
import orbitReducer from './orbit/orbitReducer';
import breezeStatusSaga from './breezeStatus/breezeStatusSaga';
import orbitSaga from "./orbit/orbitSaga";
@ -10,10 +10,7 @@ import * as BreezeActions from './breezeStatus/breezeActions'
import * as OrbitActions from './orbit/orbitActions'
import * as breezeConstants from './constants'
import * as orbitTypes from './orbit/orbitConstants'
import orbitMiddleware from "./orbit/orbitMiddleware";
import orbitStatusSaga from "./orbit/orbitStatusSaga";
import * as orbitConstants from './orbit/orbitConstants'
const breezeReducers = {
breezeStatus: breezeStatusReducer,
@ -21,14 +18,9 @@ const breezeReducers = {
orbit: orbitReducer
}
const breezeMiddlewares = [
orbitMiddleware
]
const breezeSagas = [
breezeStatusSaga,
orbitSaga,
orbitStatusSaga
orbitSaga
]
const breezeActions = {
@ -41,8 +33,7 @@ export {
breezeConstants,
breezeActions,
breezeReducers,
breezeMiddlewares,
breezeSagas,
orbitTypes
orbitConstants
}

18
src/orbit/orbitMiddleware.js

@ -1,18 +0,0 @@
import {ORBIT_DATABASE_CREATED} from "./orbitActions";
import {BREEZE_INITIALIZED} from "../breezeStatus/breezeActions";
export const orbitMiddleware = breezeInstance => () => next => action => {
const { type } = action
if (type === BREEZE_INITIALIZED)
breezeInstance = action.breeze
if (type === ORBIT_DATABASE_CREATED) {
const { database } = action;
breezeInstance.orbitDatabases[database.id] = database;
}
return next(action);
}
const initializedMiddleware = orbitMiddleware(undefined)
export default initializedMiddleware

66
src/orbit/orbitSaga.js

@ -1,11 +1,16 @@
import {all, call, put, take, takeLatest} from 'redux-saga/effects'
import { all, call, put, spawn, take, takeEvery, takeLatest } from 'redux-saga/effects'
import { eventChannel } from 'redux-saga';
import OrbitDB from 'orbit-db';
import Identities from 'orbit-db-identity-provider'
import {
ORBIT_DATABASE_CREATED,
ORBIT_DATABASE_CREATING,
ORBIT_DATABASE_FAILED,
ORBIT_DATABASE_LISTEN,
ORBIT_DATABASE_READY,
ORBIT_DATABASE_REPLICATED,
ORBIT_DATABASE_REPLICATING,
ORBIT_IDENTITY_PROVIDER_ADD,
ORBIT_IDENTITY_PROVIDER_ADDED,
ORBIT_IDENTITY_PROVIDER_FAILED,
@ -14,10 +19,11 @@ import {
ORBIT_INITIALIZING
} from './orbitActions';
import { resolveOrbitDBTypeFun} from "./orbitUtils";
import { resolveOrbitDBTypeFun} from './orbitUtils';
const LOGGING_PREFIX = 'orbitSaga: ';
/*
* Add Orbit Identity Provider
*/
@ -36,7 +42,7 @@ export function * addOrbitIdentityProvider(identityProvider) {
}
}
export function * initOrbit(action) {
function * initOrbit(action) {
try {
let { breeze, id } = action;
const { ipfs } = breeze;
@ -48,7 +54,7 @@ export function * initOrbit(action) {
breeze.orbit = orbit;
// Create our own initial databases, as given in the options
// Create initial databases from options
yield all(databases.map(db => {
return call(createDatabase, { orbit, db });
}));
@ -66,7 +72,7 @@ export function * initOrbit(action) {
/*
* Creates an orbit database given a name and a type as its parameters
*/
export function * createDatabase({orbit, db}) {
function * createDatabase({ orbit, db }) {
try {
const dbTypeFun = resolveOrbitDBTypeFun(orbit, db.type);
@ -74,6 +80,9 @@ export function * createDatabase({orbit, db}) {
yield put({ type: ORBIT_DATABASE_CREATED, database: createdDB, timestamp: +new Date });
// 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);
@ -87,9 +96,54 @@ export function * createDatabase({orbit, db}) {
}
}
/*
* Database Events
* See also https://redux-saga.js.org/docs/advanced/Channels.html
*/
function createOrbitDatabaseChannel (database){
return eventChannel(emit => {
const onReady = () => {
emit({ type: ORBIT_DATABASE_READY, database, timestamp: +new Date });
};
const onReplicate = () => {
emit({ type: ORBIT_DATABASE_REPLICATING, database, timestamp: +new Date });
};
const onReplicated = () => {
emit({ type: ORBIT_DATABASE_REPLICATED, database, timestamp: +new Date });
};
const eventListener = database.events
.once('ready', onReady)
.on('replicate', onReplicate)
.on('replicated', onReplicated)
return () => {
eventListener.removeListener('ready',onReady)
eventListener.removeListener('replicate',onReplicate)
eventListener.removeListener('replicated',onReplicated)
};
})
}
function * callListenForOrbitDatabaseEvent ({ database }) {
const orbitDatabaseChannel = yield call(createOrbitDatabaseChannel, database);
yield put({type: ORBIT_DATABASE_LISTEN, id: database.id});
try {
while (true) {
let event = yield take(orbitDatabaseChannel);
yield put(event);
}
} finally {
orbitDatabaseChannel.close();
}
}
function * orbitSaga () {
yield takeLatest(ORBIT_INITIALIZING, initOrbit);
yield takeLatest(ORBIT_DATABASE_CREATING, createDatabase);
yield takeEvery(ORBIT_DATABASE_CREATING, createDatabase);
}
export default orbitSaga

61
src/orbit/orbitStatusSaga.js

@ -1,61 +0,0 @@
import { call, put, take, takeEvery } from 'redux-saga/effects'
import {eventChannel} from "@redux-saga/core";
import {
ORBIT_DATABASE_CREATED,
ORBIT_DATABASE_LISTEN,
ORBIT_DATABASE_READY,
ORBIT_DATABASE_REPLICATED,
ORBIT_DATABASE_REPLICATING
} from './orbitActions';
/*
* Database Events
* See also https://redux-saga.js.org/docs/advanced/Channels.html
*/
function createOrbitDatabaseChannel (database){
return eventChannel(emit => {
const onReady = () => {
emit({ type: ORBIT_DATABASE_READY, database, timestamp: +new Date });
};
const onReplicate = () => {
emit({ type: ORBIT_DATABASE_REPLICATING, database, timestamp: +new Date });
};
const onReplicated = () => {
emit({ type: ORBIT_DATABASE_REPLICATED, database, timestamp: +new Date });
};
const eventListener = database.events
.once('ready', onReady)
.on('replicate', onReplicate)
.on('replicated', onReplicated)
return () => {
eventListener.removeListener('ready',onReady)
eventListener.removeListener('replicate',onReplicate)
eventListener.removeListener('replicated',onReplicated)
};
})
}
export function * callListenForOrbitDatabaseEvent ({database}) {
const orbitDatabaseChannel = yield call(createOrbitDatabaseChannel, database)
yield put({type: ORBIT_DATABASE_LISTEN, id: database.id});
try {
while (true) {
let event = yield take(orbitDatabaseChannel);
yield put(event);
}
} finally {
orbitDatabaseChannel.close();
}
}
function * orbitStatusSaga () {
yield takeEvery(ORBIT_DATABASE_CREATED, callListenForOrbitDatabaseEvent);
}
export default orbitStatusSaga

2523
yarn.lock

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