|
@ -5,96 +5,55 @@ import { |
|
|
createOrbitDatabase, |
|
|
createOrbitDatabase, |
|
|
ORBIT_DATABASE_READY, |
|
|
ORBIT_DATABASE_READY, |
|
|
ORBIT_DATABASE_REPLICATED, |
|
|
ORBIT_DATABASE_REPLICATED, |
|
|
|
|
|
ORBIT_DATABASE_WRITE, |
|
|
} from '@ezerous/breeze/src/orbit/orbitActions'; |
|
|
} from '@ezerous/breeze/src/orbit/orbitActions'; |
|
|
import determineDBAddress from '../../orbit/orbitUtils'; |
|
|
import determineDBAddress from '../../orbit/orbitUtils'; |
|
|
import { |
|
|
import { FETCH_USER_DATABASE, UPDATE_ORBIT_DATA } from '../actions/peerDbReplicationActions'; |
|
|
ADD_USER_POST, |
|
|
|
|
|
ADD_USER_TOPIC, |
|
|
|
|
|
FETCH_USER_POST, |
|
|
|
|
|
FETCH_USER_TOPIC, |
|
|
|
|
|
UPDATE_ORBIT_DATA, |
|
|
|
|
|
} from '../actions/peerDbReplicationActions'; |
|
|
|
|
|
|
|
|
|
|
|
function* fetchUserDb({ orbit, peerDbAddress }) { |
|
|
|
|
|
yield put(createOrbitDatabase(orbit, { name: peerDbAddress, type: 'keyvalue' })); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function* fetchTopic({ orbit, userAddress, topicId }) { |
|
|
function* fetchUserDb({ orbit, userAddress }) { |
|
|
const previousTopics = yield select((state) => state.orbitData.topics); |
|
|
|
|
|
const peerDbAddress = yield call(determineDBAddress, { |
|
|
const peerDbAddress = yield call(determineDBAddress, { |
|
|
orbit, dbName: 'topics', type: 'keyvalue', identityId: userAddress, |
|
|
orbit, dbName: 'topics', type: 'keyvalue', identityId: userAddress, |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
if (previousTopics === undefined || !previousTopics.some((topic) => topic.topicId === topicId)) { |
|
|
yield put(createOrbitDatabase(orbit, { name: peerDbAddress, type: 'keyvalue' })); |
|
|
yield put({ |
|
|
|
|
|
type: ADD_USER_TOPIC, |
|
|
|
|
|
topic: { |
|
|
|
|
|
userAddress, |
|
|
|
|
|
dbAddress: peerDbAddress, |
|
|
|
|
|
topicId, |
|
|
|
|
|
subject: null, |
|
|
|
|
|
}, |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
yield call(fetchUserDb, { orbit, peerDbAddress }); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function* fetchUserPost({ orbit, userAddress, postId }) { |
|
|
function* updateReduxState({ database }) { |
|
|
const previousPosts = yield select((state) => state.orbitData.posts); |
|
|
const { topics, posts } = yield select((state) => ({ |
|
|
const peerDbAddress = yield call(determineDBAddress, { |
|
|
topics: state.orbitData.topics, |
|
|
orbit, dbName: 'posts', type: 'keyvalue', identityId: userAddress, |
|
|
posts: state.orbitData.posts, |
|
|
}); |
|
|
})); |
|
|
|
|
|
|
|
|
if (previousPosts === undefined || !previousPosts.some((post) => post.postId === postId)) { |
|
|
if (database.dbname === 'topics') { |
|
|
yield put({ |
|
|
yield put({ |
|
|
type: ADD_USER_POST, |
|
|
type: UPDATE_ORBIT_DATA, |
|
|
posts: { |
|
|
topics: [...Object.entries(database.all).map(([key, value]) => ({ |
|
|
userAddress, |
|
|
id: parseInt(key, 10), |
|
|
dbAddress: peerDbAddress, |
|
|
subject: value.subject, |
|
|
postId, |
|
|
}))], |
|
|
subject: null, |
|
|
posts: [...posts], |
|
|
message: null, |
|
|
|
|
|
}, |
|
|
|
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
yield call(fetchUserDb, { orbit, peerDbAddress }); |
|
|
if (database.dbname === 'posts') { |
|
|
} |
|
|
yield put({ |
|
|
|
|
|
type: UPDATE_ORBIT_DATA, |
|
|
function* updateReduxState({ database }) { |
|
|
topics: [...topics], |
|
|
const { topics, posts } = yield select((state) => ({ topics: state.orbitData.topics, posts: state.orbitData.posts })); |
|
|
posts: [...Object.entries(database.all).map(([key, value]) => ({ |
|
|
|
|
|
id: parseInt(key, 10), |
|
|
yield put({ |
|
|
subject: value.subject, |
|
|
type: UPDATE_ORBIT_DATA, |
|
|
message: value.message, |
|
|
topics: topics.map((topic) => { |
|
|
}))], |
|
|
if (database.id === topic.dbAddress) { |
|
|
}); |
|
|
return ({ |
|
|
} |
|
|
...topic, |
|
|
|
|
|
...database.get(topic.topicId), |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return { ...topic }; |
|
|
|
|
|
}), |
|
|
|
|
|
posts: posts.map((post) => { |
|
|
|
|
|
if (database.id === post.dbAddress) { |
|
|
|
|
|
return ({ |
|
|
|
|
|
...post, |
|
|
|
|
|
...database.get(post.postId), |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return { ...post }; |
|
|
|
|
|
}), |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function* peerDbReplicationSaga() { |
|
|
function* peerDbReplicationSaga() { |
|
|
yield takeEvery(FETCH_USER_TOPIC, fetchTopic); |
|
|
yield takeEvery(FETCH_USER_DATABASE, fetchUserDb); |
|
|
yield takeEvery(FETCH_USER_POST, fetchUserPost); |
|
|
|
|
|
yield takeEvery(ORBIT_DATABASE_REPLICATED, updateReduxState); |
|
|
yield takeEvery(ORBIT_DATABASE_REPLICATED, updateReduxState); |
|
|
yield takeEvery(ORBIT_DATABASE_READY, updateReduxState); |
|
|
yield takeEvery(ORBIT_DATABASE_READY, updateReduxState); |
|
|
|
|
|
yield takeEvery(ORBIT_DATABASE_WRITE, updateReduxState); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
export default peerDbReplicationSaga; |
|
|
export default peerDbReplicationSaga; |
|
|