Add options, cache filename

This commit is contained in:
haad 2016-03-22 18:26:48 +01:00
parent 499e443818
commit 42454fa598
4 changed files with 19 additions and 15 deletions

View File

@ -3,15 +3,15 @@
const fs = require('fs');
const path = require('path');
const filename = 'orbit-db-cache.json';
const defaultFilename = 'orbit-db-cache.json';
let cache = {};
class Cache {
static set(key, value) {
cache[key] = value;
fs.writeFile(path.resolve(filename), JSON.stringify(cache, null, 2) + "\n", (err) => {
fs.writeFile(path.resolve(defaultFilename), JSON.stringify(cache, null, 2) + "\n", (err) => {
if (err) throw err;
// console.log('It\'s saved!', path.resolve(filename));
// console.log('It\'s saved!', path.resolve(defaultFilename));
});
}
@ -19,10 +19,11 @@ class Cache {
return cache[key];
}
static loadCache() {
if(fs.existsSync(path.resolve(filename))) {
console.log('Load cache from', path.resolve(filename));
cache = JSON.parse(fs.readFileSync(path.resolve(filename)));
static loadCache(cacheFile) {
cacheFile = cacheFile ? cacheFile : defaultFilename;
if(fs.existsSync(path.resolve(defaultFilename))) {
console.log('Load cache from', path.resolve(defaultFilename));
cache = JSON.parse(fs.readFileSync(path.resolve(defaultFilename)));
}
}
}

View File

@ -8,13 +8,14 @@ const PubSub = require('./PubSub');
const OrbitDB = require('./OrbitDB');
class Client {
constructor(ipfs, daemon) {
constructor(ipfs, options) {
this._ipfs = ipfs;
this._pubsub = null;
this.user = null;
this.network = null;
this.events = new EventEmitter();
this.db = new OrbitDB(this._ipfs);
this.options = options || {};
this.db = new OrbitDB(this._ipfs, this.options);
}
channel(channel, password, subscribe) {
@ -108,14 +109,15 @@ class Client {
}
class OrbitClientFactory {
static connect(host, port, username, password, allowOffline, ipfs) {
static connect(host, port, username, password, ipfs, options) {
options = options ? options : {};
if(!ipfs) {
let ipfsd = await(ipfsDaemon());
ipfs = ipfsd.ipfs;
}
const client = new Client(ipfs);
await(client._connect(host, port, username, password, allowOffline))
const client = new Client(ipfs, options);
await(client._connect(host, port, username, password, options.allowOffline))
return client;
}
}

View File

@ -10,10 +10,11 @@ const Post = require('./post/Post');
const Cache = require('./Cache');
class OrbitDB {
constructor(ipfs) {
constructor(ipfs, options) {
this._ipfs = ipfs;
this._logs = {};
this.events = {};
this.options = options || {};
}
/* Public methods */
@ -23,7 +24,7 @@ class OrbitDB {
this.events[channel] = new EventEmitter();
this.events[channel].emit('load', channel);
Cache.loadCache();
Cache.loadCache(this.options.cacheFile);
this.sync(channel, Cache.get(channel));
this.events[channel].emit('loaded', channel);
}

View File

@ -21,7 +21,7 @@ describe('Orbit Client', function() {
let channel = 'abcdefgh';
before(async((done) => {
client = await(OrbitClient.connect(host, port, username, password, true));
client = await(OrbitClient.connect(host, port, username, password, null, { allowOffline: true }));
db = client.channel(channel, '', false);
db.delete();
done();