From ec1633f97bace46204c1bcb27c0d4fd7cf03c6f4 Mon Sep 17 00:00:00 2001 From: haad Date: Mon, 12 Sep 2016 20:14:00 +0200 Subject: [PATCH] Fix network name. Cleanup OrbitDB.js. --- src/OrbitDB.js | 128 +++++++++++++------------------------------------ 1 file changed, 34 insertions(+), 94 deletions(-) diff --git a/src/OrbitDB.js b/src/OrbitDB.js index 8606605..038a07e 100644 --- a/src/OrbitDB.js +++ b/src/OrbitDB.js @@ -1,65 +1,62 @@ -'use strict'; +'use strict' -const EventEmitter = require('events').EventEmitter; -const Logger = require('logplease'); -const logger = Logger.create("orbit-db", { color: Logger.Colors.Magenta }); -const EventStore = require('orbit-db-eventstore'); -const FeedStore = require('orbit-db-feedstore'); -const KeyValueStore = require('orbit-db-kvstore'); -const CounterStore = require('orbit-db-counterstore'); -const Pubsub = require('orbit-db-pubsub'); -const Cache = require('./Cache'); +const Logger = require('logplease') +const logger = Logger.create("orbit-db", { color: Logger.Colors.Magenta }) +const EventEmitter = require('events').EventEmitter +const EventStore = require('orbit-db-eventstore') +const FeedStore = require('orbit-db-feedstore') +const KeyValueStore = require('orbit-db-kvstore') +const CounterStore = require('orbit-db-counterstore') +const Pubsub = require('orbit-db-pubsub') +const Cache = require('./Cache') class OrbitDB { constructor(ipfs, id, options) { - this._ipfs = ipfs; + this._ipfs = ipfs this._pubsub = options && options.broker ? new options.broker(ipfs) : new Pubsub(ipfs) this.user = { id: id } - this.network = null; - this.events = new EventEmitter(); - this.stores = {}; + this.network = { name: 'Orbit PUBSUB network' } + this.events = new EventEmitter() + this.stores = {} } /* Databases */ feed(dbname, options) { - return this._createStore(FeedStore, dbname, options); + return this._createStore(FeedStore, dbname, options) } eventlog(dbname, options) { - return this._createStore(EventStore, dbname, options); + return this._createStore(EventStore, dbname, options) } kvstore(dbname, options) { - return this._createStore(KeyValueStore, dbname, options); + return this._createStore(KeyValueStore, dbname, options) } counter(dbname, options) { - return this._createStore(CounterStore, dbname, options); + return this._createStore(CounterStore, dbname, options) } disconnect() { - if(this._pubsub) this._pubsub.disconnect(); - this.events.removeAllListeners('data'); + if(this._pubsub) this._pubsub.disconnect() + this.events.removeAllListeners('data') Object.keys(this.stores).map((e) => this.stores[e]).forEach((store) => { - store.events.removeAllListeners('data'); - store.events.removeAllListeners('write'); - store.events.removeAllListeners('close'); - }); - this.stores = {}; - this.user = null; - this.network = null; + store.events.removeAllListeners('data') + store.events.removeAllListeners('write') + store.events.removeAllListeners('close') + }) + this.stores = {} + this.user = null + this.network = null } _createStore(Store, dbname, options = { subscribe: true }) { - // if(!options) options = {}; - // const replicate = options.subscribe !== undefined ? options.subscribe : true; - const store = new Store(this._ipfs, this.user.id, dbname, options); - this.stores[dbname] = store; - return this._subscribe(store, dbname, options.subscribe, options); + const store = new Store(this._ipfs, this.user.id, dbname, options) + this.stores[dbname] = store + return this._subscribe(store, dbname, options.subscribe, options) } _subscribe(store, dbname, subscribe = true, options) { - // if(subscribe === undefined) subscribe = true store.events.on('data', this._onData.bind(this)) store.events.on('write', this._onWrite.bind(this)) store.events.on('close', this._onClose.bind(this)) @@ -67,7 +64,7 @@ class OrbitDB { if(subscribe && this._pubsub) this._pubsub.subscribe(dbname, this._onMessage.bind(this), this._onConnected.bind(this), store.options.maxHistory > 0) else - store.loadHistory().catch((e) => logger.error(e.stack)); + store.loadHistory().catch((e) => logger.error(e.stack)) Cache.loadCache(options.cacheFile).then(() => { const hash = Cache.get(dbname) @@ -81,7 +78,7 @@ class OrbitDB { /* Connected to the message broker */ _onConnected(dbname, hash) { - // console.log(".CONNECTED", dbname, hash, this.user.username); + // console.log(".CONNECTED", dbname, hash, this.user.username) const store = this.stores[dbname] store.loadHistory(hash).catch((e) => logger.error(e.stack)) } @@ -108,7 +105,7 @@ class OrbitDB { _onData(dbname, item) { // 'New database entry...', after a new entry was added to the database - // console.log(".SYNCED", dbname, items.length); + // console.log(".SYNCED", dbname, items.length) this.events.emit('data', dbname, item) } @@ -116,63 +113,6 @@ class OrbitDB { if(this._pubsub) this._pubsub.unsubscribe(dbname) delete this.stores[dbname] } - - _connect(hash, username, password, allowOffline) { - if(allowOffline === undefined) allowOffline = false - - const readNetworkInfo = (hash) => { - return new Promise((resolve, reject) => { - resolve(JSON.stringify({ - name: 'Orbit DEV Network', - publishers: [hash] - })); - }); - }; - - let host, port, name; - return readNetworkInfo(hash) - .then((object) => { - this.network = JSON.parse(object); - name = this.network.name; - host = this.network.publishers[0].split(":")[0]; - port = this.network.publishers[0].split(":")[1]; - }) - .then(() => { - logger.debug(`Connecting to network ${hash} (${host}:${port})`); - return this._pubsub.connect(host, port, username, password) - }) - .then(() => { - logger.debug(`Connected to network ${hash} (${host}:${port})`); - this.user = { username: username, id: username } // TODO: user id from ipfs hash - return; - }) - .catch((e) => { - logger.warn(`Couldn't connect to ${hash} network: ${e.message}`); - if(!allowOffline) { - logger.debug(`'allowOffline' set to false, terminating`); - if(this._pubsub) this._pubsub.disconnect(); - throw e; - } - this.user = { username: username, id: username } // TODO: user id from ipfs hash - return; - }); - } } -class OrbitClientFactory { - static connect(host, username, password, ipfs, options = { allowOffline: false }) { - // if(!options) options = { allowOffline: false }; - if(!ipfs) { - logger.error("IPFS instance not provided"); - throw new Error("IPFS instance not provided"); - } - - const client = new OrbitDB(ipfs, options); - client.user = { username: username, id: username } // TODO: user id from ipfs hash - return Promise.resolve(client) - // return client._connect(host, username, password, options.allowOffline) - // .then(() => client) - } -} - -module.exports = OrbitDB; +module.exports = OrbitDB