Browse Source

Orbit/TopicList improvements

develop
Ezerous 6 years ago
parent
commit
1f64421da4
  1. 2
      app/package.json
  2. 74
      app/src/components/Topic.js
  3. 19
      app/src/utils/orbitUtils.js

2
app/package.json

@ -28,7 +28,7 @@
"redux-saga": "0.16.2", "redux-saga": "0.16.2",
"semantic-ui-react": "0.86.0", "semantic-ui-react": "0.86.0",
"uuid": "3.3.2", "uuid": "3.3.2",
"web3": "1.0.0-beta.48" "web3": "1.0.0-beta.50"
}, },
"scripts": { "scripts": {
"start": "react-scripts start", "start": "react-scripts start",

74
app/src/components/Topic.js

@ -23,56 +23,52 @@ class Topic extends Component {
} }
componentDidMount() { componentDidMount() {
const { topicSubjectFetchStatus } = this.state; this.fetchSubject(this.props.topicID);
const { topicData, orbitDB, topicID } = this.props;
if (topicData !== null
&& topicSubjectFetchStatus === 'pending'
&& orbitDB.ipfsInitialized
&& orbitDB.orbitdb) {
this.fetchSubject(topicID);
}
} }
componentDidUpdate() { componentDidUpdate() {
const { topicSubjectFetchStatus } = this.state; this.fetchSubject(this.props.topicID);
const { topicData, orbitDB, topicID } = this.props;
if (topicData !== null
&& topicSubjectFetchStatus === 'pending'
&& orbitDB.ipfsInitialized
&& orbitDB.orbitdb) {
this.fetchSubject(topicID);
}
} }
async fetchSubject(topicID) { async fetchSubject(topicID) {
const { topicSubjectFetchStatus } = this.state;
const { topicData, user, orbitDB } = this.props; const { topicData, user, orbitDB } = this.props;
let topicSubject;
if (topicData !== null
if (topicData.value[1] === user.address) { && topicSubjectFetchStatus === 'pending'
const orbitData = orbitDB.topicsDB.get(topicID); && orbitDB.ipfsInitialized
topicSubject = orbitData.subject; && orbitDB.orbitdb) {
} else {
const fullAddress = `/orbitdb/${topicData.value[0]}/topics`; let topicSubject;
const store = await orbitDB.orbitdb.open(fullAddress, {type: 'keyvalue'});
await store.load(); if (topicData.value[1] === user.address) {
const orbitData = orbitDB.topicsDB.get(topicID);
const localOrbitData = store.get(topicID); if(orbitData)
if (localOrbitData) { topicSubject = orbitData.subject;
topicSubject = localOrbitData.subject;
} else { } else {
// Wait until we have received something from the network const fullAddress = `/orbitdb/${topicData.value[0]}/topics`;
const store = await orbitDB.orbitdb.open(fullAddress, {type: 'keyvalue'});
await store.load();
const localOrbitData = store.get(topicID);
if (localOrbitData)
topicSubject = localOrbitData.subject;
store.events.on('replicate', () => {
console.log("Initiated OrbitDB data replication.");
});
store.events.on('replicated', () => { store.events.on('replicated', () => {
console.log("OrbitDB data replicated successfully.");
topicSubject = store.get(topicID).subject; topicSubject = store.get(topicID).subject;
}); });
} }
}
this.setState({ this.setState({
topicSubject, topicSubject,
topicSubjectFetchStatus: 'fetched' topicSubjectFetchStatus: 'fetched'
}); });
}
} }
render() { render() {
@ -93,7 +89,7 @@ class Topic extends Component {
> >
<p> <p>
<strong> <strong>
{topicSubject !== null ? topicSubject {(topicSubject) ? topicSubject
: ( : (
<ContentLoader <ContentLoader
height={5.8} height={5.8}
@ -147,7 +143,7 @@ Topic.propTypes = {
history: PropTypes.object.isRequired, history: PropTypes.object.isRequired,
topicData: GetTopicResult.isRequired, topicData: GetTopicResult.isRequired,
orbitDB: PropTypes.object.isRequired, orbitDB: PropTypes.object.isRequired,
topicID: PropTypes.string.isRequired topicID: PropTypes.number.isRequired
}; };
const mapStateToProps = state => ({ const mapStateToProps = state => ({

19
app/src/utils/orbitUtils.js

@ -8,6 +8,7 @@ import ipfsOptions from '../config/ipfsOptions';
function initIPFS() { function initIPFS() {
const ipfs = new IPFS(ipfsOptions); const ipfs = new IPFS(ipfsOptions);
ipfs.on('error', (error) => console.error(`IPFS error: ${error}`));
ipfs.on('ready', async () => { ipfs.on('ready', async () => {
store.dispatch({ store.dispatch({
type: IPFS_INITIALIZED, ipfs type: IPFS_INITIALIZED, ipfs
@ -43,9 +44,8 @@ async function createDatabases() {
} }
async function loadDatabases(identityId, identityPublicKey, identityPrivateKey, async function loadDatabases(identityId, identityPublicKey, identityPrivateKey,
orbitId, orbitPublicKey, orbitPrivateKey, orbitId, orbitPublicKey, orbitPrivateKey,
topicsDBId, postsDBId) { topicsDBId, postsDBId) {
console.log('Loading databases...');
const directory = './orbitdb'; const directory = './orbitdb';
const keystore = Keystore.create(path.join(directory, orbitId, '/keystore')); const keystore = Keystore.create(path.join(directory, orbitId, '/keystore'));
@ -59,12 +59,15 @@ async function loadDatabases(identityId, identityPublicKey, identityPrivateKey,
{ {
peerId: orbitId, keystore peerId: orbitId, keystore
}); });
const topicsDB = await orbitdb.keyvalue(`/orbitdb/${topicsDBId}/topics`); const topicsDB = await orbitdb.keyvalue(`/orbitdb/${topicsDBId}/topics`)
const postsDB = await orbitdb.keyvalue(`/orbitdb/${postsDBId}/posts`); .catch((error) => console.error(`TopicsDB init error: ${error}`));
const postsDB = await orbitdb.keyvalue(`/orbitdb/${postsDBId}/posts`)
.catch((error) => console.error(`PostsDB init error: ${error}`));
await topicsDB.load(); await topicsDB.load().catch((error) => console.error(`TopicsDB loading error: ${error}`));
await postsDB.load(); await postsDB.load().catch((error) => console.error(`PostsDB loading error: ${error}`));
console.log('Orbit databases loaded successfully.');
store.dispatch(updateDatabases(DATABASES_LOADED, orbitdb, topicsDB, postsDB)); store.dispatch(updateDatabases(DATABASES_LOADED, orbitdb, topicsDB, postsDB));
} }
@ -73,7 +76,7 @@ function getIPFS() {
} }
async function orbitSagaPut(db, key, value) { async function orbitSagaPut(db, key, value) {
db.put(key, value); await db.put(key, value).catch((error) => console.error(`Orbit put error: ${error}`));
} }
export { initIPFS, createDatabases, loadDatabases, orbitSagaPut }; export { initIPFS, createDatabases, loadDatabases, orbitSagaPut };

Loading…
Cancel
Save