mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-06-27 08:22:31 +00:00
Add options, cache filename
This commit is contained in:
parent
499e443818
commit
42454fa598
15
src/Cache.js
15
src/Cache.js
@ -3,15 +3,15 @@
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
const filename = 'orbit-db-cache.json';
|
const defaultFilename = 'orbit-db-cache.json';
|
||||||
let cache = {};
|
let cache = {};
|
||||||
|
|
||||||
class Cache {
|
class Cache {
|
||||||
static set(key, value) {
|
static set(key, value) {
|
||||||
cache[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;
|
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];
|
return cache[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
static loadCache() {
|
static loadCache(cacheFile) {
|
||||||
if(fs.existsSync(path.resolve(filename))) {
|
cacheFile = cacheFile ? cacheFile : defaultFilename;
|
||||||
console.log('Load cache from', path.resolve(filename));
|
if(fs.existsSync(path.resolve(defaultFilename))) {
|
||||||
cache = JSON.parse(fs.readFileSync(path.resolve(filename)));
|
console.log('Load cache from', path.resolve(defaultFilename));
|
||||||
|
cache = JSON.parse(fs.readFileSync(path.resolve(defaultFilename)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,13 +8,14 @@ const PubSub = require('./PubSub');
|
|||||||
const OrbitDB = require('./OrbitDB');
|
const OrbitDB = require('./OrbitDB');
|
||||||
|
|
||||||
class Client {
|
class Client {
|
||||||
constructor(ipfs, daemon) {
|
constructor(ipfs, options) {
|
||||||
this._ipfs = ipfs;
|
this._ipfs = ipfs;
|
||||||
this._pubsub = null;
|
this._pubsub = null;
|
||||||
this.user = null;
|
this.user = null;
|
||||||
this.network = null;
|
this.network = null;
|
||||||
this.events = new EventEmitter();
|
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) {
|
channel(channel, password, subscribe) {
|
||||||
@ -108,14 +109,15 @@ class Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class OrbitClientFactory {
|
class OrbitClientFactory {
|
||||||
static connect(host, port, username, password, allowOffline, ipfs) {
|
static connect(host, port, username, password, ipfs, options) {
|
||||||
|
options = options ? options : {};
|
||||||
if(!ipfs) {
|
if(!ipfs) {
|
||||||
let ipfsd = await(ipfsDaemon());
|
let ipfsd = await(ipfsDaemon());
|
||||||
ipfs = ipfsd.ipfs;
|
ipfs = ipfsd.ipfs;
|
||||||
}
|
}
|
||||||
|
|
||||||
const client = new Client(ipfs);
|
const client = new Client(ipfs, options);
|
||||||
await(client._connect(host, port, username, password, allowOffline))
|
await(client._connect(host, port, username, password, options.allowOffline))
|
||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,10 +10,11 @@ const Post = require('./post/Post');
|
|||||||
const Cache = require('./Cache');
|
const Cache = require('./Cache');
|
||||||
|
|
||||||
class OrbitDB {
|
class OrbitDB {
|
||||||
constructor(ipfs) {
|
constructor(ipfs, options) {
|
||||||
this._ipfs = ipfs;
|
this._ipfs = ipfs;
|
||||||
this._logs = {};
|
this._logs = {};
|
||||||
this.events = {};
|
this.events = {};
|
||||||
|
this.options = options || {};
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Public methods */
|
/* Public methods */
|
||||||
@ -23,7 +24,7 @@ class OrbitDB {
|
|||||||
this.events[channel] = new EventEmitter();
|
this.events[channel] = new EventEmitter();
|
||||||
|
|
||||||
this.events[channel].emit('load', channel);
|
this.events[channel].emit('load', channel);
|
||||||
Cache.loadCache();
|
Cache.loadCache(this.options.cacheFile);
|
||||||
this.sync(channel, Cache.get(channel));
|
this.sync(channel, Cache.get(channel));
|
||||||
this.events[channel].emit('loaded', channel);
|
this.events[channel].emit('loaded', channel);
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ describe('Orbit Client', function() {
|
|||||||
let channel = 'abcdefgh';
|
let channel = 'abcdefgh';
|
||||||
|
|
||||||
before(async((done) => {
|
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 = client.channel(channel, '', false);
|
||||||
db.delete();
|
db.delete();
|
||||||
done();
|
done();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user