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 /build
/src/build /src/build
/app/src/build /app/src/build
/app/build
# Contract Artifacts # Contract Artifacts
/app/src/contracts /app/src/contracts

1
app/package.json

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

2
app/src/components/Post.js

@ -82,7 +82,7 @@ class Post extends Component {
orbitPostData = orbitDB.postsDB.get(postID); orbitPostData = orbitDB.postsDB.get(postID);
} else { } else {
const fullAddress = `/orbitdb/${postData.value[0]}/posts`; 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(); await store.load();
const localOrbitData = store.get(postID); 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); const orbitData = orbitDB.topicsDB.get(topicID);
topicSubject = orbitData.subject; topicSubject = orbitData.subject;
} else { } else {
const fullAddress = `/orbitdb/${topicData.value[0] const fullAddress = `/orbitdb/${topicData.value[0]}/topics`;
}/topics`; const store = await orbitDB.orbitdb.open(fullAddress, {type: 'keyvalue'});
const store = await orbitDB.orbitdb.keyvalue(fullAddress);
await store.load(); await store.load();
const localOrbitData = store.get(topicID); 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' '/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_CREATED = 'DATABASES_CREATED';
const DATABASES_LOADED = 'DATABASES_LOADED'; const DATABASES_LOADED = 'DATABASES_LOADED';
const DATABASES_NOT_READY = 'DATABASES_NOT_READY'; const DATABASES_NOT_READY = 'DATABASES_NOT_READY';
const UPDATE_PEERS = 'UPDATE_PEERS';
function updateDatabases(type, orbitdb, topicsDB, postsDB) { function updateDatabases(type, orbitdb, topicsDB, postsDB) {
return { return {
@ -17,4 +18,5 @@ export { DATABASES_CREATED,
DATABASES_LOADED, DATABASES_LOADED,
DATABASES_NOT_READY, DATABASES_NOT_READY,
IPFS_INITIALIZED, IPFS_INITIALIZED,
UPDATE_PEERS,
updateDatabases }; updateDatabases };

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

@ -1,7 +1,9 @@
import { DATABASES_CREATED, import {
DATABASES_CREATED,
DATABASES_LOADED, DATABASES_LOADED,
DATABASES_NOT_READY, DATABASES_NOT_READY,
IPFS_INITIALIZED } from '../actions/orbitActions'; IPFS_INITIALIZED, UPDATE_PEERS
} from '../actions/orbitActions';
const initialState = { const initialState = {
ipfs: null, ipfs: null,
@ -10,6 +12,8 @@ const initialState = {
orbitdb: null, orbitdb: null,
topicsDB: null, topicsDB: null,
postsDB: null, postsDB: null,
topicsDBPeers: [],
postsDBPeers: [],
id: null id: null
}; };
@ -48,6 +52,12 @@ const orbitReducer = (state = initialState, action) => {
postsDB: null, postsDB: null,
id: null id: null
}; };
case UPDATE_PEERS:
return {
...state,
topicsDBPeers: action.topicsDBPeers,
postsDBPeers: action.postsDBPeers
};
default: default:
return state; 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 { contract, getCurrentAccount } from './drizzleUtilsSaga';
import { loadDatabases } from '../../utils/orbitUtils'; import { loadDatabases } from '../../utils/orbitUtils';
import { DRIZZLE_UTILS_SAGA_INITIALIZED } from '../actions/drizzleUtilsActions'; 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; let latestAccount;
@ -19,8 +20,6 @@ function* getOrbitDBInfo() {
address: account address: account
}); });
if (callResult) { if (callResult) {
// console.log("Deleting local storage..");
// localStorage.clear();
const txObj2 = yield call(contract.methods.getOrbitIdentityInfo, const txObj2 = yield call(contract.methods.getOrbitIdentityInfo,
...[account]); ...[account]);
const orbitIdentityInfo = yield call(txObj2.call, { 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() { function* orbitSaga() {
yield all([ yield all([
take(DRIZZLE_UTILS_SAGA_INITIALIZED), take(DRIZZLE_UTILS_SAGA_INITIALIZED),
take(IPFS_INITIALIZED) take(IPFS_INITIALIZED)
]); ]);
yield takeLatest('ACCOUNT_CHANGED', getOrbitDBInfo); yield takeLatest('ACCOUNT_CHANGED', getOrbitDBInfo);
yield takeEvery('ACCOUNTS_FETCHED', updatePeersState);
} }
export default orbitSaga; export default orbitSaga;

2
app/src/utils/orbitUtils.js

@ -17,6 +17,8 @@ function initIPFS() {
} }
async function createDatabases() { 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...'); console.log('Creating databases...');
const ipfs = getIPFS(); const ipfs = getIPFS();
const orbitdb = await new OrbitDB(ipfs); const orbitdb = await new OrbitDB(ipfs);

Loading…
Cancel
Save