Browse Source

Orbit improvements

develop
Ezerous 6 years ago
parent
commit
48207bf551
  1. 1
      .gitignore
  2. 1
      app/package.json
  3. 2
      app/src/components/Post.js
  4. 5
      app/src/components/Topic.js
  5. 3
      app/src/config/ipfsOptions.js
  6. 2
      app/src/redux/actions/orbitActions.js
  7. 14
      app/src/redux/reducers/orbitReducer.js
  8. 24
      app/src/redux/sagas/orbitSaga.js
  9. 2
      app/src/utils/orbitUtils.js

1
.gitignore

@ -13,6 +13,7 @@ yarn.lock
/build
/src/build
/app/src/build
/app/build
# Contract Artifacts
/app/src/contracts

1
app/package.json

@ -11,6 +11,7 @@
"drizzle": "1.3.3",
"history": "4.9.0",
"ipfs": "github:ipfs/js-ipfs#e849dbcab4a313f7ffc1532a389097ee76344067",
"lodash.isequal": "4.5.0",
"orbit-db": "0.19.9",
"orbit-db-keystore": "0.1.0",
"prop-types": "15.7.2",

2
app/src/components/Post.js

@ -82,7 +82,7 @@ class Post extends Component {
orbitPostData = orbitDB.postsDB.get(postID);
} else {
const fullAddress = `/orbitdb/${postData.value[0]}/posts`;
const store = await orbitDB.orbitdb.keyvalue(fullAddress);
const store = await orbitDB.orbitdb.open(fullAddress, {type: 'keyvalue'});
await store.load();
const localOrbitData = store.get(postID);

5
app/src/components/Topic.js

@ -54,9 +54,8 @@ class Topic extends Component {
const orbitData = orbitDB.topicsDB.get(topicID);
topicSubject = orbitData.subject;
} else {
const fullAddress = `/orbitdb/${topicData.value[0]
}/topics`;
const store = await orbitDB.orbitdb.keyvalue(fullAddress);
const fullAddress = `/orbitdb/${topicData.value[0]}/topics`;
const store = await orbitDB.orbitdb.open(fullAddress, {type: 'keyvalue'});
await store.load();
const localOrbitData = store.get(topicID);

3
app/src/config/ipfsOptions.js

@ -13,6 +13,9 @@ const ipfsOptions = {
'/ip4/127.0.0.1/tcp/9090/ws/p2p-websocket-star'
]
}
},
preload: {
enabled: false
}
};

2
app/src/redux/actions/orbitActions.js

@ -2,6 +2,7 @@ const IPFS_INITIALIZED = 'IPFS_INITIALIZED';
const DATABASES_CREATED = 'DATABASES_CREATED';
const DATABASES_LOADED = 'DATABASES_LOADED';
const DATABASES_NOT_READY = 'DATABASES_NOT_READY';
const UPDATE_PEERS = 'UPDATE_PEERS';
function updateDatabases(type, orbitdb, topicsDB, postsDB) {
return {
@ -17,4 +18,5 @@ export { DATABASES_CREATED,
DATABASES_LOADED,
DATABASES_NOT_READY,
IPFS_INITIALIZED,
UPDATE_PEERS,
updateDatabases };

14
app/src/redux/reducers/orbitReducer.js

@ -1,7 +1,9 @@
import { DATABASES_CREATED,
import {
DATABASES_CREATED,
DATABASES_LOADED,
DATABASES_NOT_READY,
IPFS_INITIALIZED } from '../actions/orbitActions';
IPFS_INITIALIZED, UPDATE_PEERS
} from '../actions/orbitActions';
const initialState = {
ipfs: null,
@ -10,6 +12,8 @@ const initialState = {
orbitdb: null,
topicsDB: null,
postsDB: null,
topicsDBPeers: [],
postsDBPeers: [],
id: null
};
@ -48,6 +52,12 @@ const orbitReducer = (state = initialState, action) => {
postsDB: null,
id: null
};
case UPDATE_PEERS:
return {
...state,
topicsDBPeers: action.topicsDBPeers,
postsDBPeers: action.postsDBPeers
};
default:
return state;
}

24
app/src/redux/sagas/orbitSaga.js

@ -1,8 +1,9 @@
import { all, call, put, take, takeLatest } from 'redux-saga/effects';
import { all, call, put, select, take, takeEvery, takeLatest } from 'redux-saga/effects';
import isEqual from 'lodash.isequal';
import { contract, getCurrentAccount } from './drizzleUtilsSaga';
import { loadDatabases } from '../../utils/orbitUtils';
import { DRIZZLE_UTILS_SAGA_INITIALIZED } from '../actions/drizzleUtilsActions';
import { DATABASES_NOT_READY, IPFS_INITIALIZED } from '../actions/orbitActions';
import { DATABASES_NOT_READY, IPFS_INITIALIZED, UPDATE_PEERS } from '../actions/orbitActions';
let latestAccount;
@ -19,8 +20,6 @@ function* getOrbitDBInfo() {
address: account
});
if (callResult) {
// console.log("Deleting local storage..");
// localStorage.clear();
const txObj2 = yield call(contract.methods.getOrbitIdentityInfo,
...[account]);
const orbitIdentityInfo = yield call(txObj2.call, {
@ -51,12 +50,29 @@ function* getOrbitDBInfo() {
}
}
function* updatePeersState() {
const orbit = yield select(state => state.orbit);
if(orbit.ready){
const topicsDBAddress = orbit.topicsDB.address.toString();
const postsDBAddress = orbit.postsDB.address.toString();
const topicsDBPeers = yield call(orbit.ipfs.pubsub.peers, topicsDBAddress);
const postsDBPeers = yield call(orbit.ipfs.pubsub.peers, postsDBAddress);
if(!isEqual(topicsDBPeers.sort(), orbit.topicsDBPeers.sort()) ||
!isEqual(postsDBPeers.sort(), orbit.postsDBPeers.sort())){
yield put({
type: UPDATE_PEERS, topicsDBPeers, postsDBPeers
});
}
}
}
function* orbitSaga() {
yield all([
take(DRIZZLE_UTILS_SAGA_INITIALIZED),
take(IPFS_INITIALIZED)
]);
yield takeLatest('ACCOUNT_CHANGED', getOrbitDBInfo);
yield takeEvery('ACCOUNTS_FETCHED', updatePeersState);
}
export default orbitSaga;

2
app/src/utils/orbitUtils.js

@ -17,6 +17,8 @@ function initIPFS() {
}
async function createDatabases() {
console.log("Deleting local storage..."); // Else we are in danger of reusing an existing orbit
localStorage.clear(); // Perhaps not needed at all when orbit ids are used in Orbit 0.20.x+
console.log('Creating databases...');
const ipfs = getIPFS();
const orbitdb = await new OrbitDB(ipfs);

Loading…
Cancel
Save