1
0
mirror of https://github.com/fazo96/ipfs-boards synced 2025-01-09 12:19:49 +01:00

WIP connection of orbit-db events to redux actions

This commit is contained in:
Enrico Fasoli 2018-02-05 18:30:23 +01:00
parent 878de1a5d7
commit c9218d94e6
No known key found for this signature in database
GPG Key ID: 1238873C5F27DB4D
2 changed files with 64 additions and 4 deletions

View File

@ -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
})
})
}

View File

@ -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 + '/'))
}