diff --git a/src/Cache.js b/src/Cache.js index 3046e07..5353584 100644 --- a/src/Cache.js +++ b/src/Cache.js @@ -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))); } } } diff --git a/src/Client.js b/src/Client.js index 93cc7b0..fb684c6 100644 --- a/src/Client.js +++ b/src/Client.js @@ -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; } } diff --git a/src/OrbitDB.js b/src/OrbitDB.js index dda1a39..daef9e8 100644 --- a/src/OrbitDB.js +++ b/src/OrbitDB.js @@ -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); } diff --git a/test/orbit-client-tests.js b/test/orbit-client-tests.js index b60a4d4..fac7146 100644 --- a/test/orbit-client-tests.js +++ b/test/orbit-client-tests.js @@ -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();