|
@ -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 }) { |
|
|
|
|
|
const previousPosts = yield select((state) => state.orbitData.posts); |
|
|
|
|
|
const peerDbAddress = yield call(determineDBAddress, { |
|
|
|
|
|
orbit, dbName: 'posts', type: 'keyvalue', identityId: userAddress, |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
if (previousPosts === undefined || !previousPosts.some((post) => post.postId === postId)) { |
|
|
|
|
|
yield put({ |
|
|
|
|
|
type: ADD_USER_POST, |
|
|
|
|
|
posts: { |
|
|
|
|
|
userAddress, |
|
|
|
|
|
dbAddress: peerDbAddress, |
|
|
|
|
|
postId, |
|
|
|
|
|
subject: null, |
|
|
|
|
|
message: null, |
|
|
|
|
|
}, |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
yield call(fetchUserDb, { orbit, peerDbAddress }); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function* updateReduxState({ database }) { |
|
|
function* updateReduxState({ database }) { |
|
|
const { topics, posts } = yield select((state) => ({ topics: state.orbitData.topics, posts: state.orbitData.posts })); |
|
|
const { topics, posts } = yield select((state) => ({ |
|
|
|
|
|
topics: state.orbitData.topics, |
|
|
|
|
|
posts: state.orbitData.posts, |
|
|
|
|
|
})); |
|
|
|
|
|
|
|
|
|
|
|
if (database.dbname === 'topics') { |
|
|
yield put({ |
|
|
yield put({ |
|
|
type: UPDATE_ORBIT_DATA, |
|
|
type: UPDATE_ORBIT_DATA, |
|
|
topics: topics.map((topic) => { |
|
|
topics: [...Object.entries(database.all).map(([key, value]) => ({ |
|
|
if (database.id === topic.dbAddress) { |
|
|
id: parseInt(key, 10), |
|
|
return ({ |
|
|
subject: value.subject, |
|
|
...topic, |
|
|
}))], |
|
|
...database.get(topic.topicId), |
|
|
posts: [...posts], |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return { ...topic }; |
|
|
if (database.dbname === 'posts') { |
|
|
}), |
|
|
yield put({ |
|
|
posts: posts.map((post) => { |
|
|
type: UPDATE_ORBIT_DATA, |
|
|
if (database.id === post.dbAddress) { |
|
|
topics: [...topics], |
|
|
return ({ |
|
|
posts: [...Object.entries(database.all).map(([key, value]) => ({ |
|
|
...post, |
|
|
id: parseInt(key, 10), |
|
|
...database.get(post.postId), |
|
|
subject: value.subject, |
|
|
|
|
|
message: value.message, |
|
|
|
|
|
}))], |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
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; |
|
|