diff --git a/src/orbitdb/index.js b/src/orbitdb/index.js index 65fd9d8..1b4d352 100644 --- a/src/orbitdb/index.js +++ b/src/orbitdb/index.js @@ -1,5 +1,6 @@ import BoardStore from 'orbit-db-discussion-board' import multihashes from 'multihashes' +import { getBoardIdFromAddress } from '../utils/orbitdb' export function isValidID(id) { try { @@ -48,10 +49,66 @@ export async function open(id, metadata, options = {}) { throw new Error('invalid address') } const db = await window.orbitDb.open(address, Object.assign(defaultOptions, options)) - if (metadata) { + await db.load() + if (metadata && defaultOptions.create) { await db.updateMetadata(metadata) } if (!window.dbs) window.dbs = {} window.dbs[db.address.toString()] = db return db } + +export function connectDb(db, dispatch) { + db.events.on('write', (dbname, hash, entry) => { + dispatch({ + type: 'ORBITDB_WRITE', + id: getBoardIdFromAddress(db.address.toString()), + hash, + entry + }) + }) + db.events.on('replicated', address => { + dispatch({ + type: 'ORBITDB_REPLICATED', + id: getBoardIdFromAddress(db.address.toString()) + }) + }) + db.events.on('replicate.progress', (address, hash, entry, progress, have) => { + dispatch({ + type: 'ORBITDB_REPLICATE_PROGRESS', + id: getBoardIdFromAddress(db.address.toString()), + hash, + entry, + progress, + have + }) + }) + db.events.on('replicate', address => { + dispatch({ + type: 'ORBITDB_REPLICATE', + id: getBoardIdFromAddress(db.address.toString()) + }) + }) + db.events.on('close', address => { + dispatch({ + type: 'ORBITDB_CLOSE', + id: getBoardIdFromAddress(db.address.toString()) + }) + }) + db.events.on('load', address => { + dispatch({ + type: 'ORBITDB_LOAD', + id: getBoardIdFromAddress(db.address.toString()) + }) + }) + db.events.on('load.progress', (address, hash, entry, progress, total) => { + dispatch({ + type: 'ORBITDB_LOAD_PROGRESS', + id: getBoardIdFromAddress(db.address.toString()), + hash, + entry, + progress, + total + }) + }) +} diff --git a/src/sagas/boards.js b/src/sagas/boards.js index 4689801..5534484 100644 --- a/src/sagas/boards.js +++ b/src/sagas/boards.js @@ -1,6 +1,6 @@ import { put, call } from 'redux-saga/effects' import { push } from 'react-router-redux' -import { open } from '../orbitdb' +import { open, connectDb } from '../orbitdb' import { creatingBoard, createdBoard, boardError } from '../actions/board' import { getBoardIdFromAddress } from '../utils/orbitdb' @@ -8,7 +8,9 @@ export function* createBoard({ board }) { yield put(creatingBoard(board)) let db try { - db = yield call(open, board.id) + db = yield call(open, board.id, { + title: board.title + }) } catch (error) { yield put(boardError, error) } @@ -17,7 +19,8 @@ export function* createBoard({ board }) { id: getBoardIdFromAddress(address), address } - // TODO watch db status + // TODO: use https://redux-saga.js.org/docs/advanced/Channels.html to handle orbitdb events + // yield call(connectDb(db, dispatch)) yield put(createdBoard(Object.assign({}, board, dbInfo))) yield put(push('/b/' + dbInfo.id + '/')) } \ No newline at end of file