pull/41/head
マリウス 2 years ago
parent d77e3f6823
commit b0371b59ee
No known key found for this signature in database
GPG Key ID: 272ED814BF63261F

@ -7,7 +7,6 @@ import (
orbitdb "berty.tech/go-orbit-db" orbitdb "berty.tech/go-orbit-db"
"berty.tech/go-orbit-db/accesscontroller" "berty.tech/go-orbit-db/accesscontroller"
"berty.tech/go-orbit-db/events"
"berty.tech/go-orbit-db/iface" "berty.tech/go-orbit-db/iface"
"berty.tech/go-orbit-db/stores" "berty.tech/go-orbit-db/stores"
"berty.tech/go-orbit-db/stores/documentstore" "berty.tech/go-orbit-db/stores/documentstore"
@ -16,6 +15,7 @@ import (
icore "github.com/ipfs/interface-go-ipfs-core" icore "github.com/ipfs/interface-go-ipfs-core"
"github.com/libp2p/go-libp2p-core/crypto" "github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/event"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
"go.uber.org/zap" "go.uber.org/zap"
@ -37,7 +37,7 @@ type Database struct {
OrbitDB orbitdb.OrbitDB OrbitDB orbitdb.OrbitDB
Store orbitdb.DocumentStore Store orbitdb.DocumentStore
StoreEventChan <-chan events.Event Events event.Subscription
} }
func (db *Database) init() (error) { func (db *Database) init() (error) {
@ -79,7 +79,7 @@ func (db *Database) init() (error) {
return err return err
} }
db.StoreEventChan = db.Store.Subscribe(db.ctx) db.Events, err = db.Store.EventBus().Subscribe(new(stores.EventReady))
return nil return nil
} }
@ -156,6 +156,7 @@ func NewDatabase(
func (db *Database) Connect(onReady func(address string)) (error) { func (db *Database) Connect(onReady func(address string)) (error) {
var err error var err error
// if db.Init { // if db.Init {
err = db.init() err = db.init()
if err != nil { if err != nil {
@ -186,12 +187,13 @@ func (db *Database) Connect(onReady func(address string)) (error) {
go func() { go func() {
for { for {
for ev := range db.StoreEventChan { for ev := range db.Events.Out() {
db.Logger.Debug("got event", zap.Any("event", ev)) db.Logger.Debug("got event", zap.Any("event", ev))
switch ev.(type) { switch ev.(type) {
case *stores.EventReady: case stores.EventReady:
db.URI = db.Store.Address().String() db.URI = db.Store.Address().String()
onReady(db.URI) onReady(db.URI)
continue
} }
} }
} }
@ -209,6 +211,8 @@ func (db *Database) Connect(onReady func(address string)) (error) {
} }
func (db *Database) Disconnect() { func (db *Database) Disconnect() {
db.Events.Close()
db.Store.Close()
db.OrbitDB.Close() db.OrbitDB.Close()
} }

Loading…
Cancel
Save