Refactor OrbitDB store creation

This commit is contained in:
haad 2016-05-08 18:12:04 +02:00
parent 44e6dbf77c
commit 5ce23678b4

View File

@ -18,53 +18,40 @@ class OrbitDB {
this.stores = {}; this.stores = {};
} }
/* Databases */
feed(dbname, options) { feed(dbname, options) {
if(!options) options = {}; return this._createStore(FeedStore, dbname, options);
if(options.subscribe === undefined) Object.assign(options, { subscribe: true });
const store = new FeedStore(this._ipfs, this.user.username, dbname, options);
return this._subscribe(store, dbname, options.subscribe)
.then(() => this.stores[dbname] = store)
.then(() => store);
} }
eventlog(dbname, options) { eventlog(dbname, options) {
if(!options) options = {}; return this._createStore(EventStore, dbname, options);
if(options.subscribe === undefined) Object.assign(options, { subscribe: true });
const store = new EventStore(this._ipfs, this.user.username, dbname, options);
return this._subscribe(store, dbname, options.subscribe)
.then(() => this.stores[dbname] = store)
.then(() => store);
} }
kvstore(dbname, options) { kvstore(dbname, options) {
if(!options) options = {}; return this._createStore(KeyValueStore, dbname, options);
if(options.subscribe === undefined) Object.assign(options, { subscribe: true });
const store = new KeyValueStore(this._ipfs, this.user.username, dbname, options);
return this._subscribe(store, dbname, options.subscribe)
.then(() => this.stores[dbname] = store)
.then(() => store);
} }
counter(dbname, options) { counter(dbname, options) {
if(!options) options = {}; return this._createStore(CounterStore, dbname, options);
if(options.subscribe === undefined) Object.assign(options, { subscribe: true });
const store = new CounterStore(this._ipfs, this.user.username, dbname, options);
return this._subscribe(store, dbname, options.subscribe)
.then(() => this.stores[dbname] = store)
.then(() => store);
} }
disconnect() { disconnect() {
this._pubsub.disconnect(); this._pubsub.disconnect();
this._store = {}; this.stores = {};
this.user = null; this.user = null;
this.network = null; this.network = null;
} }
_createStore(Store, dbname, options) {
if(!options) options = {};
if(options.subscribe === undefined) Object.assign(options, { subscribe: true });
const store = new Store(this._ipfs, this.user.username, dbname, options);
return this._subscribe(store, dbname, options.subscribe)
.then(() => this.stores[dbname] = store)
.then(() => store);
}
_subscribe(store, dbname, subscribe, callback) { _subscribe(store, dbname, subscribe, callback) {
if(subscribe === undefined) subscribe = true; if(subscribe === undefined) subscribe = true;